From bedf70a217f94d3d38ee8d4e34a610a14450ca46 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Fri, 13 Oct 2023 12:37:58 +0400 Subject: [PATCH] refactor fees --- helpers/evm.ts | 109 --------------------------------------------- helpers/fees.ts | 12 ++--- helpers/helpers.ts | 76 ------------------------------- helpers/index.ts | 3 +- package.json | 3 +- 5 files changed, 8 insertions(+), 195 deletions(-) delete mode 100644 helpers/evm.ts delete mode 100644 helpers/helpers.ts diff --git a/helpers/evm.ts b/helpers/evm.ts deleted file mode 100644 index b40102d7..00000000 --- a/helpers/evm.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { MaxUint256 } from "@ethersproject/constants"; -import { parseUnits } from "@ethersproject/units"; -import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; -import { ethers } from "ethers"; - -import { - TestSystemContract, - TestSystemContract__factory, - TestZRC20, - TestZRC20__factory, - UniswapV2Router02__factory, -} from "../typechain-types"; - -const addZetaEthLiquidity = async ( - signer: SignerWithAddress, - token: TestZRC20, - uniswapRouterAddr: string -) => { - const block = await hre.ethers.provider.getBlock("latest"); - - const tx1 = await token.approve(uniswapRouterAddr, MaxUint256); - await tx1.wait(); - - const uniswapRouterFork = UniswapV2Router02__factory.connect( - uniswapRouterAddr, - signer - ); - - const tx2 = await uniswapRouterFork.addLiquidityETH( - token.address, - parseUnits("1000"), - 0, - 0, - signer.address, - block.timestamp + 360, - { value: parseUnits("1000") } - ); - await tx2.wait(); -}; - -interface EvmSetupResult { - ZRC20Contracts: TestZRC20[]; - systemContract: TestSystemContract; -} - -export const evmSetup = async ( - wGasToken: string, - uniswapFactoryAddr: string, - uniswapRouterAddr: string -): Promise => { - const [signer] = await hre.ethers.getSigners(); - - const ZRC20Factory = (await hre.ethers.getContractFactory( - "TestZRC20" - )) as TestZRC20__factory; - - const token1Contract = (await ZRC20Factory.deploy( - parseUnits("1000000"), - "tBNB", - "tBNB" - )) as TestZRC20; - const token2Contract = (await ZRC20Factory.deploy( - parseUnits("1000000"), - "gETH", - "gETH" - )) as TestZRC20; - const token3Contract = (await ZRC20Factory.deploy( - parseUnits("1000000"), - "tMATIC", - "tMATIC" - )) as TestZRC20; - - const ZRC20Contracts = [token1Contract, token2Contract, token3Contract]; - - const SystemContractFactory = (await hre.ethers.getContractFactory( - "TestSystemContract" - )) as TestSystemContract__factory; - - const systemContract = (await SystemContractFactory.deploy( - wGasToken, - uniswapFactoryAddr, - uniswapRouterAddr - )) as TestSystemContract; - - await systemContract.setGasCoinZRC20(97, ZRC20Contracts[0].address); - await systemContract.setGasCoinZRC20(5, ZRC20Contracts[1].address); - await systemContract.setGasCoinZRC20(80001, ZRC20Contracts[2].address); - - await addZetaEthLiquidity(signer, ZRC20Contracts[0], uniswapRouterAddr); - await addZetaEthLiquidity(signer, ZRC20Contracts[1], uniswapRouterAddr); - await addZetaEthLiquidity(signer, ZRC20Contracts[2], uniswapRouterAddr); - - return { ZRC20Contracts, systemContract }; -}; - -export const prepareData = (contract: string, types: string[], args: any[]) => { - const params = prepareParams(types, args); - return `${contract}${params.slice(2)}`; -}; - -export const prepareParams = (types: string[], args: any[]) => { - const abiCoder = ethers.utils.defaultAbiCoder; - for (let i = 0; i < args.length; i++) { - if (types[i] === "bytes32") { - args[i] = ethers.utils.hexlify(ethers.utils.zeroPad(args[i], 32)); - } - } - return abiCoder.encode(types, args); -}; diff --git a/helpers/fees.ts b/helpers/fees.ts index 5a4bec99..ea2f219f 100644 --- a/helpers/fees.ts +++ b/helpers/fees.ts @@ -1,5 +1,5 @@ -import { getEndpoints } from "@zetachain/networks"; -import { getHardhatConfigNetworks } from "@zetachain/networks"; +import { getEndpoints } from "@zetachain/networks/dist/src/getEndpoints"; +import networks from "@zetachain/networks/dist/src/networks"; import { getAddress } from "@zetachain/protocol-contracts"; import ZRC20 from "@zetachain/protocol-contracts/abi/zevm/ZRC20.sol/ZRC20.json"; import axios from "axios"; @@ -9,7 +9,7 @@ import { formatEther } from "ethers/lib/utils"; const formatTo18Decimals = (n: any) => parseFloat(formatEther(n)).toFixed(18); export const fetchZEVMFees = async (network: string) => { - const { url } = getHardhatConfigNetworks()["zeta_testnet"] as any; + const url = getEndpoints("evm", "zeta_testnet")[0].url; const provider = new ethers.providers.JsonRpcProvider(url); const btcZRC20 = "0x65a45c57636f9BcCeD4fe193A602008578BcA90b"; // TODO: use getAddress("zrc20", "btc_testnet") when available @@ -37,7 +37,7 @@ export const fetchCCMFees = async (network: string, gas: Number = 500000) => { throw new Error("getEndpoints: API endpoint not found"); } - const chainID = getHardhatConfigNetworks()[network].chainId; + const chainID = networks[network as keyof typeof networks]?.chain_id; const url = `${API}/zeta-chain/crosschain/convertGasToZeta?chainId=${chainID}&gasLimit=${gas}`; const { data } = await axios.get(url); @@ -59,10 +59,10 @@ export const fetchFees = async (gas: Number) => { feesZEVM: {} as Record, }; - const networks = [...Object.keys(getHardhatConfigNetworks()), "btc_testnet"]; + const networkList = [...Object.keys(networks), "btc_testnet"]; await Promise.all( - networks.map(async (n) => { + networkList.map(async (n) => { try { const zevmFees = await fetchZEVMFees(n); if (zevmFees) fees.feesZEVM[n] = zevmFees; diff --git a/helpers/helpers.ts b/helpers/helpers.ts deleted file mode 100644 index 7e75f8f6..00000000 --- a/helpers/helpers.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { - ZetaEth, - ZetaEth__factory as ZetaEthFactory, -} from "@zetachain/protocol-contracts/dist/typechain-types"; -import { BaseContract, ContractFactory } from "ethers"; - -import { - ZetaConnectorMockValue, - ZetaConnectorMockValue__factory as ZetaConnectorMockValueFactory, -} from "../typechain-types"; - -declare const hre: any; - -export type GetContractParams = - | { - deployParams: Parameters; - existingContractAddress?: null; - } - | { - deployParams?: null; - existingContractAddress: string; - }; - -export const getContract = async < - Factory extends ContractFactory, - Contract extends BaseContract ->({ - contractName, - deployParams, - existingContractAddress, -}: GetContractParams & { - contractName: string; -}): Promise => { - const ContractFactory = (await hre.ethers.getContractFactory( - contractName - )) as Factory; - - const isGetExistingContract = Boolean(existingContractAddress); - if (isGetExistingContract) { - console.log( - "Getting existing contract from address:", - existingContractAddress - ); - return ContractFactory.attach(existingContractAddress!) as Contract; - } - - const contract = (await ContractFactory.deploy(...deployParams!)) as Contract; - await contract.deployed(); - - return contract; -}; - -export const deployZetaConnectorMock = async () => { - const Factory = (await hre.ethers.getContractFactory( - "ZetaConnectorMockValue" - )) as ZetaConnectorMockValueFactory; - - const zetaConnectorMockContract = - (await Factory.deploy()) as ZetaConnectorMockValue; - - await zetaConnectorMockContract.deployed(); - - return zetaConnectorMockContract; -}; - -export const deployZetaEthMock = async () => { - const Factory = (await hre.ethers.getContractFactory( - "ZetaEthMock" - )) as ZetaEthFactory; - - const zetaConnectorMockContract = (await Factory.deploy(100_000)) as ZetaEth; - - await zetaConnectorMockContract.deployed(); - - return zetaConnectorMockContract; -}; diff --git a/helpers/index.ts b/helpers/index.ts index fc267f52..6d92fad4 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -1,4 +1,3 @@ -export * from "./evm"; +export * from "./balances"; export * from "./fees"; -export * from "./helpers"; export * from "./tx"; diff --git a/package.json b/package.json index 1a0191de..6099cfaa 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,6 @@ "./tasks": "./dist/tasks/index.js", "./helpers": "./dist/helpers/index.js", "./helpers/fees": "./dist/helpers/fees.js", - "./helpers/evm": "./dist/helpers/evm.js", "./helpers/balances": "./dist/helpers/balances.js" }, "scripts": { @@ -95,4 +94,4 @@ "tiny-secp256k1": "^2.2.3", "ws": "^8.13.0" } -} \ No newline at end of file +}