|
| 1 | +import { ethers } from 'hardhat'; |
| 2 | +const hre = require('hardhat'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const output = { |
| 7 | + walletImplementation: '', |
| 8 | + walletFactory: '' |
| 9 | + }; |
| 10 | + |
| 11 | + const feeData = await ethers.provider.getFeeData(); |
| 12 | + const gasParams = { |
| 13 | + gasPrice: feeData.gasPrice! |
| 14 | + }; |
| 15 | + const [deployer] = await ethers.getSigners(); |
| 16 | + const selfTransactions = 2; |
| 17 | + |
| 18 | + for (let i = 0; i < selfTransactions; i++) { |
| 19 | + const tx = await deployer.sendTransaction({ |
| 20 | + to: deployer.address, |
| 21 | + value: ethers.utils.parseEther('0'), |
| 22 | + gasPrice: gasParams.gasPrice |
| 23 | + }); |
| 24 | + await tx.wait(); |
| 25 | + console.log(`Self transaction with nonce: ${i} complete`); |
| 26 | + } |
| 27 | + |
| 28 | + const walletImplementationContractName = 'WalletSimple'; |
| 29 | + const walletFactoryContractName = 'WalletFactory'; |
| 30 | + |
| 31 | + const WalletImplementation = await ethers.getContractFactory( |
| 32 | + walletImplementationContractName |
| 33 | + ); |
| 34 | + const walletImplementation = await WalletImplementation.deploy(gasParams); |
| 35 | + await walletImplementation.deployed(); |
| 36 | + output.walletImplementation = walletImplementation.address; |
| 37 | + console.log( |
| 38 | + `${walletImplementationContractName} deployed at ` + |
| 39 | + walletImplementation.address |
| 40 | + ); |
| 41 | + |
| 42 | + const WalletFactory = await ethers.getContractFactory( |
| 43 | + walletFactoryContractName |
| 44 | + ); |
| 45 | + const walletFactory = await WalletFactory.deploy( |
| 46 | + walletImplementation.address, |
| 47 | + gasParams |
| 48 | + ); |
| 49 | + await walletFactory.deployed(); |
| 50 | + output.walletFactory = walletFactory.address; |
| 51 | + console.log( |
| 52 | + `${walletFactoryContractName} deployed at ` + walletFactory.address |
| 53 | + ); |
| 54 | + |
| 55 | + fs.writeFileSync('output.json', JSON.stringify(output)); |
| 56 | + |
| 57 | + // Wait 5 minutes. It takes some time for the etherscan backend to index the transaction and store the contract. |
| 58 | + console.log('Waiting for 5 minutes before verifying....'); |
| 59 | + await new Promise((r) => setTimeout(r, 1000 * 300)); |
| 60 | + |
| 61 | + // We have to wait for a minimum of 10 block confirmations before we can call the etherscan api to verify |
| 62 | + |
| 63 | + await walletImplementation.deployTransaction.wait(10); |
| 64 | + await walletFactory.deployTransaction.wait(10); |
| 65 | + |
| 66 | + console.log('Done waiting, verifying'); |
| 67 | + await verifyContract( |
| 68 | + walletImplementationContractName, |
| 69 | + walletImplementation.address, |
| 70 | + [] |
| 71 | + ); |
| 72 | + await verifyContract('WalletFactory', walletFactory.address, [ |
| 73 | + walletImplementation.address |
| 74 | + ]); |
| 75 | + |
| 76 | + console.log('Contracts verified'); |
| 77 | +} |
| 78 | + |
| 79 | +async function verifyContract( |
| 80 | + contractName: string, |
| 81 | + contractAddress: string, |
| 82 | + constructorArguments: string[], |
| 83 | + contract?: string |
| 84 | +) { |
| 85 | + try { |
| 86 | + const verifyContractArgs: { |
| 87 | + address: string; |
| 88 | + constructorArguments: string[]; |
| 89 | + contract?: string; |
| 90 | + } = { |
| 91 | + address: contractAddress, |
| 92 | + constructorArguments: constructorArguments |
| 93 | + }; |
| 94 | + |
| 95 | + if (contract) { |
| 96 | + verifyContractArgs.contract = contract; |
| 97 | + } |
| 98 | + |
| 99 | + await hre.run('verify:verify', verifyContractArgs); |
| 100 | + } catch (e) { |
| 101 | + // @ts-ignore |
| 102 | + // We get a failure API response if the source code has already been uploaded, don't throw in this case. |
| 103 | + if (!e.message.includes('Reason: Already Verified')) { |
| 104 | + throw e; |
| 105 | + } |
| 106 | + } |
| 107 | + console.log(`Verified ${contractName} on Etherscan!`); |
| 108 | +} |
| 109 | + |
| 110 | +main().catch((error) => { |
| 111 | + console.error(error); |
| 112 | + process.exitCode = 1; |
| 113 | +}); |
0 commit comments