Skip to content

Commit 0e8ce83

Browse files
committed
Add deterministic deployment
1 parent 88d15d0 commit 0e8ce83

File tree

7 files changed

+1177
-354
lines changed

7 files changed

+1177
-354
lines changed

contracts/interfaces/ICreateX.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.28;
3+
4+
interface ICreateX {
5+
struct Values {
6+
uint256 constructorAmount;
7+
uint256 initCallAmount;
8+
}
9+
10+
function deployCreate2AndInit(
11+
bytes32 salt_,
12+
bytes memory initCode_,
13+
bytes memory data_,
14+
Values memory values_
15+
) external payable returns (address newContract_);
16+
17+
function computeCreate2Address(
18+
bytes32 salt_,
19+
bytes32 initCodeHash_
20+
) external view returns (address computedAddress_);
21+
}

deploy/1_SPVGateway.migration.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { Deployer, Reporter } from "@solarity/hardhat-migrate";
22

3-
import { SPVGateway__factory } from "@ethers-v6";
3+
import { SPVGateway__factory, ICreateX__factory } from "@ethers-v6";
44

55
import { getConfig } from "./config/config";
6+
import { getSPVGatewayAddr, getSPVGatewaySalt } from "./helpers/helpers";
67

78
export = async (deployer: Deployer) => {
8-
const config = (await getConfig())!;
9+
const config = await getConfig();
910

10-
const spvGateway = await deployer.deploy(SPVGateway__factory);
11+
const createXDeployer = await deployer.deployed(ICreateX__factory, "0xba5Ed099633D3B313e4D5F7bdc1305d3c28ba5Ed");
1112

12-
await spvGateway["__SPVGateway_init(bytes,uint256,uint256)"](
13-
config.blockHeader,
14-
config.blockHeight,
15-
config.cumulativeWork,
13+
const salt = getSPVGatewaySalt();
14+
const initCalldata = SPVGateway__factory.createInterface().encodeFunctionData(
15+
"__SPVGateway_init(bytes,uint64,uint256)",
16+
[config.blockHeader, config.blockHeight, config.cumulativeWork],
1617
);
1718

18-
Reporter.reportContracts(["SPVGateway", await spvGateway.getAddress()]);
19+
await createXDeployer.deployCreate2AndInit(salt, SPVGateway__factory.bytecode, initCalldata, {
20+
constructorAmount: 0n,
21+
initCallAmount: 0n,
22+
});
23+
24+
const spvGatewayAddr = await getSPVGatewayAddr(createXDeployer);
25+
26+
Reporter.reportContracts(["SPVGateway", spvGatewayAddr]);
1927
};

deploy/config/sepolia.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DeployConfig } from "./types";
22

33
export const deployConfig: DeployConfig = {
44
blockHeader:
5-
"0x0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c",
6-
blockHeight: 0,
7-
cumulativeWork: 0,
5+
"0x010000006397bb6abd4fc521c0d3f6071b5650389f0b4551bc40b4e6b067306900000000ace470aecda9c8818c8fe57688cd2a772b5a57954a00df0420a7dd546b6d2c576b0e7f49ffff001d33f0192f",
6+
blockHeight: 2016,
7+
cumulativeWork: "0x000000000000000000000000000000000000000000000000000007e107e107e1",
88
};

deploy/helpers/helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SPVGateway__factory, ICreateX } from "@/generated-types/ethers";
2+
3+
import { ethers } from "hardhat";
4+
5+
export async function getSPVGatewayAddr(createXDeployer: ICreateX): Promise<string> {
6+
const salt = getSPVGatewaySalt();
7+
const guardedSalt = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(["bytes32"], [salt]));
8+
9+
const initcodeHash = ethers.keccak256(SPVGateway__factory.bytecode);
10+
11+
return await createXDeployer.computeCreate2Address(guardedSalt, initcodeHash);
12+
}
13+
14+
export function getSPVGatewaySalt(): string {
15+
return `0x0000000000000000000000000000000000000000000012341234123412341231`;
16+
}

hardhat.config.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ const config: HardhatUserConfig = {
5050
},
5151
},
5252
etherscan: {
53-
apiKey: {
54-
sepolia: `${process.env.ETHERSCAN_KEY}`,
55-
mainnet: `${process.env.ETHERSCAN_KEY}`,
56-
bscTestnet: `${process.env.BSCSCAN_KEY}`,
57-
bsc: `${process.env.BSCSCAN_KEY}`,
58-
polygon: `${process.env.POLYGONSCAN_KEY}`,
59-
avalancheFujiTestnet: `${process.env.AVALANCHE_KEY}`,
60-
avalanche: `${process.env.AVALANCHE_KEY}`,
61-
},
53+
apiKey: `${process.env.ETHERSCAN_KEY}`,
6254
},
6355
migrate: {
6456
paths: {

0 commit comments

Comments
 (0)