Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[contracts]: slightly better account funding for hardhat accounts (rebased) #1065

Merged
merged 5 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-owls-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/contracts': patch
---

Adds a temporary way to fund hardhat accounts when testing locally
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const deployFn: DeployFunction = async (hre) => {
await registerAddress({
hre,
name: 'OVM_L2MessageRelayer',
address: OVM_L1MultiMessageRelayer.address
address: OVM_L1MultiMessageRelayer.address,
})
}
}
Expand Down
64 changes: 64 additions & 0 deletions packages/contracts/deploy/018-fund-accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Imports: External */
import { sleep } from '@eth-optimism/core-utils'
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',
}
)

// 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(
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(200 * index)

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).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
)} ETH`
)
})
)
}
}

deployFn.dependencies = ['Proxy__OVM_L1ETHGateway']
deployFn.tags = ['fund-accounts']

export default deployFn
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down