Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding gas limit #4

Merged
merged 6 commits into from
Oct 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let PROJECT_ID = process.env.PROJECT_ID!
let LOCATION_ID = process.env.LOCATION_ID!
let keyRingId = process.env.KEY_RING_ID!
let keyId = process.env.KEY_ID!
let TX_LIMIT = BigInt(process.env.TX_LIMIT!)
dshiell marked this conversation as resolved.
Show resolved Hide resolved
dshiell marked this conversation as resolved.
Show resolved Hide resolved

kmsProvider.setPath({
projectId: PROJECT_ID,
Expand All @@ -35,6 +36,12 @@ app.post('/', async (request, reply) => {
reply.code(400).send({error: 'Invalid request'});
return;
}

let feesValid = await feesTooHigh(result.data);
jlafiandra6 marked this conversation as resolved.
Show resolved Hide resolved
if (!feesValid) {
reply.code(400).send({error: `Fees too high TX_LIMIT [${TX_LIMIT}] reached`});
return;
}
dshiell marked this conversation as resolved.
Show resolved Hide resolved
let signedTx = await handleEthSignTransaction(result.data);
reply.code(200).send({result: signedTx});
return;
Expand All @@ -50,6 +57,34 @@ app.get('/address', async (_, reply) => {
return reply.code(200).send(address);
})

async function feesTooHigh(transactionArgs: TransactionArgs) {
let maxFeePerGas = BigInt(0);
let maxPriorityFeePerGas = BigInt(0);
let maxFeePerBlobGas = BigInt(0);
if (transactionArgs.maxFeePerGas ){
maxFeePerGas = BigInt(transactionArgs.maxFeePerGas);
}
if (transactionArgs.maxPriorityFeePerGas) {
maxPriorityFeePerGas = BigInt(transactionArgs.maxPriorityFeePerGas);
}
if (transactionArgs.maxFeePerBlobGas) {
maxFeePerBlobGas = BigInt(transactionArgs.maxFeePerBlobGas);
}

var gasCost = BigInt(transactionArgs.gas) * (maxFeePerGas + maxPriorityFeePerGas);
if (gasCost > TX_LIMIT) {
return false;
}

if (transactionArgs.blobVersionedHashes && transactionArgs.blobVersionedHashes.length > 0) {
var blobGasCost = BigInt(transactionArgs.gas) * maxFeePerBlobGas;
if (blobGasCost > TX_LIMIT) {
return false;
}
}
return true;
}

async function handleEthSignTransaction(transactionArgs: TransactionArgs) {
console.log('Transaction Args:', transactionArgs);
const kzg = await loadKZG()
Expand Down
Loading