Skip to content

Commit 9891b06

Browse files
committed
Merge branch 'joaquim/gas' of github.com:thirdweb-dev/web3-api into joaquim/gas
2 parents 81de10a + f0c0a29 commit 9891b06

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/db/transactions/queueTx.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ interface QueueTxParams {
1515
deployedContractType?: string;
1616
simulateTx?: boolean;
1717
idempotencyKey?: string;
18+
txOverrides?: {
19+
gasLimit?: string;
20+
maxFeePerGas?: string;
21+
maxPriorityFeePerGas?: string;
22+
};
1823
}
1924

2025
export const queueTx = async ({
@@ -26,6 +31,7 @@ export const queueTx = async ({
2631
deployedContractType,
2732
simulateTx,
2833
idempotencyKey,
34+
txOverrides,
2935
}: QueueTxParams) => {
3036
// TODO: We need a much safer way of detecting if the transaction should be a user operation
3137
const isUserOp = !!(tx.getSigner as ERC4337EthersSigner).erc4337provider;
@@ -38,6 +44,7 @@ export const queueTx = async ({
3844
deployedContractType: deployedContractType,
3945
data: tx.encode(),
4046
value: BigNumber.from(await tx.getValue()).toHexString(),
47+
...txOverrides,
4148
};
4249

4350
if (isUserOp) {

src/server/routes/contract/write/write.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,23 @@ export async function writeToContract(fastify: FastifyInstance) {
8888
const tx = contract.prepare(functionName, args, {
8989
value: txOverrides?.value,
9090
gasLimit: txOverrides?.gas,
91+
maxFeePerGas: txOverrides?.maxFeePerGas,
92+
maxPriorityFeePerGas: txOverrides?.maxPriorityFeePerGas,
9193
});
9294

95+
const gasLimit = txOverrides?.gas;
96+
delete txOverrides?.gas;
97+
9398
const queueId = await queueTx({
9499
tx,
95100
chainId,
96101
simulateTx,
97102
extension: "none",
98103
idempotencyKey,
104+
txOverrides: {
105+
...txOverrides,
106+
gasLimit,
107+
},
99108
});
100109

101110
reply.status(StatusCodes.OK).send({

src/server/schemas/txOverrides.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ export const txOverrides = Type.Object({
1515
description: "Gas limit for the transaction",
1616
}),
1717
),
18+
maxFeePerGas: Type.Optional(
19+
Type.String({
20+
examples: ["1000000000"],
21+
description: "Maximum fee per gas",
22+
}),
23+
),
24+
maxPriorityFeePerGas: Type.Optional(
25+
Type.String({
26+
examples: ["1000000000"],
27+
description: "Maximum priority fee per gas",
28+
}),
29+
),
1830
}),
1931
),
2032
});

src/worker/tasks/processTx.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,27 @@ export const processTx = async () => {
171171
...gasOverrides,
172172
});
173173

174-
// TODO: We need to target specific cases
175-
// Bump gas limit to avoid occasional out of gas errors
176-
txRequest.gasLimit = txRequest.gasLimit
177-
? BigNumber.from(txRequest.gasLimit).mul(120).div(100)
178-
: undefined;
174+
// Gas limit override
175+
if (tx.gasLimit) {
176+
txRequest.gasLimit = BigNumber.from(tx.gasLimit);
177+
} else {
178+
// TODO: We need to target specific cases
179+
// Bump gas limit to avoid occasional out of gas errors
180+
txRequest.gasLimit = txRequest.gasLimit
181+
? BigNumber.from(txRequest.gasLimit).mul(120).div(100)
182+
: undefined;
183+
}
184+
185+
// Gas price overrides
186+
if (tx.maxFeePerGas) {
187+
txRequest.maxFeePerGas = BigNumber.from(tx.maxFeePerGas);
188+
}
189+
190+
if (tx.maxPriorityFeePerGas) {
191+
txRequest.maxPriorityFeePerGas = BigNumber.from(
192+
tx.maxPriorityFeePerGas,
193+
);
194+
}
179195

180196
const signature = await signer.signTransaction(txRequest);
181197
const rpcRequest = {
@@ -378,6 +394,24 @@ export const processTx = async () => {
378394
nonce,
379395
},
380396
);
397+
398+
// Temporary fix untill SDK allows us to do this
399+
if (tx.gasLimit) {
400+
unsignedOp.callGasLimit = BigNumber.from(tx.gasLimit);
401+
unsignedOp.paymasterAndData = "0x";
402+
const DUMMY_SIGNATURE =
403+
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
404+
unsignedOp.signature = DUMMY_SIGNATURE;
405+
const paymasterResult =
406+
await signer.smartAccountAPI.paymasterAPI.getPaymasterAndData(
407+
unsignedOp,
408+
);
409+
const paymasterAndData = paymasterResult.paymasterAndData;
410+
if (paymasterAndData && paymasterAndData !== "0x") {
411+
unsignedOp.paymasterAndData = paymasterAndData;
412+
}
413+
}
414+
381415
const userOp = await signer.smartAccountAPI.signUserOp(unsignedOp);
382416
const userOpHash = await signer.smartAccountAPI.getUserOpHash(
383417
userOp,

0 commit comments

Comments
 (0)