This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdeploy.ts
More file actions
122 lines (111 loc) · 4.36 KB
/
deploy.ts
File metadata and controls
122 lines (111 loc) · 4.36 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { BigNumber } from "@ethersproject/bignumber";
import { EtherSymbol, One, Zero } from "@ethersproject/constants";
import { formatEther, parseEther } from "@ethersproject/units";
import { deployments, ethers, getNamedAccounts, getChainId, network } from "hardhat";
import { DeployFunction } from "hardhat-deploy/types";
import { registerTransfer } from "../src.ts/utils";
import { logger } from "../src.ts/constants";
const func: DeployFunction = async () => {
const log = logger.child({ module: "Deploy" });
const chainId = await getChainId();
const provider = ethers.provider;
const { deployer } = await getNamedAccounts();
// Log initial state
const balance = await provider.getBalance(deployer);
const nonce = await provider.getTransactionCount(deployer);
log.info(`Preparing to migrate contracts to chain ${chainId}`);
log.info(`Deployer address=${deployer} nonce=${nonce} balance=${formatEther(balance)}`);
if (balance.eq(0)) {
throw new Error(`Account ${deployer} has zero balance on chain ${chainId}, aborting migration`);
}
////////////////////////////////////////
// Run the migration
type Args = Array<string | BigNumber>;
const migrate = async (name: string, args: Args): Promise<void> => {
const processedArgs = await Promise.all(
args.map(
async (arg: any): Promise<any> => {
try {
return (await deployments.get(arg)).address;
} catch (e) {
return arg;
}
},
),
);
log.info(`Deploying ${name} with args [${processedArgs.join(", ")}]`);
await deployments.deploy(name, {
from: deployer,
args: processedArgs,
/*
gasLimit: deployTx.gasLimit && BigNumber.from(deployTx.gasLimit).lt(MIN_GAS_LIMIT)
? MIN_GAS_LIMIT
: undefined,
*/
});
const deployment = await deployments.get(name);
if (!deployment.transactionHash) {
throw new Error(`Failed to deploy ${name}`);
}
const tx = await ethers.provider.getTransaction(deployment.transactionHash!);
const receipt = await ethers.provider.getTransactionReceipt(deployment.transactionHash!);
log.info(`Sent transaction to deploy ${name}, txHash: ${deployment.transactionHash}`);
log.info(
`Success! Consumed ${receipt?.gasUsed ?? "unknown"} gas worth ${EtherSymbol} ${formatEther(
(receipt?.gasUsed ?? Zero).mul(tx.gasPrice ?? One),
)} deploying ${name} to address: ${deployment.address}`,
);
};
const standardMigration = [
["ChannelMastercopy", []],
["ChannelFactory", ["ChannelMastercopy", Zero]],
["HashlockTransfer", []],
["Withdraw", []],
["TransferRegistry", []],
["TestToken", []],
];
// Only deploy test fixtures during hardhat tests
if (network.name === "hardhat") {
log.info(`Running localnet migration`);
for (const row of [
...standardMigration,
["TestChannel", []],
["TestChannelFactory", ["TestChannel", Zero]],
["FailingToken", []],
["NonconformingToken", []],
["TestLibIterableMapping", []],
["CMCAsset", []],
]) {
const name = row[0] as string;
const args = row[1] as Array<string | BigNumber>;
await migrate(name, args);
}
await registerTransfer("Withdraw", deployer);
await registerTransfer("HashlockTransfer", deployer);
// Default: run standard migration
} else {
log.info(`Running testnet migration`);
for (const row of standardMigration) {
const name = row[0] as string;
const args = row[1] as Array<string | BigNumber>;
await migrate(name, args);
}
await registerTransfer("Withdraw", deployer);
await registerTransfer("HashlockTransfer", deployer);
}
if ([1337, 5].includes(network.config.chainId ?? 0)) {
log.info(`Detected AMM deployment chain ${network.config.chainId}`);
const res = await deployments.deploy("StableSwap", {
from: deployer,
args: [parseEther("2500")],
});
log.info({ address: res.address, txHash: res.transactionHash }, `Deployed AMM ${network.config.chainId}`);
}
////////////////////////////////////////
// Print summary
log.info("All done!");
const spent = formatEther(balance.sub(await provider.getBalance(deployer)));
const nTx = (await provider.getTransactionCount(deployer)) - nonce;
log.info(`Sent ${nTx} transaction${nTx === 1 ? "" : "s"} & spent ${EtherSymbol} ${spent}`);
};
export default func;