Skip to content

Commit

Permalink
Addressed review feedback from John McClure
Browse files Browse the repository at this point in the history
  • Loading branch information
jalextowle committed Dec 6, 2024
1 parent ebbf3b7 commit a76d7e2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 161 deletions.
4 changes: 0 additions & 4 deletions hardhat.config.mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 7 additions & 25 deletions tasks/deploy/hyperdrive-matching-engine.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 30 additions & 1 deletion tasks/deploy/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.
*/
Expand Down
114 changes: 50 additions & 64 deletions tasks/deploy/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
);
});
115 changes: 48 additions & 67 deletions tasks/deploy/uni-v3-zap.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
);
});

0 comments on commit a76d7e2

Please sign in to comment.