-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
102 lines (87 loc) · 3.34 KB
/
utils.ts
File metadata and controls
102 lines (87 loc) · 3.34 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/dist/src/signer-with-address";
import {
BigNumber,
BigNumberish,
Contract,
ContractReceipt,
ContractTransaction,
utils,
} from "ethers";
import { Result } from "ethers/lib/utils";
import { ethers, upgrades } from "hardhat";
import accounts from "./test-accounts";
// Gets the private key of a given test account address
export const getPrivateKey = (address: string): Buffer =>
Buffer.from(
accounts
.find(({ privateKey }) => new ethers.Wallet(privateKey).address === address)!
.privateKey.slice(2),
"hex"
);
// ABI-encode given parameters
export const encodeParameters = (types: string[], values: any[]) => {
const abi = new ethers.utils.AbiCoder();
return abi.encode(types, values);
};
// Deploys a given contract from an address
export const deployContract = async <T extends Contract>(
name: string,
deployer: SignerWithAddress,
...args: any[]
): Promise<T> => {
const contractFactory = await ethers.getContractFactory(name, deployer);
const contractInstance = await contractFactory.deploy(...args);
return (await contractInstance.deployed()) as T;
};
export const deployContractUpgradeable = async <T extends Contract>(
name: string,
deployer: SignerWithAddress,
...args: any[]
): Promise<T> => {
const contractFactory = await ethers.getContractFactory(name, deployer);
const contractInstance = await upgrades.deployProxy(contractFactory, [...args]);
return (await contractInstance.deployed()) as T;
};
// Retrieves an event's parameters
export const getEvents = async (
txReceipt: ContractReceipt,
eventSignature: string,
originatingContractName: string
): Promise<Result[]> => {
const contractAbi = (await ethers.getContractFactory(originatingContractName)).interface;
let parsedLogs: utils.LogDescription[] = [];
txReceipt.logs.forEach((log) => {
try {
parsedLogs.push(contractAbi.parseLog(log));
} catch {
// Ignore any errors, the log entry might just belong to
// a different contract than the one we have the ABI of
}
});
// Return all matching events
return parsedLogs.filter(({ signature }) => signature === eventSignature).map((log) => log.args);
};
export const getTxTimestamp = async (tx: ContractTransaction): Promise<BigNumber> =>
bn(await ethers.provider.getBlock(tx.blockNumber as number).then((block) => block.timestamp));
// Mine a block at a given timestamp
export const mineBlock = async (timestamp?: BigNumberish) =>
timestamp
? await ethers.provider.send("evm_mine", [bn(timestamp).toNumber()])
: await ethers.provider.send("evm_mine", []);
// Mine a given number of blocks
export const mineBlocks = async (numBlocks: number) => {
for (let i = 0; i < numBlocks; i++) {
await mineBlock();
}
};
// Retrieves the current timestamp on the blockchain
export const now = async (): Promise<BigNumber> => {
const latestBlock = await ethers.provider.getBlock("latest");
return bn(latestBlock.timestamp);
};
// Pads a given number with 18 zeros
export const expandTo18Decimals = (n: BigNumberish): BigNumber => bn(n).mul(bn(10).pow(18));
// Simple wrapper for converting to BigNumber
export const bn = (n: BigNumberish): BigNumber => BigNumber.from(n);
// Converts a given number of days to seconds
export const daysToTimestamp = (days: BigNumberish): BigNumber => bn(days).mul(24).mul(3600);