-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathutils.ts
111 lines (98 loc) · 3.27 KB
/
utils.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
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
103
104
105
106
107
108
109
110
111
import { toBufferBE } from 'bigint-buffer';
import { ContractMethodArgs, Typed } from 'ethers';
import { ethers, network } from 'hardhat';
import type { Counter } from '../types';
import { TypedContractMethod } from '../types/common';
import { getSigners } from './signers';
export const waitForBlock = (blockNumber: bigint | number) => {
if (network.name === 'hardhat') {
return new Promise((resolve, reject) => {
const intervalId = setInterval(async () => {
try {
const currentBlock = await ethers.provider.getBlockNumber();
if (BigInt(currentBlock) >= blockNumber) {
clearInterval(intervalId);
resolve(currentBlock);
}
} catch (error) {
clearInterval(intervalId);
reject(error);
}
}, 50); // Check every 50 milliseconds
});
} else {
return new Promise((resolve, reject) => {
const waitBlock = async (currentBlock: number) => {
if (blockNumber <= BigInt(currentBlock)) {
await ethers.provider.off('block', waitBlock);
resolve(blockNumber);
}
};
ethers.provider.on('block', waitBlock).catch((err) => {
reject(err);
});
});
}
};
export const waitNBlocks = async (Nblocks: number) => {
const currentBlock = await ethers.provider.getBlockNumber();
if (network.name === 'hardhat') {
await produceDummyTransactions(Nblocks);
}
await waitForBlock(currentBlock + Nblocks);
};
export const waitForBalance = async (address: string): Promise<void> => {
return new Promise((resolve, reject) => {
const checkBalance = async () => {
const balance = await ethers.provider.getBalance(address);
if (balance > 0) {
await ethers.provider.off('block', checkBalance);
resolve();
}
};
ethers.provider.on('block', checkBalance).catch((err) => {
reject(err);
});
});
};
export const createTransaction = async <A extends [...{ [I in keyof A]-?: A[I] | Typed }]>(
method: TypedContractMethod<A>,
...params: A
) => {
const gasLimit = await method.estimateGas(...params);
const updatedParams: ContractMethodArgs<A> = [
...params,
{ gasLimit: Math.min(Math.round(+gasLimit.toString() * 1.2), 10000000) },
];
return method(...updatedParams);
};
export const produceDummyTransactions = async (blockCount: number) => {
const contract = await deployCounterContract();
let counter = blockCount;
while (counter > 0) {
counter--;
const tx = await contract.increment();
const _ = await tx.wait();
}
};
async function deployCounterContract(): Promise<Counter> {
const signers = await getSigners();
const contractFactory = await ethers.getContractFactory('Counter');
const contract = await contractFactory.connect(signers.dave).deploy();
await contract.waitForDeployment();
return contract;
}
export const mineNBlocks = async (n: number) => {
for (let index = 0; index < n; index++) {
await ethers.provider.send('evm_mine');
}
};
export const bigIntToBytes64 = (value: bigint) => {
return new Uint8Array(toBufferBE(value, 64));
};
export const bigIntToBytes128 = (value: bigint) => {
return new Uint8Array(toBufferBE(value, 128));
};
export const bigIntToBytes256 = (value: bigint) => {
return new Uint8Array(toBufferBE(value, 256));
};