-
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
8 changed files
with
305 additions
and
15 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,169 @@ | ||
import { | ||
afterAll, | ||
afterEach, | ||
beforeAll, | ||
describe, | ||
expect, | ||
test, | ||
} from "bun:test"; | ||
import path from "node:path"; | ||
import type { JsonRpcProvider } from "@ethersproject/providers"; | ||
import type { Anvil } from "@viem/anvil"; | ||
import type { Merge } from "type-fest"; | ||
import { | ||
type Address, | ||
type Hex, | ||
type PublicClient, | ||
type TestClient, | ||
encodeFunctionData, | ||
parseAbi, | ||
} from "viem"; | ||
import { ARBITRUM_OPS_SAFE, ARBITRUM_SPELL } from "../utils/constants"; | ||
import { runWeb3Function } from "./utils"; | ||
import { setupAnvil } from "./utils/setupAnvil"; | ||
|
||
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; | ||
let provider: JsonRpcProvider; | ||
let testClient: Omit<Merge<PublicClient, TestClient>, "mode">; | ||
let snapshotId: Hex; | ||
|
||
beforeAll(async () => { | ||
({ anvil, provider, testClient, snapshotId } = await setupAnvil({ | ||
forkUrl: | ||
"https://rpc.ankr.com/arbitrum/665a6078efb46adbd3eb6b831180fab7d7007c48590f2517e2bce900ba31e8ca", | ||
forkBlockNumber: 234134609n, | ||
})); | ||
}); | ||
|
||
afterAll(async () => { | ||
await anvil.stop(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await testClient.revert({ id: snapshotId }); | ||
}); | ||
|
||
const run = async () => | ||
runWeb3Function( | ||
w3fPath, | ||
{ | ||
gelatoArgs: { | ||
chainId: (await provider.getNetwork()).chainId, | ||
gasPrice: (await provider.getGasPrice()).toString(), | ||
}, | ||
userArgs: { | ||
multiRewardDistributorAddress: | ||
"0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
multiRewardStakingAddresses: [ | ||
"0x280c64c4C4869CF2A6762EaDD4701360C1B11F97", | ||
"0xc30911b52b5752447aB08615973e434c801CD652", | ||
], | ||
epochBasedDistributorAddress: | ||
"0x111AbF466654c166Ee4AC15d6A29a3e0625533db", | ||
epochBasedStakingAddresses: [], | ||
}, | ||
secrets: {}, | ||
storage: {}, | ||
}, | ||
[provider], | ||
); | ||
|
||
test( | ||
"canExec: true - Multiple distributions", | ||
async () => { | ||
const { result } = await run(); | ||
|
||
expect(result).toEqual({ | ||
canExec: true, | ||
callData: [ | ||
{ | ||
data: "0x63453ae1000000000000000000000000280c64c4c4869cf2a6762eadd4701360c1b11f97", | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
}, | ||
{ | ||
data: "0x63453ae1000000000000000000000000c30911b52b5752447ab08615973e434c801cd652", | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
}, | ||
], | ||
}); | ||
}, | ||
{ timeout: 60000 }, | ||
); | ||
test( | ||
"canExec: true - Single distributions", | ||
async () => { | ||
const execAddress = | ||
"0x280c64c4C4869CF2A6762EaDD4701360C1B11F97" as const satisfies Address; | ||
await testClient.sendUnsignedTransaction({ | ||
from: ARBITRUM_OPS_SAFE, | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
data: encodeFunctionData({ | ||
abi: parseAbi(["function distribute(address) external"]), | ||
functionName: "distribute", | ||
args: [execAddress], | ||
}), | ||
gasPrice: 0n, | ||
}); | ||
|
||
const { result } = await run(); | ||
|
||
expect(result).toEqual({ | ||
canExec: true, | ||
callData: [ | ||
{ | ||
data: "0x63453ae1000000000000000000000000c30911b52b5752447ab08615973e434c801cd652", | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
}, | ||
], | ||
}); | ||
}, | ||
{ timeout: 60000 }, | ||
); | ||
test( | ||
"canExec: false - No distributions to execute", | ||
async () => { | ||
// Increase approval | ||
await testClient.setStorageAt({ | ||
address: ARBITRUM_SPELL, | ||
index: | ||
"0xe291b9f68327d6549fd70e333daad56b6cdb38ac27f870dafc9c5d1dcd54d5a5", | ||
value: | ||
"0x0000000000000000000000000000000000000000002116545850052128000000", | ||
}); | ||
|
||
const execAddresses = [ | ||
"0x280c64c4C4869CF2A6762EaDD4701360C1B11F97", | ||
"0xc30911b52b5752447ab08615973e434c801cd652", | ||
] as const satisfies Array<Address>; | ||
|
||
await Promise.all( | ||
execAddresses.map( | ||
async (execAddress) => | ||
await testClient.sendUnsignedTransaction({ | ||
from: ARBITRUM_OPS_SAFE, | ||
to: "0xbF5DC3f598AFA173135160CDFce6BFeE45c912eF", | ||
data: encodeFunctionData({ | ||
abi: parseAbi(["function distribute(address) external"]), | ||
functionName: "distribute", | ||
args: [execAddress], | ||
}), | ||
gasPrice: 0n, | ||
}), | ||
), | ||
); | ||
|
||
const { result } = await run(); | ||
|
||
expect(result).toEqual({ | ||
canExec: false, | ||
message: "No distributions to execute", | ||
}); | ||
}, | ||
{ timeout: 60000 }, | ||
); | ||
}); |
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; | ||
}; |
Oops, something went wrong.