From 742221c630e759710ff868e685bd649f8955a2ef Mon Sep 17 00:00:00 2001 From: Andres Aiello Date: Fri, 22 Dec 2023 13:11:12 -0300 Subject: [PATCH] feat: simplify protocol contracts deployment --- lib/contracts.constants.ts | 68 ++++++++++++++++--- lib/contracts.helpers.ts | 5 +- scripts/deployments/core/deploy.ts | 23 ++----- .../deterministic-deploy-erc20-custody.ts | 11 +-- .../deterministic-deploy-zeta-connector.ts | 11 ++- .../core/deterministic-deploy-zeta-token.ts | 14 ++-- 6 files changed, 80 insertions(+), 52 deletions(-) diff --git a/lib/contracts.constants.ts b/lib/contracts.constants.ts index 1ea515fd..9aba37d5 100644 --- a/lib/contracts.constants.ts +++ b/lib/contracts.constants.ts @@ -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"; diff --git a/lib/contracts.helpers.ts b/lib/contracts.helpers.ts index 565d5a78..5508789b 100644 --- a/lib/contracts.helpers.ts +++ b/lib/contracts.helpers.ts @@ -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 }) => { const Factory = (await ethers.getContractFactory("ZetaConnectorBase")) as ZetaConnectorBaseFactory; diff --git a/scripts/deployments/core/deploy.ts b/scripts/deployments/core/deploy.ts index c144016d..eb3bc268 100644 --- a/scripts/deployments/core/deploy.ts +++ b/scripts/deployments/core/deploy.ts @@ -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() diff --git a/scripts/deployments/core/deterministic-deploy-erc20-custody.ts b/scripts/deployments/core/deterministic-deploy-erc20-custody.ts index 75ddef57..c822c11d 100644 --- a/scripts/deployments/core/deterministic-deploy-erc20-custody.ts +++ b/scripts/deployments/core/deterministic-deploy-erc20-custody.ts @@ -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, @@ -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; @@ -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) { diff --git a/scripts/deployments/core/deterministic-deploy-zeta-connector.ts b/scripts/deployments/core/deterministic-deploy-zeta-connector.ts index 7918ef3b..aacab938 100644 --- a/scripts/deployments/core/deterministic-deploy-zeta-connector.ts +++ b/scripts/deployments/core/deterministic-deploy-zeta-connector.ts @@ -2,7 +2,7 @@ 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, @@ -10,7 +10,7 @@ import { } 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.`); } @@ -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); @@ -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() diff --git a/scripts/deployments/core/deterministic-deploy-zeta-token.ts b/scripts/deployments/core/deterministic-deploy-zeta-token.ts index f55284af..ad6692ef 100644 --- a/scripts/deployments/core/deterministic-deploy-zeta-token.ts +++ b/scripts/deployments/core/deterministic-deploy-zeta-token.ts @@ -2,11 +2,7 @@ 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, @@ -14,7 +10,7 @@ import { } 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.`); } @@ -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); @@ -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()