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(hardhat-helpers): standalone contracts deploy functions #13233

Merged
merged 12 commits into from
Jan 24, 2024
16 changes: 7 additions & 9 deletions governance/scripts/deployments/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const { ethers } = require('hardhat')
const UniswapOracleV2 = require('@unlock-protocol/hardhat-helpers/dist/ABIs/UniswapV2Oracle.json')
const { UniswapOracleV3 } = require('@unlock-protocol/contracts')

const { getNetwork } = require('@unlock-protocol/hardhat-helpers')
const {
getNetwork,
deployContract,
} = require('@unlock-protocol/hardhat-helpers')

// TODO: check if oracle has already been deployed and skips if one already exists!
async function main({ uniswapFactoryAddress, uniswapVersion = 3 } = {}) {
if (!uniswapFactoryAddress) {
console.log(await getNetwork())
const {
uniswapV3: { factoryAddress },
} = await getNetwork()
Expand All @@ -34,13 +36,9 @@ async function main({ uniswapFactoryAddress, uniswapVersion = 3 } = {}) {
)
}

const oracle = await Oracle.deploy(uniswapFactoryAddress)
await oracle.waitForDeployment()
const { hash } = await oracle.deploymentTransaction()

// get addresses
const oracleAddress = await oracle.getAddress()

const { hash, address: oracleAddress } = await deployContract(Oracle, [
uniswapFactoryAddress,
])
console.log(
`UNISWAP ORACLE > Oracle deployed at ${oracleAddress} (tx: ${hash})`
)
Expand Down
10 changes: 6 additions & 4 deletions governance/scripts/deployments/publicLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { ethers, run } = require('hardhat')
const {
isLocalhost,
ADDRESS_ZERO,
deployContract,
} = require('@unlock-protocol/hardhat-helpers')
const contracts = require('@unlock-protocol/contracts')

Expand All @@ -20,10 +21,11 @@ async function main({ publicLockVersion }) {
throw Error('Need to set --public-lock-version')
}

const publicLock = await PublicLock.deploy()
await publicLock.waitForDeployment()
const { hash } = await publicLock.deploymentTransaction()
const publicLockAddress = await publicLock.getAddress()
const {
contract: publicLock,
hash,
address: publicLockAddress,
} = await deployContract(PublicLock)

console.log(
`PUBLIC LOCK > deployed v${await publicLock.publicLockVersion()} to : ${publicLockAddress} (tx: ${hash})`
Expand Down
26 changes: 17 additions & 9 deletions governance/scripts/deployments/swapBurner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { ethers, run } = require('hardhat')
const { PERMIT2_ADDRESS } = require('@uniswap/universal-router-sdk')
const { getNetwork } = require('@unlock-protocol/hardhat-helpers')
const {
getNetwork,
isLocalhost,
deployContract,
} = require('@unlock-protocol/hardhat-helpers')
const { UnlockSwapBurner } = require('@unlock-protocol/contracts')

async function main() {
Expand Down Expand Up @@ -29,16 +33,20 @@ async function main() {
UnlockSwapBurner.bytecode
)

const swapper = await SwapAndBurn.deploy(
unlockAddress,
PERMIT2_ADDRESS,
routerAddress
console.log(` waiting for tx to be mined for contract verification...`)
const {
contract: swapper,
hash,
address: swapperAddress,
} = await deployContract(
SwapAndBurn,
[unlockAddress, PERMIT2_ADDRESS, routerAddress],
{ wait: 5 }
)
console.log(` swapper deployed at ${await swapper.getAddress()}`)

if (chainId !== 31337) {
console.log(` waiting for tx to be mined for contract verification...`)
await swapper.waitForDeployment(5)
console.log(`SwapAndBurn deployed at ${swapperAddress} (tx: ${hash})`)

if (!(await isLocalhost())) {
await run('verify:verify', {
address: await swapper.getAddress(),
constructorArguments: [unlockAddress, PERMIT2_ADDRESS, routerAddress],
Expand Down
25 changes: 17 additions & 8 deletions governance/scripts/deployments/swapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { PERMIT2_ADDRESS } = require('@uniswap/universal-router-sdk')
const {
uniswapRouterAddresses,
getNetwork,
isLocalhost,
deployContract,
} = require('@unlock-protocol/hardhat-helpers')
const { UnlockSwapPurchaser } = require('@unlock-protocol/contracts')

Expand All @@ -11,7 +13,7 @@ async function main() {
const { unlockAddress, id: chainId } = await getNetwork()

const routers = Object.values(uniswapRouterAddresses[chainId])
console.log(`Deploying Swapper to ${chainId}
console.log(`Deploying SwapPurchaser to ${chainId}
- unlockAddress: ${unlockAddress}
- PERMIT2_ADDRESS : ${PERMIT2_ADDRESS}
- routers: ${routers}`)
Expand All @@ -24,19 +26,26 @@ async function main() {
console.log(
`Deploying UnlockSwapPurchaser on chain ${chainId} (unlock: ${unlockAddress}, permit2: ${PERMIT2_ADDRESS}, routers: ${routers.toString()}) `
)
const Swapper = await ethers.getContractFactory(
const SwapPurchaser = await ethers.getContractFactory(
UnlockSwapPurchaser.abi,
UnlockSwapPurchaser.bytecode
)

const swapper = await Swapper.deploy(unlockAddress, PERMIT2_ADDRESS, routers)
console.log(` swapper deployed at ${swapper.address}`)
console.log(` waiting for tx to be mined for contract verification...`)
const {
contract: swapPurchaser,
hash,
address: swapPurchaserAddress,
} = await deployContract(
SwapPurchaser,
[unlockAddress, PERMIT2_ADDRESS, routers],
{ wait: 5 }
)
console.log(`SwapPurchaser deployed at ${swapPurchaserAddress} (tx: ${hash})`)

if (chainId !== 31337) {
console.log(` waiting for tx to be mined for contract verification...`)
await swapper.waitForDeployment(5)
if (!(await isLocalhost())) {
await run('verify:verify', {
address: await swapper.getAddress(),
address: await swapPurchaser.getAddress(),
constructorArguments: [unlockAddress, PERMIT2_ADDRESS, routers],
})
}
Expand Down
9 changes: 3 additions & 6 deletions governance/scripts/deployments/weth.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const { ethers } = require('hardhat')
const { deployContract } = require('@unlock-protocol/hardhat-helpers')
const WETH = require('@unlock-protocol/hardhat-helpers/dist/ABIs/weth.json')

async function main() {
const Weth = await ethers.getContractFactory(WETH.abi, WETH.bytecode)
const weth = await Weth.deploy()
await weth.deployed()
const { contract: weth, address, hash } = await deployContract(Weth)

// eslint-disable-next-line no-console
console.log(
`WETH > deployed to : ${weth.address} (tx: ${weth.deployTransaction.hash}`
)
console.log(`WETH > deployed to : ${address} (tx: ${hash}`)
return weth.address
}

Expand Down
20 changes: 20 additions & 0 deletions packages/hardhat-helpers/src/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const deployContract = async (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need a deployUpgradableContract helper?
Maybe in a follow up PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, added too here

Factory,
deployArgs = [],
deployOptions = { wait: 1 }
) => {
const contract = await Factory.deploy(...deployArgs)
await contract.waitForDeployment(deployOptions.wait)
const { hash } = await contract.deploymentTransaction()
const address = await contract.getAddress()

return {
contract,
hash,
address,
}
}

export default {
deployContract,
}
2 changes: 2 additions & 0 deletions packages/hardhat-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import unlock from './unlock'
import upgrades from './upgrades'
import localhost from './localhost'
import events from './events'
import deploy from './deploy'

module.exports = {
...balance,
Expand All @@ -30,4 +31,5 @@ module.exports = {
...upgrades,
...localhost,
...events,
...deploy,
}
2 changes: 1 addition & 1 deletion packages/hardhat-helpers/src/localhost.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const isLocalhost = async () => {
const { ethers } = require('hardhat')
const { chainId } = await ethers.provider.getNetwork()
return chainId.toString() === '31137'
return chainId.toString() === '31137' || !!process.env.RUN_FORK
}

export default {
Expand Down
Loading