This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
wrapperHelpers.ts
81 lines (71 loc) 路 2.73 KB
/
wrapperHelpers.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
import { JsonRpcSigner } from '@ethersproject/providers';
import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { MockProvider } from 'ethereum-waffle';
import { Signer } from 'ethers';
import { MockConnector } from '~~/helpers/test-utils/wrapper/MockConnector';
export const isActive = async (connector: MockConnector): Promise<boolean> => {
const active =
connector != null &&
(await connector.getChainId()) != null &&
(await connector.getProvider()) != null &&
(await connector.getAccount()) != null;
return active;
};
export const waitForActivation = async (callback: () => Promise<boolean>): Promise<void> => {
let timeout = false;
void setTimeout(() => {
timeout = true;
}, 2000);
while (!(await callback()) && !timeout) {
// sleep for 100ms
await (async (): Promise<void> => await new Promise((resolve) => setTimeout(resolve, 100)))();
}
};
export type THardhatAccountsNames = 'deployer' | 'user1' | 'user2' | 'user3' | 'user4' | 'user5' | 'governance';
export type THardhatAccounts = Record<THardhatAccountsNames, SignerWithAddress>;
export const getTestAccounts = async (
provider: MockProvider
): Promise<{ [names in THardhatAccountsNames]: string }> => {
const accounts = await provider.listAccounts();
return {
deployer: accounts[0],
user1: accounts[1],
user2: accounts[2],
user3: accounts[3],
user4: accounts[4],
user5: accounts[5],
governance: accounts[10],
};
};
export const getTestSigners = async (
provider: MockProvider
): Promise<{ [names in THardhatAccountsNames]: JsonRpcSigner }> => {
const accounts = await provider.listAccounts();
return {
deployer: provider.getSigner(accounts[0]),
user1: provider.getSigner(accounts[1]),
user2: provider.getSigner(accounts[2]),
user3: provider.getSigner(accounts[3]),
user4: provider.getSigner(accounts[4]),
user5: provider.getSigner(accounts[5]),
governance: provider.getSigner(accounts[10]),
};
};
export const getHardhatAccount = async (provider: MockProvider, hardhatAccountIndex: number): Promise<string> => {
const accounts = await provider.listAccounts();
if (accounts?.[hardhatAccountIndex] == null) {
const error = new Error('MockConnector: unknown mock hardhat account');
console.error(error);
throw error;
}
return accounts[hardhatAccountIndex];
};
export const getHardhatSigner = async (provider: MockProvider, hardhatAccountIndex: number): Promise<Signer> => {
const accounts = await provider.listAccounts();
if (accounts?.[hardhatAccountIndex] == null) {
const error = new Error('MockConnector: unknown mock hardhat account');
console.error(error);
throw error;
}
return provider.getSigner(accounts[hardhatAccountIndex]);
};