Skip to content

add gas to txOverrides #477

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

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
7 changes: 7 additions & 0 deletions src/db/transactions/queueTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ interface QueueTxParams {
deployedContractType?: string;
simulateTx?: boolean;
idempotencyKey?: string;
txOverrides?: {
gasLimit?: string;
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
};
}

export const queueTx = async ({
Expand All @@ -26,6 +31,7 @@ export const queueTx = async ({
deployedContractType,
simulateTx,
idempotencyKey,
txOverrides,
}: QueueTxParams) => {
// TODO: We need a much safer way of detecting if the transaction should be a user operation
const isUserOp = !!(tx.getSigner as ERC4337EthersSigner).erc4337provider;
Expand All @@ -38,6 +44,7 @@ export const queueTx = async ({
deployedContractType: deployedContractType,
data: tx.encode(),
value: BigNumber.from(await tx.getValue()).toHexString(),
...txOverrides,
};

if (isUserOp) {
Expand Down
14 changes: 13 additions & 1 deletion src/server/routes/contract/write/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,26 @@ export async function writeToContract(fastify: FastifyInstance) {
walletAddress,
accountAddress,
});
const tx = await contract.prepare(functionName, args, txOverrides);
const tx = contract.prepare(functionName, args, {
value: txOverrides?.value,
gasLimit: txOverrides?.gas,
maxFeePerGas: txOverrides?.maxFeePerGas,
maxPriorityFeePerGas: txOverrides?.maxPriorityFeePerGas,
});

const gasLimit = txOverrides?.gas;
delete txOverrides?.gas;

const queueId = await queueTx({
tx,
chainId,
simulateTx,
extension: "none",
idempotencyKey,
txOverrides: {
...txOverrides,
gasLimit,
},
});

reply.status(StatusCodes.OK).send({
Expand Down
20 changes: 19 additions & 1 deletion src/server/schemas/txOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ export const txOverrides = Type.Object({
Type.Object({
value: Type.Optional(
Type.String({
examples: ["0"],
examples: ["10000000000"],
description: "Amount of native currency to send",
}),
),
gas: Type.Optional(
Type.String({
examples: ["530000"],
description: "Gas limit for the transaction",
}),
),
maxFeePerGas: Type.Optional(
Type.String({
examples: ["1000000000"],
description: "Maximum fee per gas",
}),
),
maxPriorityFeePerGas: Type.Optional(
Type.String({
examples: ["1000000000"],
description: "Maximum priority fee per gas",
}),
),
}),
),
});
44 changes: 39 additions & 5 deletions src/worker/tasks/processTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,27 @@ export const processTx = async () => {
...gasOverrides,
});

// TODO: We need to target specific cases
// Bump gas limit to avoid occasional out of gas errors
txRequest.gasLimit = txRequest.gasLimit
? BigNumber.from(txRequest.gasLimit).mul(120).div(100)
: undefined;
// Gas limit override
if (tx.gasLimit) {
txRequest.gasLimit = BigNumber.from(tx.gasLimit);
} else {
// TODO: We need to target specific cases
// Bump gas limit to avoid occasional out of gas errors
txRequest.gasLimit = txRequest.gasLimit
? BigNumber.from(txRequest.gasLimit).mul(120).div(100)
: undefined;
}
Comment on lines +177 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
// TODO: We need to target specific cases
// Bump gas limit to avoid occasional out of gas errors
txRequest.gasLimit = txRequest.gasLimit
? BigNumber.from(txRequest.gasLimit).mul(120).div(100)
: undefined;
}
} else if (txRequest.gasLimit) {
// Use +20% gas limit to prevent occasional gas errors.
txRequest.gasLimit = BigNumber.from(txRequest.gasLimit).mul(120).div(100)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was added for shield (ran into issues running out of gas on airdrops). We could remove it and see if it returns?


// Gas price overrides
if (tx.maxFeePerGas) {
txRequest.maxFeePerGas = BigNumber.from(tx.maxFeePerGas);
}

if (tx.maxPriorityFeePerGas) {
txRequest.maxPriorityFeePerGas = BigNumber.from(
tx.maxPriorityFeePerGas,
);
}

const signature = await signer.signTransaction(txRequest);
const rpcRequest = {
Expand Down Expand Up @@ -378,6 +394,24 @@ export const processTx = async () => {
nonce,
},
);

// Temporary fix untill SDK allows us to do this
if (tx.gasLimit) {
unsignedOp.callGasLimit = BigNumber.from(tx.gasLimit);
unsignedOp.paymasterAndData = "0x";
const DUMMY_SIGNATURE =
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
unsignedOp.signature = DUMMY_SIGNATURE;
const paymasterResult =
await signer.smartAccountAPI.paymasterAPI.getPaymasterAndData(
unsignedOp,
);
const paymasterAndData = paymasterResult.paymasterAndData;
if (paymasterAndData && paymasterAndData !== "0x") {
unsignedOp.paymasterAndData = paymasterAndData;
}
}

const userOp = await signer.smartAccountAPI.signUserOp(unsignedOp);
const userOpHash = await signer.smartAccountAPI.getUserOpHash(
userOp,
Expand Down
Loading