-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeploy-merkle-funder-depositories.ts
83 lines (70 loc) · 2.9 KB
/
deploy-merkle-funder-depositories.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
import { goSync } from '@api3/promise-utils';
import * as hre from 'hardhat';
import * as readline from 'readline';
import { loadConfig } from '../src/config';
import { computeMerkleFunderDepositoryAddress } from '../src/evm';
import buildMerkleTree from '../src/merkle-tree';
async function main() {
const chainName = hre.network.name;
console.log('Chain name:', chainName);
const chainId = parseInt(await hre.getChainId());
console.log('Chain ID:', chainId);
const merkleFunderDeployment = await hre.deployments.get('MerkleFunder');
console.log('MerkleFunder address:', merkleFunderDeployment.address);
const deployerAddress = (await hre.getUnnamedAccounts())[0];
console.log('Deployer address:', deployerAddress);
const deployer = await hre.ethers.getSigner(deployerAddress);
const merkleFunder = new hre.ethers.Contract(merkleFunderDeployment.address, merkleFunderDeployment.abi, deployer);
const loadConfigResult = goSync(() => loadConfig());
if (!loadConfigResult.success) {
console.log('Failed to load config:\n', loadConfigResult.error.message);
return;
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const chainMerkleFunderDepositories = loadConfigResult.data[chainId].merkleFunderDepositories;
if (!chainMerkleFunderDepositories) {
console.log('No merkleFunderDepositories for chain: ', chainName, chainId);
return;
}
for (const { owner, values } of chainMerkleFunderDepositories) {
// Build merkle tree
const tree = buildMerkleTree(values);
console.log('Merkle tree:\n', tree.render());
// Compute MerkleFunderFepository address and check if it is already deployed
const merkleFunderDepositoryAddress = await computeMerkleFunderDepositoryAddress(
merkleFunder.address,
owner,
tree.root
);
if ((await hre.ethers.provider.getCode(merkleFunderDepositoryAddress)) === '0x') {
const answer = await new Promise<string>((resolve) =>
rl.question(
'Your config specifies a MerkleFunderDepository that is not deployed yet. Continue? (y/n) [y]: ',
(input) => {
resolve(input.trim().toLowerCase() || 'y');
}
)
);
if (answer === 'y') {
const tx = await merkleFunder.deployMerkleFunderDepository(owner, tree.root);
console.log('MerkleFunderDepository is deployed at', merkleFunderDepositoryAddress);
const receipt = await tx.wait();
console.log('Transaction hash:', receipt.transactionHash);
console.log('Gas used:', hre.ethers.utils.formatUnits(receipt.gasUsed, 'gwei'));
} else {
console.log('Deployment aborted by the user');
}
} else {
console.log('MerkleFunderDepository is already deployed at', merkleFunderDepositoryAddress);
}
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});