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
5 changes: 2 additions & 3 deletions governance/hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ require('@nomicfoundation/hardhat-ethers')
require('@nomicfoundation/hardhat-verify')
require('@openzeppelin/hardhat-upgrades')

const tdly = require('@tenderly/hardhat-tenderly')
tdly.setup()

// import helpers
const {
etherscan,
Expand Down Expand Up @@ -37,6 +34,8 @@ if (process.env.RUN_FORK) {

// add tenderly if needed
if (process.env.TENDERLY_FORK) {
const tdly = require('@tenderly/hardhat-tenderly')
tdly.setup()
networks.tenderly = {
url: process.env.TENDERLY_FORK,
accounts: networks.mainnet.accounts,
Expand Down
82 changes: 9 additions & 73 deletions governance/scripts/deployments/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
/* eslint-disable global-require */
const { ethers, run, upgrades, network } = require('hardhat')
const UniswapV2Router02 = require('@uniswap/v2-periphery/build/UniswapV2Router02.json')
const { networks } = require('@unlock-protocol/networks')
const createLock = require('../lock/create')
const { getUnlock, ADDRESS_ZERO } = require('@unlock-protocol/hardhat-helpers')

const { MaxUint256 } = ethers

const log = (...message) => {
// eslint-disable-next-line no-console
console.log('UNLOCK DEPLOYMENT >', ...message)
}

// TODO: for each contract deployed, can we instantly verify them?
// TODO: prompt user for each action before doing them and ask them for input?
async function main({
premintAmount, // in ETH, must be a string
liquidity, // in ETH, must be a string
unlockAddress,
unlockVersion,
publicLockVersion,
udtAddress,
publicLockAddress,
wethAddress,
uniswapRouterAddress,
uniswapFactoryAddress,
oracleAddress,
estimatedGasForPurchase,
locksmithURI,
Expand Down Expand Up @@ -72,16 +64,14 @@ async function main({
// If UDT is not set for this network, let's not worry about it
if (udtAddress !== ADDRESS_ZERO) {
// pre-mint some UDTs, then delegate mint caps to contract
if (isLocalNet || premintAmount) {
if (isLocalNet) {
const UDT = await ethers.getContractFactory('UnlockDiscountTokenV3')
udt = UDT.attach(udtAddress)

const premintAmount = '1000000.0'
udt = udt.connect(minter)
await udt.mint(
deployer.address,
ethers.parseEther(premintAmount || '1000000.0')
)
log(`Pre-minted ${premintAmount || '1000000.0'} UDT to deployer`)
await udt.mint(deployer.address, ethers.parseEther())
log(`Pre-minted ${premintAmount} UDT to deployer`)

await udt.addMinter(unlockAddress)
log('grant minting permissions to the Unlock Contract')
Expand All @@ -95,65 +85,6 @@ async function main({
wethAddress = await run('deploy:weth')
log(`WETH deployed to : ${wethAddress}`)
}

// deploy uniswap v2 if needed
if ((!uniswapFactoryAddress || !uniswapRouterAddress) && isLocalNet) {
if (!wethAddress || wethAddress === ADDRESS_ZERO) {
throw new Error(
'Missing wethAddress. Cannot deploy Uniswap factory. Please use --weth-address'
)
}
const { router, factory } = await run('deploy:uniswap', { wethAddress })
uniswapRouterAddress = router
uniswapFactoryAddress = factory
}

if (!uniswapRouterAddress) {
throw new Error(
'Missing uniswapRouterAddress. Cannot proceed. Please use --uniswap-router-address'
)
}

if (!uniswapFactoryAddress) {
throw new Error(
'Missing uniswapFactoryAddress. Cannot proceed. Please use --uniswap-factory-address'
)
}

// get uniswap instance
const Router = await ethers.getContractFactory(
UniswapV2Router02.abi,
UniswapV2Router02.bytecode
)
const uniswapRouter = Router.attach(uniswapRouterAddress)
uniswapFactoryAddress = await uniswapRouter.factory()

// add liquidity
if (isLocalNet) {
const amountLiquidity = liquidity || '1000.0'
await udt
.connect(deployer)
.approve(uniswapRouterAddress, ethers.parseEther(amountLiquidity))
log(`UDT approved Uniswap Router for ${amountLiquidity} ETH`)

await uniswapRouter.connect(deployer).addLiquidityETH(
udtAddress,
ethers.parseEther(amountLiquidity), // pool size
'1',
'1',
deployer.address, // receiver
MaxUint256, // max timestamp
{ value: ethers.parseEther('10.0') }
)
log(`added liquidity to uniswap ${amountLiquidity}`)
}

// deploy oracle if needed
if (!oracleAddress) {
oracleAddress = await run('deploy:oracle', {
uniswapFactoryAddress,
})
}
}

// config unlock
Expand Down Expand Up @@ -211,6 +142,11 @@ async function main({
maxNumberOfKeys: 100,
name: 'Test Lock',
})

return {
unlockAddress,
publicLockAddress,
}
}

// execute as standalone
Expand Down
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
28 changes: 15 additions & 13 deletions governance/scripts/deployments/publicLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@ const { ethers, run } = require('hardhat')
const {
isLocalhost,
ADDRESS_ZERO,
deployContract,
copyAndBuildContractsAtVersion,
} = require('@unlock-protocol/hardhat-helpers')

async function main({ publicLockVersion }) {
// fetch chain info
const [signer] = await ethers.getSigners()

let PublicLock
if (publicLockVersion) {
console.log(
`PUBLIC LOCK > Deploying lock template for released version ${publicLockVersion} with signer ${signer.address}`
)
;[PublicLock] = await copyAndBuildContractsAtVersion(__dirname, [
{ contractName: 'PublicLock', version: publicLockVersion },
])
} else {
if (!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()
console.log(
`PUBLIC LOCK > Deploying lock template for released version ${publicLockVersion} with signer ${signer.address}`
)

const [qualifiedPath] = await copyAndBuildContractsAtVersion(__dirname, [
{ contractName: 'PublicLock', version: publicLockVersion },
])

const {
contract: publicLock,
hash,
address: publicLockAddress,
} = await deployContract(qualifiedPath)

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, isLocalhost } = 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 (await isLocalhost()) {
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
19 changes: 9 additions & 10 deletions governance/scripts/deployments/udt.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { ethers, upgrades, run } = require('hardhat')
const { ethers, run } = require('hardhat')
const {
copyAndBuildContractsAtVersion,
isLocalhost,
deployUpgradeableContract,
} = require('@unlock-protocol/hardhat-helpers')

async function main() {
Expand All @@ -10,21 +11,19 @@ async function main() {
await copyAndBuildContractsAtVersion(__dirname, [
{
contractName: 'UnlockDiscountToken',
contractFullName: 'UnlockDiscountTokenV3',
version: 3,
},
])

const UDT = await ethers.getContractFactory(
'contracts/past-versions/UnlockDiscountTokenV3.sol:UnlockDiscountTokenV3'
const { hash, address: udtAddress } = await deployUpgradeableContract(
'contracts/past-versions/UnlockDiscountTokenV3.sol:UnlockDiscountTokenV3',
[minter.address],
{
initializer: 'initialize(address)',
}
)

const udt = await upgrades.deployProxy(UDT, [minter.address], {
initializer: 'initialize(address)',
})
await udt.waitForDeployment()
const { hash } = await udt.deploymentTransaction()
const udtAddress = await udt.getAddress()

// eslint-disable-next-line no-console
console.log(
`UDT SETUP > UDT v3 (w proxy) deployed to: ${udtAddress} (tx: ${hash})`
Expand Down
Loading
Loading