Skip to content

Commit

Permalink
add frequency check for ema update; cleanup/refactor limit checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dshiell committed Nov 10, 2024
1 parent 4edcfe5 commit 4cfd067
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ let keyRingId = process.env.KEY_RING_ID!
let keyId = process.env.KEY_ID!
let TX_GASPRICE_LIMIT = BigInt(process.env.TXPRICE_LIMIT!)
let TX_BLOBPRICE_LIMIT = BigInt(process.env.TX_BLOBPRICE_LIMIT!)
let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1")
let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1") // default alpha is equivalent to 0.01
const EMA_UPDATE_DELTA_SECS = BigInt(process.env.EMA_UPDATE_DELTA_SECS ?? "60") // default update frequency is once a minute

let lastUpdateTime = 0;

kmsProvider.setPath({
projectId: PROJECT_ID,
Expand Down Expand Up @@ -91,27 +94,41 @@ async function feesTooHigh(transactionArgs: TransactionArgs) {
maxFeePerBlobGas = BigInt(transactionArgs.maxFeePerBlobGas);
}

var gasPrice = (maxFeePerGas + maxPriorityFeePerGas);
const newGasLimit = computeLimitEMA(gasPrice, TX_GASPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_GASPRICE_LIMIT: %d -> %d', TX_GASPRICE_LIMIT, newGasLimit);
TX_GASPRICE_LIMIT = newGasLimit;
const now = Math.floor(Date.now() / 1000);
const elapsed_secs = now - lastUpdateTime;
const doUpdate = (elapsed_secs < EMA_UPDATE_DELTA_SECS);
const gasPrice = (maxFeePerGas + maxPriorityFeePerGas);
let rejectTxn = false;

if (doUpdate) {
lastUpdateTime = now;

// update gas limit
const newGasLimit = computeLimitEMA(gasPrice, TX_GASPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_GASPRICE_LIMIT: %d -> %d', TX_GASPRICE_LIMIT, newGasLimit);
TX_GASPRICE_LIMIT = newGasLimit;

// update blob limit
const newBlobLimit = computeLimitEMA(maxFeePerBlobGas, TX_BLOBPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_BLOBPRICE_LIMIT: %d -> %d', TX_BLOBPRICE_LIMIT, newBlobLimit);
TX_BLOBPRICE_LIMIT = newBlobLimit;
}

// check gas price limit
if (gasPrice > TX_GASPRICE_LIMIT) {
console.error('Tx fees too high: %d > %d', gasPrice, TX_GASPRICE_LIMIT);
return true;
rejectTxn = true;
}

// check blob price limits if applicable
if (transactionArgs.blobVersionedHashes && transactionArgs.blobVersionedHashes.length > 0) {
const newBlobLimit = computeLimitEMA(maxFeePerBlobGas, TX_BLOBPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_BLOBPRICE_LIMIT: %d -> %d', TX_BLOBPRICE_LIMIT, newBlobLimit);
TX_BLOBPRICE_LIMIT = newBlobLimit;

if (maxFeePerBlobGas > TX_BLOBPRICE_LIMIT) {
console.error('Blob fees too high: %d > %d', maxFeePerBlobGas, TX_BLOBPRICE_LIMIT );
return true;
rejectTxn = true;
}
}

return false;
return rejectTxn;
}

async function handleEthSignTransaction(transactionArgs: TransactionArgs) {
Expand Down

0 comments on commit 4cfd067

Please sign in to comment.