From d9d202907bfcf54c771d0e797484fc1cd4c49160 Mon Sep 17 00:00:00 2001 From: Kelvin Fichter Date: Thu, 10 Jun 2021 12:19:51 -0400 Subject: [PATCH 1/5] feat[contracts]: better account funding for hardhat accounts --- .../014-OVM_L1MultiMessageRelayer.deploy.ts | 2 +- .../contracts/deploy/018-fund-accounts.ts | 61 +++++++++++++++++++ packages/contracts/package.json | 2 +- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 packages/contracts/deploy/018-fund-accounts.ts diff --git a/packages/contracts/deploy/014-OVM_L1MultiMessageRelayer.deploy.ts b/packages/contracts/deploy/014-OVM_L1MultiMessageRelayer.deploy.ts index 21040b0bf6db..e814688b95bf 100644 --- a/packages/contracts/deploy/014-OVM_L1MultiMessageRelayer.deploy.ts +++ b/packages/contracts/deploy/014-OVM_L1MultiMessageRelayer.deploy.ts @@ -30,7 +30,7 @@ const deployFn: DeployFunction = async (hre) => { await registerAddress({ hre, name: 'OVM_L2MessageRelayer', - address: OVM_L1MultiMessageRelayer.address + address: OVM_L1MultiMessageRelayer.address, }) } } diff --git a/packages/contracts/deploy/018-fund-accounts.ts b/packages/contracts/deploy/018-fund-accounts.ts new file mode 100644 index 000000000000..d7e551fd8d1d --- /dev/null +++ b/packages/contracts/deploy/018-fund-accounts.ts @@ -0,0 +1,61 @@ +/* Imports: External */ +import { DeployFunction } from 'hardhat-deploy/dist/types' +import { + defaultHardhatNetworkHdAccountsConfigParams, + defaultHardhatNetworkParams, +} from 'hardhat/internal/core/config/default-config' +import { normalizeHardhatNetworkAccountsConfig } from 'hardhat/internal/core/providers/util' + +/* Imports: Internal */ +import { getDeployedContract } from '../src/hardhat-deploy-ethers' + +// This is a TEMPORARY way to fund the default hardhat accounts on L2. The better way to do this is +// to make a modification to hardhat-ovm. However, I don't have the time right now to figure the +// details of how to make that work cleanly. This is fine in the meantime. +const deployFn: DeployFunction = async (hre) => { + // Only execute this step if we're on the hardhat chain ID. + const { chainId } = await hre.ethers.provider.getNetwork() + if (chainId === defaultHardhatNetworkParams.chainId) { + const Proxy__OVM_L1ETHGateway = await getDeployedContract( + hre, + 'Proxy__OVM_L1ETHGateway', + { + iface: 'OVM_L1ETHGateway', + } + ) + + const accounts = normalizeHardhatNetworkAccountsConfig( + defaultHardhatNetworkHdAccountsConfigParams + ) + + // Fund the accounts in parallel to speed things up. + await Promise.all( + accounts.map(async (account) => { + const wallet = new hre.ethers.Wallet( + account.privateKey, + hre.ethers.provider + ) + const balance = await wallet.getBalance() + const depositAmount = balance.div(2) // Deposit half of the wallet's balance into L2. + await Proxy__OVM_L1ETHGateway.connect(wallet).depositTo( + wallet.address, + 8_000_000, + '0x', + { + value: depositAmount, + } + ) + console.log( + `✓ Funded ${wallet.address} on L2 with ${hre.ethers.utils.formatEther( + depositAmount + )} ETH` + ) + }) + ) + } +} + +deployFn.dependencies = ['Proxy__OVM_L1ETHGateway'] +deployFn.tags = ['fund-accounts'] + +export default deployFn diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 0ca16f5048d0..46881b7c926b 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -37,7 +37,7 @@ "posttest:slither": "rm -f @openzeppelin && rm -f @ens && rm -f hardhat", "lint": "yarn lint:fix && yarn lint:check", "lint:fix": "yarn run lint:fix:typescript", - "lint:fix:typescript": "prettier --config .prettierrc.json --write \"hardhat.config.ts\" \"{src,test}/**/*.ts\"", + "lint:fix:typescript": "prettier --config .prettierrc.json --write \"hardhat.config.ts\" \"{src,test,deploy}/**/*.ts\"", "lint:check": "yarn run lint:typescript", "lint:typescript": "tslint --format stylish --project .", "clean": "rm -rf ./dist ./artifacts ./artifacts-ovm ./cache ./cache-ovm ./tsconfig.build.tsbuildinfo", From c1683af38a9c195efb3952e1fde992cac418c2f1 Mon Sep 17 00:00:00 2001 From: Kelvin Fichter Date: Thu, 10 Jun 2021 12:25:24 -0400 Subject: [PATCH 2/5] add a sleep to avoid any potential problems --- packages/contracts/deploy/018-fund-accounts.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/contracts/deploy/018-fund-accounts.ts b/packages/contracts/deploy/018-fund-accounts.ts index d7e551fd8d1d..cf56895c76a6 100644 --- a/packages/contracts/deploy/018-fund-accounts.ts +++ b/packages/contracts/deploy/018-fund-accounts.ts @@ -1,4 +1,5 @@ /* Imports: External */ +import { sleep } from '@eth-optimism/core-utils' import { DeployFunction } from 'hardhat-deploy/dist/types' import { defaultHardhatNetworkHdAccountsConfigParams, @@ -30,7 +31,11 @@ const deployFn: DeployFunction = async (hre) => { // Fund the accounts in parallel to speed things up. await Promise.all( - accounts.map(async (account) => { + accounts.map(async (account, index) => { + // Add a sleep here to avoid any potential issues with spamming hardhat. Not sure if this + // is strictly necessary but it can't hurt. + await sleep(100 * index) + const wallet = new hre.ethers.Wallet( account.privateKey, hre.ethers.provider From d0736deba1e4ba8e488ea31ac72edbf8b4a0f266 Mon Sep 17 00:00:00 2001 From: Kelvin Fichter Date: Thu, 10 Jun 2021 12:25:57 -0400 Subject: [PATCH 3/5] chore: add changeset --- .changeset/poor-owls-wash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/poor-owls-wash.md diff --git a/.changeset/poor-owls-wash.md b/.changeset/poor-owls-wash.md new file mode 100644 index 000000000000..a966e8b709e0 --- /dev/null +++ b/.changeset/poor-owls-wash.md @@ -0,0 +1,5 @@ +--- +'@eth-optimism/contracts': patch +--- + +Adds a temporary way to fund hardhat accounts when testing locally From 234a2d94800b063f750149b3217888b8b85624aa Mon Sep 17 00:00:00 2001 From: Kelvin Fichter Date: Thu, 10 Jun 2021 12:59:09 -0400 Subject: [PATCH 4/5] fix: bug with gas estimation in funding step --- packages/contracts/deploy/018-fund-accounts.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/contracts/deploy/018-fund-accounts.ts b/packages/contracts/deploy/018-fund-accounts.ts index cf56895c76a6..6c3110aa6154 100644 --- a/packages/contracts/deploy/018-fund-accounts.ts +++ b/packages/contracts/deploy/018-fund-accounts.ts @@ -34,7 +34,7 @@ const deployFn: DeployFunction = async (hre) => { accounts.map(async (account, index) => { // Add a sleep here to avoid any potential issues with spamming hardhat. Not sure if this // is strictly necessary but it can't hurt. - await sleep(100 * index) + await sleep(200 * index) const wallet = new hre.ethers.Wallet( account.privateKey, @@ -42,14 +42,10 @@ const deployFn: DeployFunction = async (hre) => { ) const balance = await wallet.getBalance() const depositAmount = balance.div(2) // Deposit half of the wallet's balance into L2. - await Proxy__OVM_L1ETHGateway.connect(wallet).depositTo( - wallet.address, - 8_000_000, - '0x', - { - value: depositAmount, - } - ) + await Proxy__OVM_L1ETHGateway.connect(wallet).deposit(8_000_000, '0x', { + value: depositAmount, + gasLimit: 2_000_000, // Idk, gas estimation was broken and this fixes it. + }) console.log( `✓ Funded ${wallet.address} on L2 with ${hre.ethers.utils.formatEther( depositAmount From 03f3a209dfdfd877571a1bff885f4e88ffb32ffb Mon Sep 17 00:00:00 2001 From: Kelvin Fichter Date: Thu, 10 Jun 2021 13:04:49 -0400 Subject: [PATCH 5/5] fix: limit to 20 accounts max --- packages/contracts/deploy/018-fund-accounts.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/contracts/deploy/018-fund-accounts.ts b/packages/contracts/deploy/018-fund-accounts.ts index 6c3110aa6154..3dc7c9b0a4fe 100644 --- a/packages/contracts/deploy/018-fund-accounts.ts +++ b/packages/contracts/deploy/018-fund-accounts.ts @@ -25,9 +25,11 @@ const deployFn: DeployFunction = async (hre) => { } ) + // Default has 20 accounts but we restrict to 20 accounts manually as well just to prevent + // future problems if the number of default accounts increases for whatever reason. const accounts = normalizeHardhatNetworkAccountsConfig( defaultHardhatNetworkHdAccountsConfigParams - ) + ).slice(0, 20) // Fund the accounts in parallel to speed things up. await Promise.all(