-
Notifications
You must be signed in to change notification settings - Fork 0
/
advertising-board.test.ts
73 lines (53 loc) · 2.1 KB
/
advertising-board.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import path from "path";
import { Web3FunctionContextData } from "@gelatonetwork/web3-functions-sdk";
import { Web3FunctionLoader } from "@gelatonetwork/web3-functions-sdk/loader";
import { runWeb3Function } from "./utils";
import { AnvilServer } from "./utils/anvil-server";
const w3fName = "advertising-board";
const w3fRootDir = path.join("web3-functions");
const w3fPath = path.join(w3fRootDir, w3fName, "index.ts");
describe("Advertising Board Web3 Function test", () => {
let context: Web3FunctionContextData;
let goerliFork: AnvilServer;
beforeAll(async () => {
goerliFork = await AnvilServer.fork({
forkBlockNumber: 8483100,
forkUrl: "https://rpc.ankr.com/eth_goerli",
});
const { secrets } = Web3FunctionLoader.load(w3fName, w3fRootDir);
const gasPrice = (await goerliFork.provider.getGasPrice()).toString();
context = {
secrets,
storage: {},
gelatoArgs: {
chainId: 5,
gasPrice,
},
userArgs: {},
};
}, 10000);
afterAll(async () => {
goerliFork.kill();
});
it("canExec: false - Time not elapsed", async () => {
const blockTime = (await goerliFork.provider.getBlock("latest")).timestamp;
// mock storage state of "lastPost"
context.storage = { lastPost: blockTime.toString() };
const res = await runWeb3Function(w3fPath, context, [goerliFork.provider]);
expect(res.result.canExec).toEqual(false);
});
it("canExec: True - Time elapsed", async () => {
const blockTimeBefore = (await goerliFork.provider.getBlock("latest"))
.timestamp;
const nextPostTime = blockTimeBefore + 3600;
// fast forward block time
await goerliFork.provider.send("evm_mine", [nextPostTime]);
// pass current block time
const blockTime = (await goerliFork.provider.getBlock("latest")).timestamp;
const res = await runWeb3Function(w3fPath, context, [goerliFork.provider]);
expect(res.result.canExec).toEqual(true);
// expect "lastPost" to be updated
expect(res.storage.state).toEqual("updated");
expect(res.storage.storage["lastPost"]).toEqual(blockTime.toString());
});
});