Skip to content

Commit

Permalink
Merge branch 'joaquim/gas' of github.com:thirdweb-dev/web3-api into j…
Browse files Browse the repository at this point in the history
…oaquim/gas
  • Loading branch information
farhanW3 committed Apr 19, 2024
2 parents 81de10a + f0c0a29 commit 9891b06
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
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
9 changes: 9 additions & 0 deletions src/server/routes/contract/write/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,23 @@ export async function writeToContract(fastify: FastifyInstance) {
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
12 changes: 12 additions & 0 deletions src/server/schemas/txOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export const txOverrides = Type.Object({
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;
}

// 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

0 comments on commit 9891b06

Please sign in to comment.