-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathvalidate-contract-deployments.test.ts
More file actions
81 lines (63 loc) · 2.28 KB
/
Copy pathvalidate-contract-deployments.test.ts
File metadata and controls
81 lines (63 loc) · 2.28 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
import { createPublicClient, http } from 'viem';
import * as chains from 'viem/chains';
import type { Chain } from 'viem/chains';
import { DELEGATOR_CONTRACTS } from '../src/index';
import { compareVersions } from 'compare-versions';
/*
This test validates that the DeleGator contracts are deployed on the specified chains,
as specified in the @metamask-private/delegation-deployments package.
It does this by getting the DeleGatorEnvironment for each chain and then ensuring that
code is found at the expected address for each contract.
*/
// The default rpc urls for these chains are not reliable, so we override them
// This may be a game of cat and mouse, so a better solution may be needed.
const rpcUrlOverrides = {
[chains.mainnet.id]: 'https://eth.merkle.io',
[chains.bsc.id]: 'https://bsc-dataseed1.binance.org/',
};
const latestVersion = Object.keys(DELEGATOR_CONTRACTS).reduce(
(acc, version) => {
if (compareVersions(version, acc) === 1) {
return version;
}
return acc;
},
'0.0.0',
);
const latestContracts = DELEGATOR_CONTRACTS[latestVersion];
const chainIds = Object.keys(latestContracts);
let hasFailed = false;
const allChains = chainIds.map(async (chainIdAsString) => {
const chainId = parseInt(chainIdAsString);
const contracts = latestContracts[chainIdAsString];
const transport = http(rpcUrlOverrides[chainId]);
const chain = (chains as any as Chain[]).find((c) => c.id === chainId);
if (!chain) {
throw new Error(`Chain ${chainId} not found`);
}
const publicClient = createPublicClient({
chain,
transport,
});
const contractNames = Object.keys(contracts);
const allContracts = contractNames.map(async (contractName) => {
const contractAddress = contracts[contractName];
const code = await publicClient.getCode({ address: contractAddress });
if (code === '0x') {
console.error(
`${chain.name}: ${contractName} is not deployed at ${contractAddress}`,
);
hasFailed = true;
}
});
await Promise.all(allContracts);
console.log(`${chain.name} succeeded`);
});
Promise.all(allChains).then(() => {
if (hasFailed) {
process.exitCode = 1;
console.error('Failed to validate contract deployments');
} else {
console.log('Successfully validated contract deployments');
}
});