Commit 91e9a748 authored by kf's avatar kf Committed by Kelvin Fichter

fix: bugs in forked network setup script

parent 70ff6476
/* eslint @typescript-eslint/no-var-requires: "off" */ /* eslint @typescript-eslint/no-var-requires: "off" */
import { ethers } from 'ethers'
import { DeployFunction } from 'hardhat-deploy/dist/types' import { DeployFunction } from 'hardhat-deploy/dist/types'
import { import {
getAdvancedContract, getContractFromArtifact,
fundAccount,
sendImpersonatedTx,
waitUntilTrue, waitUntilTrue,
BIG_BALANCE,
} from '../src/hardhat-deploy-ethers' } from '../src/hardhat-deploy-ethers'
const deployFn: DeployFunction = async (hre) => { const deployFn: DeployFunction = async (hre) => {
const { deployer } = await hre.getNamedAccounts()
if ((hre as any).deployConfig.forked !== 'true') { if ((hre as any).deployConfig.forked !== 'true') {
return return
} }
console.log(`Running custom setup for forked experimental networks`) console.log(`Running custom setup for forked experimental networks`)
const { deployer } = await hre.getNamedAccounts()
// Fund the deployer account // Fund the deployer account so it can be used for the rest of this deployment.
console.log(`Funding deployer account...`) console.log(`Funding deployer account...`)
const amount = `0xFFFFFFFFFFFFFFFFFF` await fundAccount(hre, deployer, BIG_BALANCE)
await hre.ethers.provider.send('hardhat_setBalance', [deployer, amount])
console.log(`Waiting for balance to reflect...`)
await waitUntilTrue(async () => {
const balance = await hre.ethers.provider.getBalance(deployer)
return balance.gte(hre.ethers.BigNumber.from(amount))
})
// Get a reference to the AddressManager contract. // Get a reference to the AddressManager contract.
const artifact = await hre.deployments.get('Lib_AddressManager') const Lib_AddressManager = await getContractFromArtifact(
const Lib_AddressManager = getAdvancedContract({
hre, hre,
contract: new hre.ethers.Contract( 'Lib_AddressManager'
artifact.address, )
artifact.abi,
hre.ethers.provider
),
})
// Impersonate the owner of the AddressManager // Transfer ownership of the AddressManager to the deployer.
console.log(`Impersonating owner account...`)
const owner = await Lib_AddressManager.owner()
await hre.ethers.provider.send('hardhat_impersonateAccount', [owner])
console.log(`Started impersonating ${owner}`)
console.log(`Setting AddressManager owner to ${deployer}`) console.log(`Setting AddressManager owner to ${deployer}`)
const signer = await hre.ethers.getSigner(owner) await sendImpersonatedTx({
await Lib_AddressManager.connect(signer).transferOwnership(deployer) hre,
contract: Lib_AddressManager,
fn: 'transferOwnership',
from: await Lib_AddressManager.owner(),
gas: ethers.BigNumber.from(2_000_000).toHexString(),
args: [deployer],
})
console.log(`Waiting for owner to be correctly set...`) console.log(`Waiting for owner to be correctly set...`)
await waitUntilTrue(async () => { await waitUntilTrue(async () => {
return (await Lib_AddressManager.owner()) === deployer return (await Lib_AddressManager.owner()) === deployer
}) })
console.log(`Disabling impersonation...`) // Get a reference to the L1StandardBridge contract.
await hre.ethers.provider.send('hardhat_stopImpersonatingAccount', [owner]) const Proxy__OVM_L1StandardBridge = await getContractFromArtifact(
hre,
'Proxy__OVM_L1StandardBridge'
)
// Transfer ownership of the L1StandardBridge to the deployer.
console.log(`Setting L1StandardBridge owner to ${deployer}`)
await sendImpersonatedTx({
hre,
contract: Proxy__OVM_L1StandardBridge,
fn: 'setOwner',
from: await Proxy__OVM_L1StandardBridge.callStatic.getOwner({
from: hre.ethers.constants.AddressZero,
}),
gas: ethers.BigNumber.from(2_000_000).toHexString(),
args: [deployer],
})
console.log(`Waiting for owner to be correctly set...`)
await waitUntilTrue(async () => {
return (
(await Proxy__OVM_L1StandardBridge.callStatic.getOwner({
from: hre.ethers.constants.AddressZero,
})) === deployer
)
})
} }
deployFn.tags = ['hardhat', 'upgrade'] deployFn.tags = ['hardhat', 'upgrade']
......
/* Imports: External */ /* Imports: External */
import { Contract } from 'ethers' import { ethers, Contract } from 'ethers'
import { Provider } from '@ethersproject/abstract-provider' import { Provider } from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer' import { Signer } from '@ethersproject/abstract-signer'
import { sleep, hexStringEquals } from '@eth-optimism/core-utils' import { sleep, hexStringEquals } from '@eth-optimism/core-utils'
import { HttpNetworkConfig } from 'hardhat/types'
export const waitUntilTrue = async ( export const waitUntilTrue = async (
check: () => Promise<boolean>, check: () => Promise<boolean>,
...@@ -208,3 +209,82 @@ export const getDeployedContract = async ( ...@@ -208,3 +209,82 @@ export const getDeployedContract = async (
contract: new Contract(deployed.address, iface, signerOrProvider), contract: new Contract(deployed.address, iface, signerOrProvider),
}) })
} }
export const fundAccount = async (
hre: any,
address: string,
amount: ethers.BigNumber
) => {
if ((hre as any).deployConfig.forked !== 'true') {
throw new Error('this method can only be used against a forked network')
}
console.log(`Funding account ${address}...`)
await hre.ethers.provider.send('hardhat_setBalance', [
address,
amount.toHexString(),
])
console.log(`Waiting for balance to reflect...`)
await waitUntilTrue(async () => {
const balance = await hre.ethers.provider.getBalance(address)
return balance.gte(amount)
})
console.log(`Account successfully funded.`)
}
export const sendImpersonatedTx = async (opts: {
hre: any
contract: ethers.Contract
fn: string
from: string
gas: string
args: any[]
}) => {
if ((opts.hre as any).deployConfig.forked !== 'true') {
throw new Error('this method can only be used against a forked network')
}
console.log(`Impersonating account ${opts.from}...`)
await opts.hre.ethers.provider.send('hardhat_impersonateAccount', [opts.from])
console.log(`Funding account ${opts.from}...`)
await fundAccount(opts.hre, opts.from, BIG_BALANCE)
console.log(`Sending impersonated transaction...`)
const tx = await opts.contract.populateTransaction[opts.fn](...opts.args)
const provider = new opts.hre.ethers.providers.JsonRpcProvider(
(opts.hre.network.config as HttpNetworkConfig).url
)
await provider.send('eth_sendTransaction', [
{
...tx,
from: opts.from,
gas: opts.gas,
},
])
console.log(`Stopping impersonation of account ${opts.from}...`)
await opts.hre.ethers.provider.send('hardhat_stopImpersonatingAccount', [
opts.from,
])
}
export const getContractFromArtifact = async (
hre: any,
name: string
): Promise<ethers.Contract> => {
const artifact = await hre.deployments.get(name)
return getAdvancedContract({
hre,
contract: new hre.ethers.Contract(
artifact.address,
artifact.abi,
hre.ethers.provider
),
})
}
// Large balance to fund accounts with.
export const BIG_BALANCE = ethers.BigNumber.from(`0xFFFFFFFFFFFFFFFFFFFF`)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment