From 4cfd06776a41bd174f012c035c5c26a6b7eec298 Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 10 Nov 2024 14:48:38 -0800 Subject: [PATCH 1/2] add frequency check for ema update; cleanup/refactor limit checking --- src/server.ts | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/server.ts b/src/server.ts index 7ad6c15..1afccdc 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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, @@ -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) { From dec4be13dc88609743adbec0f253a44498be903e Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 10 Nov 2024 14:57:12 -0800 Subject: [PATCH 2/2] fix doUpdate condition --- src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index 1afccdc..7c3d121 100644 --- a/src/server.ts +++ b/src/server.ts @@ -96,7 +96,7 @@ async function feesTooHigh(transactionArgs: TransactionArgs) { const now = Math.floor(Date.now() / 1000); const elapsed_secs = now - lastUpdateTime; - const doUpdate = (elapsed_secs < EMA_UPDATE_DELTA_SECS); + const doUpdate = (elapsed_secs >= EMA_UPDATE_DELTA_SECS); const gasPrice = (maxFeePerGas + maxPriorityFeePerGas); let rejectTxn = false;