-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(reward-distributor): Add simple sucessful test
- Loading branch information
Showing
6 changed files
with
155 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | ||
import path from "node:path"; | ||
import { JsonRpcProvider } from "@ethersproject/providers"; | ||
import { checkAnvil, getAnvilCommand, run } from "@foundry-rs/easy-foundryup"; | ||
import { Web3FunctionResultV2 } from "@gelatonetwork/web3-functions-sdk"; | ||
import { type Anvil, createAnvil } from "@viem/anvil"; | ||
import { runWeb3Function } from "./utils"; | ||
|
||
const w3fName = "reward-distributor"; | ||
const w3fRootDir = path.join("web3-functions"); | ||
const w3fPath = path.join(w3fRootDir, w3fName, "index.ts"); | ||
|
||
describe("Reward Distributor Web3 Function test", () => { | ||
let anvil: Anvil; | ||
beforeAll(async () => { | ||
if (!(await checkAnvil())) { | ||
await run(); | ||
} | ||
anvil = createAnvil({ | ||
// All anvil options are supported & typed. | ||
forkUrl: | ||
"https://rpc.ankr.com/arbitrum/665a6078efb46adbd3eb6b831180fab7d7007c48590f2517e2bce900ba31e8ca", | ||
forkBlockNumber: 234134609n, | ||
anvilBinary: await getAnvilCommand(), | ||
}); | ||
await anvil.start(); | ||
}); | ||
afterAll(async () => { | ||
await anvil.stop(); | ||
}); | ||
test("canExec: true - Multiple calls ", async () => { | ||
const provider = new JsonRpcProvider(`http://${anvil.host}:${anvil.port}`); | ||
const { result } = await runWeb3Function( | ||
w3fPath, | ||
{ | ||
gelatoArgs: { | ||
chainId: 42161, | ||
gasPrice: (await provider.getGasPrice()).toString(), | ||
}, | ||
userArgs: { | ||
multiRewardDistributorAddress: | ||
"0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
multiRewardStakingAddresses: [ | ||
"0x280c64c4C4869CF2A6762EaDD4701360C1B11F97", | ||
"0xc30911b52b5752447aB08615973e434c801CD652", | ||
], | ||
epochBasedDistributorAddress: | ||
"0x111AbF466654c166Ee4AC15d6A29a3e0625533db", | ||
epochBasedStakingAddresses: [], | ||
}, | ||
secrets: {}, | ||
storage: {}, | ||
}, | ||
[provider], | ||
); | ||
expect(result).toEqual({ | ||
canExec: true, | ||
callData: [ | ||
{ | ||
data: "0x63453ae1000000000000000000000000280c64c4c4869cf2a6762eadd4701360c1b11f97", | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
}, | ||
{ | ||
data: "0x63453ae1000000000000000000000000c30911b52b5752447ab08615973e434c801cd652", | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { JsonRpcProvider } from "@ethersproject/providers"; | ||
import type { | ||
MultiChainProviderConfig, | ||
Web3FunctionContextData, | ||
Web3FunctionRunnerOptions, | ||
} from "@gelatonetwork/web3-functions-sdk"; | ||
import { Web3FunctionBuilder } from "@gelatonetwork/web3-functions-sdk/builder"; | ||
import { Web3FunctionRunner } from "@gelatonetwork/web3-functions-sdk/runtime"; | ||
|
||
export const MAX_RPC_LIMIT = 100; | ||
export const MAX_DOWNLOAD_LIMIT = 10 * 1024 * 1024; | ||
export const MAX_UPLOAD_LIMIT = 5 * 1024 * 1024; | ||
export const MAX_REQUEST_LIMIT = 100; | ||
export const MAX_STORAGE_LIMIT = 1 * 1024 * 1024; | ||
|
||
export const runWeb3Function = async ( | ||
web3FunctionPath: string, | ||
context: Web3FunctionContextData<"onRun">, | ||
providers: JsonRpcProvider[], | ||
) => { | ||
const buildRes = await Web3FunctionBuilder.build(web3FunctionPath, { | ||
debug: false, | ||
}); | ||
|
||
if (!buildRes.success) | ||
throw new Error(`Fail to build web3Function: ${buildRes.error}`); | ||
|
||
const runner = new Web3FunctionRunner(false); | ||
const runtime: "docker" | "thread" = "thread"; | ||
const memory = buildRes.schema.memory; | ||
const rpcLimit = MAX_RPC_LIMIT; | ||
const timeout = buildRes.schema.timeout * 1000; | ||
const version = buildRes.schema.web3FunctionVersion; | ||
|
||
const options: Web3FunctionRunnerOptions = { | ||
runtime, | ||
showLogs: true, | ||
memory, | ||
downloadLimit: MAX_DOWNLOAD_LIMIT, | ||
uploadLimit: MAX_UPLOAD_LIMIT, | ||
requestLimit: MAX_REQUEST_LIMIT, | ||
rpcLimit, | ||
timeout, | ||
storageLimit: MAX_STORAGE_LIMIT, | ||
}; | ||
const script = buildRes.filePath; | ||
|
||
const multiChainProviderConfig: MultiChainProviderConfig = {}; | ||
|
||
for (const provider of providers) { | ||
const chainId = (await provider.getNetwork()).chainId; | ||
|
||
multiChainProviderConfig[chainId] = provider; | ||
} | ||
|
||
const res = await runner.run("onRun", { | ||
script, | ||
context, | ||
options, | ||
version, | ||
multiChainProviderConfig, | ||
}); | ||
|
||
if (!res.success) | ||
throw new Error(`Fail to run web3 function: ${res.error.message}`); | ||
|
||
return res; | ||
}; |