Skip to content

Commit

Permalink
fix bugs and cover with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Oct 19, 2023
1 parent 3d5a1e9 commit 55f3426
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
33 changes: 23 additions & 10 deletions packages/gelato-web3-function/delay-dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ Web3Function.onRun(async (context) => {
to: delayModAddress,
data,
});
gasBalance = gasBalance.sub(gasLimit);
if (gasBalance.lt(0)) {
throw new Error("Gas allowance exceeded");

if (gasLimit.gt(gasBalance)) {
console.log(
`Gas allowance insufficient: ${gasBalance.toString()}. Skipping call with gas limit ${gasLimit.toString()}`
);
return false;
}

gasBalance = gasBalance.sub(gasLimit);

const result = await gelatoRelay.sponsoredCall(
{ chainId: BigInt(chainId), target: delayModAddress, data },
relayApiKey,
Expand Down Expand Up @@ -170,14 +176,15 @@ Web3Function.onRun(async (context) => {
// Relay all executable transactions
for (let tx of executableTransactions) {
try {
await relay(
const result = await relay(
delayMod.interface.encodeFunctionData("executeNextTx", [
tx.to,
tx.value,
tx.data,
tx.operation,
])
);
if (result === false) break;
} catch (e: any) {
await updateStorage();
return {
Expand All @@ -187,12 +194,18 @@ Web3Function.onRun(async (context) => {
}
}

// If we get here, some calls have been relayed and all of them were successful
await updateStorage();
return {
canExec: true,
callData: [],
};
if (callsMade > 0) {
await updateStorage();
return {
canExec: true,
callData: [],
};
} else {
return {
canExec: false,
message: `Gas allowance balance of ${gasBalance} insufficient for executing next transaction from queue`,
};
}
});

const QUERY = `
Expand Down
34 changes: 33 additions & 1 deletion packages/gelato-web3-function/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,39 @@ describe("delay-dispatch web3 function", () => {
});
});

it.skip("respects the gas allowance", async () => {});
it("respects the gas allowance", async () => {
const timeFirstTxIsExpired = QUEUE[0].createdAt + COOLDOWN + EXPIRATION + 1;
jest
.spyOn(Date, "now")
.mockImplementation(() => timeFirstTxIsExpired * 1000);

const result = await runWeb3Function({
userArgs: { ...userArgs, gasAllowance: 300_000 }, // since we hard-code the gas to 123_000, this should only execute 2 txs, but not 3
provider,
});
expect(result).toEqual({ canExec: true, callData: [] });

expect(sponsoredCallSpy).toHaveBeenCalledTimes(2);
});

it("returns `canExec: false` if nothing has been relayed", async () => {
const timeFirstTxIsExpired = QUEUE[0].createdAt + COOLDOWN + EXPIRATION + 1;
jest
.spyOn(Date, "now")
.mockImplementation(() => timeFirstTxIsExpired * 1000);

const result = await runWeb3Function({
userArgs: { ...userArgs, gasAllowance: 10 }, // not enough for making a single call
provider,
});
expect(result).toEqual({
canExec: false,
message:
"Gas allowance balance of 10 insufficient for executing next transaction from queue",
});

expect(sponsoredCallSpy).toHaveBeenCalledTimes(0);
});
});

// from query to goerli subgraph: delayModifier(id: "0x0b7a9a6f1c4e739df11f55c6879d48c9851a2162")
Expand Down

0 comments on commit 55f3426

Please sign in to comment.