|
| 1 | +import { ethers } from 'hardhat'; |
| 2 | +import { BigNumber } from 'ethers'; |
| 3 | +const hre = require('hardhat'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const output = { |
| 8 | + walletImplementation: '', |
| 9 | + walletFactory: '', |
| 10 | + forwarderImplementation: '', |
| 11 | + forwarderFactory: '' |
| 12 | + }; |
| 13 | + |
| 14 | + const feeData = await ethers.provider.getFeeData(); |
| 15 | + |
| 16 | + |
| 17 | + const [walletDeployer, forwarderDeployer] = await ethers.getSigners(); |
| 18 | + |
| 19 | + const gasParams = { |
| 20 | + gasPrice: feeData.gasPrice!.mul('2'), |
| 21 | + gasLimit: 5000000 |
| 22 | + }; |
| 23 | + const walletTxCount = await walletDeployer.getTransactionCount(); |
| 24 | + |
| 25 | + console.log('Deploying wallet contracts....'); |
| 26 | + console.log('Wallet Tx Count: ', walletTxCount); |
| 27 | + const walletSelfTransactions = 2 - walletTxCount; |
| 28 | + for (let i = 0; i < walletSelfTransactions; i++) { |
| 29 | + const tx = await walletDeployer.sendTransaction({ |
| 30 | + to: walletDeployer.address, |
| 31 | + value: ethers.utils.parseEther('0'), |
| 32 | + gasPrice: gasParams.gasPrice |
| 33 | + }); |
| 34 | + await tx.wait(); |
| 35 | + console.log(`Self transaction with nonce: ${i} complete`); |
| 36 | + } |
| 37 | + |
| 38 | + const walletImplementationContractName = 'AvaxcWalletSimple'; |
| 39 | + const walletFactoryContractName = 'WalletFactory'; |
| 40 | + |
| 41 | + const WalletImplementation = await ethers.getContractFactory( |
| 42 | + walletImplementationContractName, |
| 43 | + walletDeployer |
| 44 | + ); |
| 45 | + const walletImplementation = await WalletImplementation.deploy(gasParams); |
| 46 | + await walletImplementation.deployed(); |
| 47 | + output.walletImplementation = walletImplementation.address; |
| 48 | + console.log( |
| 49 | + `${walletImplementationContractName} deployed at ` + |
| 50 | + walletImplementation.address |
| 51 | + ); |
| 52 | + |
| 53 | + const WalletFactory = await ethers.getContractFactory( |
| 54 | + walletFactoryContractName, |
| 55 | + walletDeployer |
| 56 | + ); |
| 57 | + const walletFactory = await WalletFactory.deploy( |
| 58 | + walletImplementation.address, |
| 59 | + gasParams |
| 60 | + ); |
| 61 | + await walletFactory.deployed(); |
| 62 | + output.walletFactory = walletFactory.address; |
| 63 | + console.log( |
| 64 | + `${walletFactoryContractName} deployed at ` + walletFactory.address |
| 65 | + ); |
| 66 | + |
| 67 | + const forwarderTxCount = await forwarderDeployer.getTransactionCount(); |
| 68 | + |
| 69 | + console.log('Deploying forwarder contracts....'); |
| 70 | + console.log('Forwarder Tx Count: ', forwarderTxCount); |
| 71 | + const forwarderSelfTransactions = 234 - forwarderTxCount; |
| 72 | + |
| 73 | + for (let i = 0; i < forwarderSelfTransactions; i++) { |
| 74 | + const tx = await forwarderDeployer.sendTransaction({ |
| 75 | + to: forwarderDeployer.address, |
| 76 | + value: ethers.utils.parseEther('0'), |
| 77 | + gasPrice: gasParams.gasPrice |
| 78 | + }); |
| 79 | + await tx.wait(); |
| 80 | + console.log(`Self transaction with nonce: ${i} complete`); |
| 81 | + } |
| 82 | + |
| 83 | + const forwarderImplementationContractName = 'Forwarder'; |
| 84 | + const forwarderFactoryContractName = 'ForwarderFactory'; |
| 85 | + |
| 86 | + const ForwarderImplementation = await ethers.getContractFactory( |
| 87 | + forwarderImplementationContractName, |
| 88 | + forwarderDeployer |
| 89 | + ); |
| 90 | + |
| 91 | + const forwarderImplementation = await ForwarderImplementation.deploy( |
| 92 | + gasParams |
| 93 | + ); |
| 94 | + await forwarderImplementation.deployed(); |
| 95 | + output.forwarderImplementation = forwarderImplementation.address; |
| 96 | + |
| 97 | + console.log( |
| 98 | + `${forwarderImplementationContractName} deployed at ` + |
| 99 | + forwarderImplementation.address |
| 100 | + ); |
| 101 | + |
| 102 | + const ForwarderFactory = await ethers.getContractFactory( |
| 103 | + forwarderFactoryContractName, |
| 104 | + forwarderDeployer |
| 105 | + ); |
| 106 | + |
| 107 | + const forwarderFactory = await ForwarderFactory.deploy( |
| 108 | + forwarderImplementation.address, |
| 109 | + gasParams |
| 110 | + ); |
| 111 | + |
| 112 | + await forwarderFactory.deployed(); |
| 113 | + output.forwarderFactory = forwarderFactory.address; |
| 114 | + console.log( |
| 115 | + `${forwarderFactoryContractName} deployed at ` + forwarderFactory.address |
| 116 | + ); |
| 117 | + |
| 118 | + fs.writeFileSync('output.json', JSON.stringify(output)); |
| 119 | + |
| 120 | + // Wait 5 minutes. It takes some time for the etherscan backend to index the transaction and store the contract. |
| 121 | + console.log('Waiting for 5 minutes before verifying....'); |
| 122 | + await new Promise((r) => setTimeout(r, 1000 * 300)); |
| 123 | + |
| 124 | + // We have to wait for a minimum of 10 block confirmations before we can call the etherscan api to verify |
| 125 | + |
| 126 | + await walletImplementation.deployTransaction.wait(10); |
| 127 | + await walletFactory.deployTransaction.wait(10); |
| 128 | + await forwarderImplementation.deployTransaction.wait(10); |
| 129 | + await forwarderFactory.deployTransaction.wait(10); |
| 130 | + |
| 131 | + console.log('Done waiting, verifying'); |
| 132 | + await verifyContract( |
| 133 | + walletImplementationContractName, |
| 134 | + walletImplementation.address, |
| 135 | + [] |
| 136 | + ); |
| 137 | + await verifyContract('WalletFactory', walletFactory.address, [ |
| 138 | + walletImplementation.address |
| 139 | + ]); |
| 140 | + |
| 141 | + await verifyContract( |
| 142 | + forwarderImplementationContractName, |
| 143 | + forwarderImplementation.address, |
| 144 | + [] |
| 145 | + ); |
| 146 | + |
| 147 | + await verifyContract('ForwarderFactory', forwarderFactory.address, [ |
| 148 | + forwarderImplementation.address |
| 149 | + ]); |
| 150 | + |
| 151 | + console.log('Contracts verified'); |
| 152 | +} |
| 153 | + |
| 154 | +async function verifyContract( |
| 155 | + contractName: string, |
| 156 | + contractAddress: string, |
| 157 | + constructorArguments: string[], |
| 158 | + contract?: string |
| 159 | +) { |
| 160 | + try { |
| 161 | + const verifyContractArgs: { |
| 162 | + address: string; |
| 163 | + constructorArguments: string[]; |
| 164 | + contract?: string; |
| 165 | + } = { |
| 166 | + address: contractAddress, |
| 167 | + constructorArguments: constructorArguments |
| 168 | + }; |
| 169 | + |
| 170 | + if (contract) { |
| 171 | + verifyContractArgs.contract = contract; |
| 172 | + } |
| 173 | + |
| 174 | + await hre.run('verify:verify', verifyContractArgs); |
| 175 | + } catch (e) { |
| 176 | + // @ts-ignore |
| 177 | + // We get a failure API response if the source code has already been uploaded, don't throw in this case. |
| 178 | + if (!e.message.includes('Reason: Already Verified')) { |
| 179 | + throw e; |
| 180 | + } |
| 181 | + } |
| 182 | + console.log(`Verified ${contractName} on Etherscan!`); |
| 183 | +} |
| 184 | + |
| 185 | +main().catch((error) => { |
| 186 | + console.error(error); |
| 187 | + process.exitCode = 1; |
| 188 | +}); |
0 commit comments