-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
56 lines (51 loc) · 1.68 KB
/
utils.js
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
import * as ethers from "ethers";
const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");
let snapshotId, signer;
export default {
setup: async (chainId, impersonateAddress) => {
const { chainId: forkedChainId } = await provider.getNetwork();
if (parseInt(forkedChainId) !== parseInt(chainId))
throw `Forked wrong network! Expected ${chainId} got ${forkedChainId}`;
signer = provider.getSigner(impersonateAddress);
snapshotId = await provider.send("evm_snapshot", []);
if (impersonateAddress) {
await provider.send("anvil_impersonateAccount", [impersonateAddress]);
await provider.send("anvil_setBalance", [
impersonateAddress,
ethers.constants.MaxUint256.toHexString(),
]);
}
return signer;
},
teardown: async () => {
try {
await provider.send("evm_revert", [snapshotId]);
} finally {
return;
}
},
getTokenBalance: async (tokenAddress, addressToGetBalance) => {
const erc20 = new ethers.Contract(
tokenAddress,
["function balanceOf(address) view returns (uint256)"],
provider
);
return erc20.balanceOf(addressToGetBalance);
},
transferToken: async (tokenAddress, receiverAddress, amount) => {
const erc20 = new ethers.Contract(
tokenAddress,
["function transfer(address,uint256)"],
signer
);
return (await erc20.transfer(receiverAddress, amount)).wait();
},
approveToken: async (tokenAddress, receiverAddress, amount) => {
const erc20 = new ethers.Contract(
tokenAddress,
["function approve(address,uint256)"],
signer
);
return (await erc20.approve(receiverAddress, amount)).wait();
},
};