Skip to content

Commit

Permalink
feat: simplify protocol contracts deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Dec 22, 2023
1 parent fbf8687 commit 742221c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 52 deletions.
68 changes: 59 additions & 9 deletions lib/contracts.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,66 @@ export const ZETA_INITIAL_SUPPLY = 2_100_000_000;
export const MAX_ETH_ADDRESS = "0xffffffffffffffffffffffffffffffffffffffff";

// dev: this values should be calculated using get-salt script
export const ZETA_TOKEN_SALT_NUMBER_ETH = "84108";
export const ZETA_TOKEN_SALT_NUMBER_NON_ETH = "29265";
const SALT_NUMBERS = {
baobab_testnet: {
zetaConnector: "71733",
zetaConsumer: "0",
zetaERC20Custody: "195084",
zetaToken: "29265",
},
bsc_mainnet: {
zetaConnector: "71733",
zetaConsumer: "0",
zetaERC20Custody: "195084",
zetaToken: "29265",
},
bsc_testnet: {
zetaConnector: "71733",
zetaConsumer: "0",
zetaERC20Custody: "195084",
zetaToken: "29265",
},
btc_testnet: {
zetaConnector: "",
zetaConsumer: "",
zetaERC20Custody: "",
zetaToken: "",
},
eth_mainnet: {
zetaConnector: "1414",
zetaConsumer: "0",
zetaERC20Custody: "87967",
zetaToken: "84108",
},
goerli_testnet: {
zetaConnector: "1414",
zetaConsumer: "0",
zetaERC20Custody: "87967",
zetaToken: "84108",
},
mumbai_testnet: {
zetaConnector: "71733",
zetaConsumer: "0",
zetaERC20Custody: "195084",
zetaToken: "29265",
},
zeta_testnet: {
zetaConnector: "71733",
zetaConsumer: "0",
zetaERC20Custody: "195084",
zetaToken: "29265",
},
};

// dev: this values should be calculated using get-salt script
export const ZETA_CONNECTOR_SALT_NUMBER_ETH = "1414";
export const ZETA_CONNECTOR_SALT_NUMBER_NON_ETH = "71733";
export const getSaltNumber = (contractName: string, networkName: string) => {
const saltNumber = SALT_NUMBERS[networkName][contractName];

if (!saltNumber) {
throw new Error(`Salt number for ${contractName} on ${networkName} is not defined.`);
}

return saltNumber;
};

export const ERC20_CUSTODY_SALT_NUMBER_ETH = "87967";
export const ERC20_CUSTODY_SALT_NUMBER_NON_ETH = "195084";
export const ERC20_CUSTODY_ZETA_FEE = "0";
export const ERC20_CUSTODY_ZETA_MAX_FEE = parseEther("1000");

export const ZETA_CONSUMER_SALT_NUMBER = "0";
5 changes: 1 addition & 4 deletions lib/contracts.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import { BaseContract, ContractFactory } from "ethers";
import { ethers } from "hardhat";

export const isEthNetworkName = (networkName: string) =>
networkName === "eth-localnet" ||
networkName === "goerli_testnet" ||
networkName === "eth_mainnet" ||
networkName === "bsc_mainnet";
networkName === "eth-localnet" || networkName === "goerli_testnet" || networkName === "eth_mainnet";

export const deployZetaConnectorBase = async ({ args }: { args: Parameters<ZetaConnectorBaseFactory["deploy"]> }) => {
const Factory = (await ethers.getContractFactory("ZetaConnectorBase")) as ZetaConnectorBaseFactory;
Expand Down
23 changes: 6 additions & 17 deletions scripts/deployments/core/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import { network } from "hardhat";

import { isProtocolNetworkName } from "../../../lib/address.tools";
import { isEthNetworkName } from "../../../lib/contracts.helpers";
import { setZetaAddresses } from "../../tools/set-zeta-token-addresses";
import { deployZetaConnector } from "./deploy-zeta-connector";
import { deployZetaToken } from "./deploy-zeta-token";
import { deterministicDeployERC20Custody } from "./deterministic-deploy-erc20-custody";
import { deterministicDeployZetaConnector } from "./deterministic-deploy-zeta-connector";
import { deterministicDeployZetaToken } from "./deterministic-deploy-zeta-token";

const networkName = network.name;

async function main() {
if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name");

const zetaTokenAddress = await deployZetaToken();
const connectorAddress = await deployZetaConnector();

/**
* @description The Eth implementation of Zeta token doesn't need any address
*/
if (isEthNetworkName(network.name)) return;

/**
* @description Avoid setting Zeta addresses for local network,
* since it must be done after starting the local Zeta node
*/
await setZetaAddresses(connectorAddress, zetaTokenAddress);
await deterministicDeployZetaToken();
await deterministicDeployZetaConnector();
await deterministicDeployERC20Custody();
}

main()
Expand Down
11 changes: 3 additions & 8 deletions scripts/deployments/core/deterministic-deploy-erc20-custody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { BigNumber } from "ethers";
import { ethers, network } from "hardhat";
import { getAddress, isProtocolNetworkName } from "lib";

import {
ERC20_CUSTODY_SALT_NUMBER_ETH,
ERC20_CUSTODY_SALT_NUMBER_NON_ETH,
ERC20_CUSTODY_ZETA_FEE,
ERC20_CUSTODY_ZETA_MAX_FEE,
} from "../../../lib/contracts.constants";
import { isEthNetworkName } from "../../../lib/contracts.helpers";
import { ERC20_CUSTODY_ZETA_FEE, ERC20_CUSTODY_ZETA_MAX_FEE, getSaltNumber } from "../../../lib/contracts.constants";
import {
deployContractToAddress,
saltToHex,
Expand All @@ -30,7 +24,7 @@ export const deterministicDeployERC20Custody = async () => {
const tssUpdaterAddress = getAddress("tssUpdater", network.name);
const immutableCreate2FactoryAddress = getAddress("immutableCreate2Factory", network.name);

const saltNumber = isEthNetworkName(network.name) ? ERC20_CUSTODY_SALT_NUMBER_ETH : ERC20_CUSTODY_SALT_NUMBER_NON_ETH;
const saltNumber = getSaltNumber(network.name, "zetaERC20Custody");
const saltStr = BigNumber.from(saltNumber).toHexString();

const zetaFee = ERC20_CUSTODY_ZETA_FEE;
Expand All @@ -54,6 +48,7 @@ export const deterministicDeployERC20Custody = async () => {

console.log("Deployed ERC20 Custody. Address:", address);
console.log("Constructor Args", constructorArgs);
return address;
};

if (!process.env.EXECUTE_PROGRAMMATICALLY) {
Expand Down
11 changes: 5 additions & 6 deletions scripts/deployments/core/deterministic-deploy-zeta-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { BigNumber } from "ethers";
import { ethers, network } from "hardhat";
import { getAddress, isProtocolNetworkName } from "lib";

import { ZETA_CONNECTOR_SALT_NUMBER_ETH, ZETA_CONNECTOR_SALT_NUMBER_NON_ETH } from "../../../lib/contracts.constants";
import { getSaltNumber } from "../../../lib/contracts.constants";
import { isEthNetworkName } from "../../../lib/contracts.helpers";
import {
deployContractToAddress,
saltToHex,
} from "../../../lib/ImmutableCreate2Factory/ImmutableCreate2Factory.helpers";
import { ZetaConnectorEth__factory, ZetaConnectorNonEth__factory } from "../../../typechain-types";

export async function deterministicDeployZetaConnector() {
export const deterministicDeployZetaConnector = async () => {
if (!isProtocolNetworkName(network.name)) {
throw new Error(`network.name: ${network.name} isn't supported.`);
}
Expand All @@ -25,9 +25,7 @@ export async function deterministicDeployZetaConnector() {
const tssUpdaterAddress = getAddress("tssUpdater", network.name);
const immutableCreate2FactoryAddress = getAddress("immutableCreate2Factory", network.name);

const saltNumber = isEthNetworkName(network.name)
? ZETA_CONNECTOR_SALT_NUMBER_ETH
: ZETA_CONNECTOR_SALT_NUMBER_NON_ETH;
const saltNumber = getSaltNumber(network.name, "zetaConnector");
const saltStr = BigNumber.from(saltNumber).toHexString();

const salthex = saltToHex(saltStr, DEPLOYER_ADDRESS);
Expand All @@ -52,7 +50,8 @@ export async function deterministicDeployZetaConnector() {

console.log("Deployed ZetaConnector. Address:", address);
console.log("Constructor Args", constructorArgs);
}
return address;
};

if (!process.env.EXECUTE_PROGRAMMATICALLY) {
deterministicDeployZetaConnector()
Expand Down
14 changes: 6 additions & 8 deletions scripts/deployments/core/deterministic-deploy-zeta-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import { BigNumber } from "ethers";
import { ethers, network } from "hardhat";
import { getAddress, isProtocolNetworkName } from "lib";

import {
ZETA_INITIAL_SUPPLY,
ZETA_TOKEN_SALT_NUMBER_ETH,
ZETA_TOKEN_SALT_NUMBER_NON_ETH,
} from "../../../lib/contracts.constants";
import { getSaltNumber, ZETA_INITIAL_SUPPLY } from "../../../lib/contracts.constants";
import { isEthNetworkName } from "../../../lib/contracts.helpers";
import {
deployContractToAddress,
saltToHex,
} from "../../../lib/ImmutableCreate2Factory/ImmutableCreate2Factory.helpers";
import { ZetaEth__factory, ZetaNonEth__factory } from "../../../typechain-types";

export async function deterministicDeployZetaToken() {
export const deterministicDeployZetaToken = async () => {
if (!isProtocolNetworkName(network.name)) {
throw new Error(`network.name: ${network.name} isn't supported.`);
}
Expand All @@ -28,7 +24,7 @@ export async function deterministicDeployZetaToken() {
const tssUpdaterAddress = getAddress("tssUpdater", network.name);
const immutableCreate2FactoryAddress = getAddress("immutableCreate2Factory", network.name);

const saltNumber = isEthNetworkName(network.name) ? ZETA_TOKEN_SALT_NUMBER_ETH : ZETA_TOKEN_SALT_NUMBER_NON_ETH;
const saltNumber = getSaltNumber(network.name, "zetaToken");
const saltStr = BigNumber.from(saltNumber).toHexString();

const salthex = saltToHex(saltStr, DEPLOYER_ADDRESS);
Expand Down Expand Up @@ -59,7 +55,9 @@ export async function deterministicDeployZetaToken() {

console.log("Deployed zetaToken. Address:", address);
console.log("Constructor Args", constructorArgs);
}

return address;
};

if (!process.env.EXECUTE_PROGRAMMATICALLY) {
deterministicDeployZetaToken()
Expand Down

0 comments on commit 742221c

Please sign in to comment.