Skip to content

Commit

Permalink
deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Oct 18, 2023
1 parent 05e7ecb commit 649f58c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 17 deletions.
22 changes: 21 additions & 1 deletion packages/gelato-web3-function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

This Gelato web3 function can be started for a Delay mod to automatically execute enqueued transactions as soon as the cooldown period has passed.

The latest version is deployed at IPFS CID: `QmYpE74tFz59XLNLu9MSHJejsPHryZsUMcuDjBzfgz5WQk`

## How to run

https://docs.gelato.network/developer-services/web3-functions/running-web3-functions
### With Gelato Automate SDK

```ts
import { AutomateSDK } from "@gelatonetwork/automate-sdk";

const automate = new AutomateSDK(chainId, wallet);

const { taskId, tx } = await automate.createBatchExecTask({
name: "Web3Function - Delay dispatch",
web3FunctionHash: "QmYpE74tFz59XLNLu9MSHJejsPHryZsUMcuDjBzfgz5WQk",
web3FunctionArgs: {
delayMod: "0x...", // address of the Delay mod to watch
gasAllowance: 1_000_000, // total gas that can be spent per interval
allowanceInterval: 7150, // unit is blocks (7150 blocks is roughly one day on mainnet)
},
});
```

More info: https://docs.gelato.network/developer-services/web3-functions/running-web3-functions
28 changes: 16 additions & 12 deletions packages/gelato-web3-function/delay-dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ Web3Function.onRun(async (context) => {
throw new Error("Missing RELAY_API_KEY secret");
}

const provider = multiChainProvider.default(); // this will be a provider for the chain this function is deployed for (gelatoArgs.chainId)

// parse user args
const delayModAddress = userArgs.delayMod as string;
if (!delayModAddress) {
throw new Error("Missing delayMod userArg");
}
const gasAllowanceString = userArgs.gasAllowance as string;
let allowanceInterval = userArgs.allowanceInterval as number;
let gasAllowance: BigNumber;
try {
gasAllowance = BigNumber.from(gasAllowanceString);
} catch (err) {
throw new Error(`Invalid gasAllowance userArg: ${gasAllowanceString}`);
}
if (typeof allowanceInterval !== "number" || allowanceInterval < 0) {
throw new Error(`Invalid allowanceInterval userArg: ${allowanceInterval}`);
}

const provider = multiChainProvider.default(); // this will be a provider for the chain this function is deployed for (gelatoArgs.chainId)

if ((await provider.getCode(delayModAddress)) === "0x") {
return {
Expand Down Expand Up @@ -66,16 +78,8 @@ Web3Function.onRun(async (context) => {
);

// Retrieve gas allowance balance and top up with accrued if needed
const gasAllowanceString = userArgs.gasAllowance as string;
const allowanceRefillInterval = userArgs.allowanceRefillInterval as number;
let gasAllowance: BigNumber;
try {
gasAllowance = BigNumber.from(gasAllowanceString);
} catch (err) {
throw new Error(`Invalid gasAllowance userArg: ${gasAllowanceString}`);
}
const accrued = allowanceRefillInterval
? gasAllowance.mul(currentBlock - lastBlock).div(allowanceRefillInterval)
const accrued = allowanceInterval
? gasAllowance.mul(currentBlock - lastBlock).div(allowanceInterval)
: BigNumber.from(0);
const previousGasBalance = BigNumber.from(
(await storage.get("gasBalance")) || gasAllowance
Expand Down
2 changes: 1 addition & 1 deletion packages/gelato-web3-function/delay-dispatch/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"userArgs": {
"delayMod": "string",
"gasAllowance": "string",
"allowanceRefillInterval": "number"
"allowanceInterval": "number"
}
}
20 changes: 20 additions & 0 deletions packages/gelato-web3-function/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from "path";
import { Web3FunctionBuilder } from "@gelatonetwork/web3-functions-sdk/builder";

const main = async () => {
// Deploy Web3Function on IPFS
console.log("Deploying Web3Function on IPFS...");

const web3FunctionPath = path.join("delay-dispatch", "index.ts");
const cid = await Web3FunctionBuilder.deploy(web3FunctionPath);
console.log(`Web3Function IPFS CID: ${cid}`);
};

main()
.then(() => {
process.exit();
})
.catch((err) => {
console.error("Error:", err.message);
process.exit(1);
});
5 changes: 3 additions & 2 deletions packages/gelato-web3-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "[email protected]",
"license": "LGPL-3.0+",
"scripts": {
"build": "rm -rf build && tsc",
"deploy": "ts-node deploy.ts",
"ts:check": "tsc",
"format": "prettier --write '*/**/*.{js,json,md,ts}'",
"format:check": "prettier --check '*/**/*.{js,json,md,ts}'",
"test": "jest"
Expand Down Expand Up @@ -34,4 +35,4 @@
"installConfig": {
"hoistingLimits": "workspaces"
}
}
}
1 change: 1 addition & 0 deletions packages/gelato-web3-function/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("delay-dispatch web3 function", () => {
userArgs: {
delayMod: "0x0b7a9a6f1c4e739df11f55c6879d48c9851a2162",
gasAllowance: 10_000_000,
allowanceInterval: 7150,
},
};
// const relay = new GelatoRelay();
Expand Down
3 changes: 2 additions & 1 deletion packages/gelato-web3-function/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"allowSyntheticDefaultImports": true,
"downlevelIteration": true,
"skipLibCheck": true,
"noEmit": true,
"moduleResolution": "node",
"pretty": true,
"resolveJsonModule": true,
Expand All @@ -14,6 +15,6 @@
"declaration": true,
"useUnknownInCatchVariables": false
},
"include": ["index.ts", "jest.config.ts", "test"],
"include": ["delay-dispatch", "jest.config.ts", "test", "deploy"],
"exclude": ["node_modules"]
}

0 comments on commit 649f58c

Please sign in to comment.