From a76d7e28ee7f58dc9f4ad2bad9ffb2bb16d719b0 Mon Sep 17 00:00:00 2001 From: Alex Towle Date: Fri, 6 Dec 2024 16:06:10 -0600 Subject: [PATCH] Addressed review feedback from John McClure --- hardhat.config.mainnet.ts | 4 - tasks/deploy/hyperdrive-matching-engine.ts | 32 ++---- tasks/deploy/lib/utils.ts | 31 +++++- tasks/deploy/registry.ts | 114 +++++++++----------- tasks/deploy/uni-v3-zap.ts | 115 +++++++++------------ 5 files changed, 135 insertions(+), 161 deletions(-) diff --git a/hardhat.config.mainnet.ts b/hardhat.config.mainnet.ts index deaee16a2..7f5468759 100644 --- a/hardhat.config.mainnet.ts +++ b/hardhat.config.mainnet.ts @@ -11,11 +11,9 @@ import { MAINNET_EZETH_182DAY, MAINNET_EZETH_COORDINATOR, MAINNET_FACTORY, - MAINNET_MORPHO_BLUE_CBBTC_USDC_182DAY, MAINNET_MORPHO_BLUE_COORDINATOR, MAINNET_MORPHO_BLUE_SUSDE_DAI_182DAY, MAINNET_MORPHO_BLUE_USDE_DAI_182DAY, - MAINNET_MORPHO_BLUE_WBTC_USDC_182DAY, MAINNET_MORPHO_BLUE_WSTETH_USDA_182DAY, MAINNET_RETH_182DAY, MAINNET_RETH_COORDINATOR, @@ -47,8 +45,6 @@ const config: HardhatUserConfig = { MAINNET_MORPHO_BLUE_SUSDE_DAI_182DAY, MAINNET_MORPHO_BLUE_USDE_DAI_182DAY, MAINNET_MORPHO_BLUE_WSTETH_USDA_182DAY, - MAINNET_MORPHO_BLUE_WBTC_USDC_182DAY, - MAINNET_MORPHO_BLUE_CBBTC_USDC_182DAY, MAINNET_STUSD_182DAY, MAINNET_SUSDE_182DAY, MAINNET_SGYD_182DAY, diff --git a/tasks/deploy/hyperdrive-matching-engine.ts b/tasks/deploy/hyperdrive-matching-engine.ts index 9baad53af..2f533bdf2 100644 --- a/tasks/deploy/hyperdrive-matching-engine.ts +++ b/tasks/deploy/hyperdrive-matching-engine.ts @@ -1,19 +1,12 @@ import { subtask } from "hardhat/config"; -import { - Address, - decodeEventLog, - encodeDeployData, - encodePacked, - parseEther, -} from "viem"; +import { Address, decodeEventLog, encodeDeployData, encodePacked } from "viem"; import { CREATEX_ABI } from "../../lib/createx/interface/src/lib/constants"; import { CREATE_X_FACTORY, - CREATE_X_FACTORY_DEPLOYER, - CREATE_X_PRESIGNED_TRANSACTION, HYPERDRIVE_MATCHING_ENGINE_SALT, HyperdriveDeployBaseTask, HyperdriveDeployNamedTaskParams, + deployCreateX, } from "./lib"; // Extend params to include the additional constructor arguments @@ -28,23 +21,13 @@ HyperdriveDeployBaseTask( "deploys the HyperdriveMatchingEngine contract to the configured chain", ), ).setAction( - async ( - { name, morpho }: DeployHyperdriveMatchingEngineParams, - { hyperdriveDeploy, artifacts, viem, network }, - ) => { + async ({ name, morpho }: DeployHyperdriveMatchingEngineParams, hre) => { console.log("\nRunning deploy:hyperdrive-matching-engine ..."); + const { hyperdriveDeploy, artifacts, viem, network } = hre; - if (network.name == "anvil") { - let tc = await viem.getTestClient(); - await tc.setBalance({ - value: parseEther("1"), - address: CREATE_X_FACTORY_DEPLOYER, - }); - let wc = await viem.getWalletClient(CREATE_X_FACTORY_DEPLOYER); - await wc.sendRawTransaction({ - serializedTransaction: CREATE_X_PRESIGNED_TRANSACTION, - }); - } + // If the network is anvil, we need to deploy the CREATEX factory if it + // hasn't been deployed before. + await deployCreateX(hre); // Skip if the zap is already deployed if (!!hyperdriveDeploy.deployments.byNameSafe(name)) { @@ -72,7 +55,6 @@ HyperdriveDeployBaseTask( HYPERDRIVE_MATCHING_ENGINE_SALT, creationCode, ]); - let pc = await viem.getPublicClient(); let { logs } = await pc.waitForTransactionReceipt({ hash: tx }); const decodedLog = decodeEventLog({ abi: CREATEX_ABI, diff --git a/tasks/deploy/lib/utils.ts b/tasks/deploy/lib/utils.ts index 793b34c4c..f9be6df7d 100644 --- a/tasks/deploy/lib/utils.ts +++ b/tasks/deploy/lib/utils.ts @@ -1,7 +1,12 @@ import dayjs from "dayjs"; import _duration from "dayjs/plugin/duration"; import { HardhatRuntimeEnvironment } from "hardhat/types"; -import { Address, Hex, toHex } from "viem"; +import { Address, Hex, parseEther, toHex } from "viem"; +import { + CREATE_X_FACTORY, + CREATE_X_FACTORY_DEPLOYER, + CREATE_X_PRESIGNED_TRANSACTION, +} from "./constants"; import { HyperdriveDeployRuntimeOptions } from "./environment-extensions"; import { DurationString, @@ -12,6 +17,30 @@ import { dayjs.extend(_duration); +/** + * Deploys the CreateX contract on anvil networks if it hasn't already been + * deployed. + */ +export async function deployCreateX(hre: HardhatRuntimeEnvironment) { + // If the network is anvil, we need to deploy the CREATEX factory if it + // hasn't been deployed before. + let pc = await hre.viem.getPublicClient(); + let bytecode = await pc.getBytecode({ + address: CREATE_X_FACTORY as Address, + }); + if (hre.network.name == "anvil" && bytecode!.length == 2) { + let tc = await hre.viem.getTestClient(); + await tc.setBalance({ + value: parseEther("1"), + address: CREATE_X_FACTORY_DEPLOYER, + }); + let wc = await hre.viem.getWalletClient(CREATE_X_FACTORY_DEPLOYER); + await wc.sendRawTransaction({ + serializedTransaction: CREATE_X_PRESIGNED_TRANSACTION, + }); + } +} + /** * Generates a 32-byte hex string from the input value. */ diff --git a/tasks/deploy/registry.ts b/tasks/deploy/registry.ts index 60580745e..e2a6d0d9f 100644 --- a/tasks/deploy/registry.ts +++ b/tasks/deploy/registry.ts @@ -4,16 +4,14 @@ import { decodeEventLog, encodeFunctionData, encodePacked, - parseEther, } from "viem"; import { CREATEX_ABI } from "../../lib/createx/interface/src/lib/constants"; import { CREATE_X_FACTORY, - CREATE_X_FACTORY_DEPLOYER, - CREATE_X_PRESIGNED_TRANSACTION, HyperdriveDeployBaseTask, HyperdriveDeployNamedTaskParams, REGISTRY_SALT, + deployCreateX, } from "./lib"; export type DeployRegistryParams = HyperdriveDeployNamedTaskParams; @@ -23,71 +21,59 @@ HyperdriveDeployBaseTask( "deploy:registry", "deploys the hyperdrive registry to the configured chain", ), -).setAction( - async ( - { name }: DeployRegistryParams, - { hyperdriveDeploy, artifacts, getNamedAccounts, viem, network }, - ) => { - console.log("\nRunning deploy:registry ..."); +).setAction(async ({ name }: DeployRegistryParams, hre) => { + console.log("\nRunning deploy:registry ..."); + const { hyperdriveDeploy, artifacts, getNamedAccounts, viem } = hre; - let deployer = (await getNamedAccounts())["deployer"] as Address; + let deployer = (await getNamedAccounts())["deployer"] as Address; - if (network.name == "anvil") { - let tc = await viem.getTestClient(); - await tc.setBalance({ - value: parseEther("1"), - address: CREATE_X_FACTORY_DEPLOYER, - }); - let wc = await viem.getWalletClient(CREATE_X_FACTORY_DEPLOYER); - await wc.sendRawTransaction({ - serializedTransaction: CREATE_X_PRESIGNED_TRANSACTION, - }); - } + // If the network is anvil, we need to deploy the CREATEX factory if it + // hasn't been deployed before. + await deployCreateX(hre); - // Skip if the registry is already deployed. - if (!!hyperdriveDeploy.deployments.byNameSafe(name)) { - console.log(`skipping ${name}, found existing deployment`); - return; - } + // Skip if the registry is already deployed. + if (!!hyperdriveDeploy.deployments.byNameSafe(name)) { + console.log(`skipping ${name}, found existing deployment`); + return; + } - let createXDeployer = await viem.getContractAt( - "IDeterministicDeployer", - CREATE_X_FACTORY, - ); + let createXDeployer = await viem.getContractAt( + "IDeterministicDeployer", + CREATE_X_FACTORY, + ); - // Assemble the creation code by packing the registry contract's - // bytecode with its constructor arguments. - let artifact = artifacts.readArtifactSync("HyperdriveRegistry"); - let creationCode = encodePacked(["bytes"], [artifact.bytecode]); - let initializationData = encodeFunctionData({ - abi: artifact.abi, - functionName: "initialize", - args: [name, deployer], - }); + // Assemble the creation code by packing the registry contract's + // bytecode with its constructor arguments. + let artifact = artifacts.readArtifactSync("HyperdriveRegistry"); + let creationCode = encodePacked(["bytes"], [artifact.bytecode]); + let initializationData = encodeFunctionData({ + abi: artifact.abi, + functionName: "initialize", + args: [name, deployer], + }); - // Call the Create3 deployer to deploy the contract. - let tx = await createXDeployer.write.deployCreate3AndInit([ - REGISTRY_SALT, - creationCode, - initializationData, - { constructorAmount: 0n, initCallAmount: 0n }, - ]); - let pc = await viem.getPublicClient(); - let { logs } = await pc.waitForTransactionReceipt({ hash: tx }); - const decodedLog = decodeEventLog({ - abi: CREATEX_ABI, - topics: logs[1].topics, - data: logs[1].data, - }); - let deployedAddress = decodedLog.args.newContract; + // Call the Create3 deployer to deploy the contract. + let tx = await createXDeployer.write.deployCreate3AndInit([ + REGISTRY_SALT, + creationCode, + initializationData, + { constructorAmount: 0n, initCallAmount: 0n }, + ]); + let pc = await viem.getPublicClient(); + let { logs } = await pc.waitForTransactionReceipt({ hash: tx }); + const decodedLog = decodeEventLog({ + abi: CREATEX_ABI, + topics: logs[1].topics, + data: logs[1].data, + }); + let deployedAddress = decodedLog.args.newContract; - // Use the deployer address to back-compute the deployed contract address - // and store the deployment configuration in deployments.json. - console.log(` - saving ${name}...`); - hyperdriveDeploy.deployments.add( - name, - "HyperdriveRegistry", - deployedAddress as Address, - ); - }, -); + // Use the deployer address to back-compute the deployed contract address + // and store the deployment configuration in deployments.json. + console.log(` - saving ${name}...`); + hyperdriveDeploy.deployments.add( + name, + "HyperdriveRegistry", + deployedAddress as Address, + ); +}); diff --git a/tasks/deploy/uni-v3-zap.ts b/tasks/deploy/uni-v3-zap.ts index 1ffae4a06..e58da05b6 100644 --- a/tasks/deploy/uni-v3-zap.ts +++ b/tasks/deploy/uni-v3-zap.ts @@ -1,19 +1,12 @@ import { subtask } from "hardhat/config"; -import { - Address, - decodeEventLog, - encodeDeployData, - encodePacked, - parseEther, -} from "viem"; +import { Address, decodeEventLog, encodeDeployData, encodePacked } from "viem"; import { CREATEX_ABI } from "../../lib/createx/interface/src/lib/constants"; import { CREATE_X_FACTORY, - CREATE_X_FACTORY_DEPLOYER, - CREATE_X_PRESIGNED_TRANSACTION, HyperdriveDeployBaseTask, HyperdriveDeployNamedTaskParams, UNI_V3_ZAP_SALT, + deployCreateX, } from "./lib"; // Extend params to include the additional constructor arguments @@ -27,66 +20,54 @@ HyperdriveDeployBaseTask( "deploy:uni-v3-zap", "deploys the UniV3Zap contract to the configured chain", ), -).setAction( - async ( - { name, swapRouter, weth }: DeployUniV3ZapParams, - { hyperdriveDeploy, artifacts, viem, network }, - ) => { - console.log("\nRunning deploy:univ3zap ..."); +).setAction(async ({ name, swapRouter, weth }: DeployUniV3ZapParams, hre) => { + console.log("\nRunning deploy:univ3zap ..."); + const { hyperdriveDeploy, artifacts, viem, network } = hre; - if (network.name == "anvil") { - let tc = await viem.getTestClient(); - await tc.setBalance({ - value: parseEther("1"), - address: CREATE_X_FACTORY_DEPLOYER, - }); - let wc = await viem.getWalletClient(CREATE_X_FACTORY_DEPLOYER); - await wc.sendRawTransaction({ - serializedTransaction: CREATE_X_PRESIGNED_TRANSACTION, - }); - } + // If the network is anvil, we need to deploy the CREATEX factory if it + // hasn't been deployed before. + await deployCreateX(hre); - // Skip if the zap is already deployed - if (!!hyperdriveDeploy.deployments.byNameSafe(name)) { - console.log(`skipping ${name}, found existing deployment`); - return; - } + // Skip if the zap is already deployed + if (!!hyperdriveDeploy.deployments.byNameSafe(name)) { + console.log(`skipping ${name}, found existing deployment`); + return; + } - // Assemble the creation code by packing the contract's bytecode with - // its constructor arguments. - let artifact = artifacts.readArtifactSync("UniV3Zap"); - let deployData = encodeDeployData({ - abi: artifact.abi, - bytecode: artifact.bytecode, - args: [name, swapRouter, weth], - }); - let creationCode = encodePacked(["bytes"], [deployData]); + // Assemble the creation code by packing the contract's bytecode with + // its constructor arguments. + let artifact = artifacts.readArtifactSync("UniV3Zap"); + let deployData = encodeDeployData({ + abi: artifact.abi, + bytecode: artifact.bytecode, + args: [name, swapRouter, weth], + }); + let creationCode = encodePacked(["bytes"], [deployData]); - // Call the Create3 deployer to deploy the contract - // Note: No initialization data needed since we're using a constructor - let createXDeployer = await viem.getContractAt( - "IDeterministicDeployer", - CREATE_X_FACTORY, - ); - let tx = await createXDeployer.write.deployCreate3([ - UNI_V3_ZAP_SALT, - creationCode, - ]); - let pc = await viem.getPublicClient(); - let { logs } = await pc.waitForTransactionReceipt({ hash: tx }); - const decodedLog = decodeEventLog({ - abi: CREATEX_ABI, - topics: logs[1].topics, - data: logs[1].data, - }); - let deployedAddress = decodedLog.args.newContract; + // Call the Create3 deployer to deploy the contract + // Note: No initialization data needed since we're using a constructor + let createXDeployer = await viem.getContractAt( + "IDeterministicDeployer", + CREATE_X_FACTORY, + ); + let tx = await createXDeployer.write.deployCreate3([ + UNI_V3_ZAP_SALT, + creationCode, + ]); + let pc = await viem.getPublicClient(); + let { logs } = await pc.waitForTransactionReceipt({ hash: tx }); + const decodedLog = decodeEventLog({ + abi: CREATEX_ABI, + topics: logs[1].topics, + data: logs[1].data, + }); + let deployedAddress = decodedLog.args.newContract; - // Save the deployment - console.log(` - saving ${name}...`); - hyperdriveDeploy.deployments.add( - name, - "UniV3Zap", - deployedAddress as Address, - ); - }, -); + // Save the deployment + console.log(` - saving ${name}...`); + hyperdriveDeploy.deployments.add( + name, + "UniV3Zap", + deployedAddress as Address, + ); +});