diff --git a/packages/zevm-app-contracts/contracts/withdrawErc20/withdrawErc20.sol b/packages/zevm-app-contracts/contracts/withdrawErc20/withdrawErc20.sol index ab84e65e..9342097a 100644 --- a/packages/zevm-app-contracts/contracts/withdrawErc20/withdrawErc20.sol +++ b/packages/zevm-app-contracts/contracts/withdrawErc20/withdrawErc20.sol @@ -1,11 +1,13 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.7; +import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol"; import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol"; import "@zetachain/toolkit/contracts/SwapHelperLib.sol"; contract WithdrawERC20 { + uint16 internal constant MAX_DEADLINE = 200; SystemContract public immutable systemContract; error InsufficientInputAmount(); @@ -14,14 +16,38 @@ contract WithdrawERC20 { systemContract = SystemContract(systemContractAddress); } + function swapTokensForExactTokens( + address zetaToken, + address uniswapV2Router, + address zrc20, + uint256 amount, + address targetZRC20, + uint256 amountInMax + ) internal returns (uint256) { + address[] memory path; + path = new address[](3); + path[0] = zrc20; + path[1] = zetaToken; + path[2] = targetZRC20; + + IZRC20(zrc20).approve(address(uniswapV2Router), amountInMax); + uint256[] memory amounts = IUniswapV2Router01(uniswapV2Router).swapTokensForExactTokens( + amount, + amountInMax, + path, + address(this), + block.timestamp + MAX_DEADLINE + ); + return amounts[0]; + } + function withdraw(address zrc20, uint256 amount, bytes memory to) external virtual { IZRC20(zrc20).transferFrom(msg.sender, address(this), amount); (address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee(); - uint256 inputForGas = SwapHelperLib.swapTokensForExactTokens( + uint256 inputForGas = swapTokensForExactTokens( systemContract.wZetaContractAddress(), - systemContract.uniswapv2FactoryAddress(), systemContract.uniswapv2Router02Address(), zrc20, gasFee, diff --git a/packages/zevm-app-contracts/data/addresses.json b/packages/zevm-app-contracts/data/addresses.json index 83d5c063..422c919a 100644 --- a/packages/zevm-app-contracts/data/addresses.json +++ b/packages/zevm-app-contracts/data/addresses.json @@ -6,7 +6,15 @@ "zetaSwap": "0xA8168Dc495Ed61E70f5c1941e2860050AB902cEF", "zetaSwapBtcInbound": "0x358E2cfC0E16444Ba7D3164Bbeeb6bEA7472c559", "invitationManager": "0x3649C03C472B698213926543456E9c21081e529d", - "withdrawERC20": "0xa349B9367cc54b47CAb8D09A95836AE8b4D1d84E" + "withdrawERC20": "0x1E8Dc7529FB4CD8711dd846bb59E6c2547a37Bf5" + }, + "zeta_mainnet": { + "disperse": "", + "rewardDistributorFactory": "", + "zetaSwap": "", + "zetaSwapBtcInbound": "", + "invitationManager": "", + "withdrawERC20": "0xe074D5AA7322D6701fAc29743f833A32312eB4Eb" } } } \ No newline at end of file diff --git a/packages/zevm-app-contracts/scripts/address.helpers.ts b/packages/zevm-app-contracts/scripts/address.helpers.ts index d43bdaac..94aa36c3 100644 --- a/packages/zevm-app-contracts/scripts/address.helpers.ts +++ b/packages/zevm-app-contracts/scripts/address.helpers.ts @@ -1,13 +1,18 @@ import { networks } from "@zetachain/networks"; -import { ZetaProtocolNetwork } from "@zetachain/protocol-contracts"; +import { isProtocolNetworkName, isTestnetNetwork, ZetaProtocolNetwork } from "@zetachain/protocol-contracts"; import protocolAddresses from "@zetachain/protocol-contracts/dist/data/addresses.json"; import { readFileSync, writeFileSync } from "fs"; import { join } from "path"; import addresses from "../data/addresses.json"; -export const getZEVMAppAddress = (address: string): string => { - return (addresses["zevm"] as any)["zeta_testnet"][address]; +export const getZEVMAppAddress = (address: string, networkName: string): string => { + if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); + //@ts-ignore + const isTestnet = isTestnetNetwork(networkName); + const zetaChain = isTestnet ? "zeta_testnet" : "zeta_mainnet"; + + return (addresses["zevm"] as any)[zetaChain][address]; }; export const getChainId = (network: ZetaProtocolNetwork): number => { @@ -20,8 +25,14 @@ export const getGasSymbolByNetwork = (network: ZetaProtocolNetwork): number => { return networks[network].gas_symbol; }; -export const getSystemContractAddress = () => { - return protocolAddresses["zevm"]["zeta_testnet"].systemContract; +export const getSystemContractAddress = (networkName: string) => { + if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); + + //@ts-ignore + const isTestnet = isTestnetNetwork(networkName); + const zetaChain = isTestnet ? "zeta_testnet" : "zeta_mainnet"; + + return protocolAddresses["zevm"][zetaChain].systemContract; }; export const saveAddress = (name: string, address: string, networkName: ZetaProtocolNetwork) => { diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/add-reward.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/add-reward.ts index aee56866..34d08f88 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/add-reward.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/add-reward.ts @@ -17,7 +17,7 @@ const main = async () => { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); - const systemContractAddress = getSystemContractAddress(); + const systemContractAddress = getSystemContractAddress(networkName); const systemContract = await SystemContract__factory.connect(systemContractAddress, deployer); await addReward(deployer, systemContract, REWARD_CONTRACT_ADDRESS, REWARD_DURATION, REWARDS_AMOUNT); diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-default-rewards.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-default-rewards.ts index 2b4feb22..a02fe151 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-default-rewards.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-default-rewards.ts @@ -15,10 +15,10 @@ const main = async () => { const networkName = network.name; if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); - const systemContractAddress = getSystemContractAddress(); + const systemContractAddress = getSystemContractAddress(networkName); const systemContract = await SystemContract__factory.connect(systemContractAddress, deployer); - const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory"); + const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory", networkName); const rewardDistributorFactory = RewardDistributorFactory__factory.connect(factoryContractAddress, deployer); let rewardContractAddress = ""; diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-token-reward.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-token-reward.ts index 2c777db4..2dcfe48c 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-token-reward.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-token-reward.ts @@ -17,10 +17,10 @@ const main = async () => { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); - const systemContractAddress = getSystemContractAddress(); + const systemContractAddress = getSystemContractAddress(networkName); const systemContract = await SystemContract__factory.connect(systemContractAddress, deployer); - const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory"); + const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory", networkName); const rewardDistributorFactory = RewardDistributorFactory__factory.connect(factoryContractAddress, deployer); diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-zrc20-reward.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-zrc20-reward.ts index ef8ff3db..a6b51594 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-zrc20-reward.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy-zrc20-reward.ts @@ -17,10 +17,10 @@ const main = async () => { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); - const systemContractAddress = getSystemContractAddress(); + const systemContractAddress = getSystemContractAddress(networkName); const systemContract = await SystemContract__factory.connect(systemContractAddress, deployer); - const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory"); + const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory", networkName); const rewardDistributorFactory = RewardDistributorFactory__factory.connect(factoryContractAddress, deployer); diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy.ts index e53b0d1b..aea7c784 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/deploy.ts @@ -6,7 +6,7 @@ import { getSystemContractAddress, saveAddress } from "../address.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); async function main() { const [deployer] = await ethers.getSigners(); diff --git a/packages/zevm-app-contracts/scripts/liquidity-incentives/read-rewards-data.ts b/packages/zevm-app-contracts/scripts/liquidity-incentives/read-rewards-data.ts index 78fccf1f..a8c74ad6 100644 --- a/packages/zevm-app-contracts/scripts/liquidity-incentives/read-rewards-data.ts +++ b/packages/zevm-app-contracts/scripts/liquidity-incentives/read-rewards-data.ts @@ -39,7 +39,7 @@ async function main() { const [deployer] = await ethers.getSigners(); if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); - const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory"); + const factoryContractAddress = getZEVMAppAddress("rewardDistributorFactory", networkName); const rewardDistributorFactory = RewardDistributorFactory__factory.connect(factoryContractAddress, deployer); const incentivesContractsLen = await rewardDistributorFactory.incentivesContractsLen(); diff --git a/packages/zevm-app-contracts/scripts/uniswap/add-liquidity-zeta-uniswap.ts b/packages/zevm-app-contracts/scripts/uniswap/add-liquidity-zeta-uniswap.ts index ecc9cfee..980ffcb9 100644 --- a/packages/zevm-app-contracts/scripts/uniswap/add-liquidity-zeta-uniswap.ts +++ b/packages/zevm-app-contracts/scripts/uniswap/add-liquidity-zeta-uniswap.ts @@ -19,7 +19,7 @@ import { getNow, printReserves, sortPair } from "./uniswap.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); const BTC_TO_ADD = parseUnits("0", 8); const ETH_TO_ADD = parseUnits("0"); diff --git a/packages/zevm-app-contracts/scripts/uniswap/remove-liquidity-zeta-uniswap.ts b/packages/zevm-app-contracts/scripts/uniswap/remove-liquidity-zeta-uniswap.ts index 3aa378c6..c32d44fe 100644 --- a/packages/zevm-app-contracts/scripts/uniswap/remove-liquidity-zeta-uniswap.ts +++ b/packages/zevm-app-contracts/scripts/uniswap/remove-liquidity-zeta-uniswap.ts @@ -19,7 +19,7 @@ import { getChainId, getSystemContractAddress } from "../address.helpers"; import { getNow, printReserves, sortPair } from "./uniswap.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); const removeTokenEthLiquidity = async ( tokenContract: ERC20, diff --git a/packages/zevm-app-contracts/scripts/uniswap/sell-token.ts b/packages/zevm-app-contracts/scripts/uniswap/sell-token.ts index 17097847..166687f1 100644 --- a/packages/zevm-app-contracts/scripts/uniswap/sell-token.ts +++ b/packages/zevm-app-contracts/scripts/uniswap/sell-token.ts @@ -16,7 +16,7 @@ import { getChainId, getSystemContractAddress } from "../address.helpers"; import { getNow, printReserves } from "./uniswap.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); const BTC_TO_SELL = parseUnits("0", 8); const ETH_TO_SELL = parseUnits("0"); diff --git a/packages/zevm-app-contracts/scripts/uniswap/sell-zeta.ts b/packages/zevm-app-contracts/scripts/uniswap/sell-zeta.ts index 0a3b4a52..a3ba3c23 100644 --- a/packages/zevm-app-contracts/scripts/uniswap/sell-zeta.ts +++ b/packages/zevm-app-contracts/scripts/uniswap/sell-zeta.ts @@ -15,7 +15,7 @@ import { getChainId, getSystemContractAddress } from "../address.helpers"; import { getNow, printReserves } from "./uniswap.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); const ZETA_TO_SELL = parseUnits("0.001"); diff --git a/packages/zevm-app-contracts/scripts/withdrawERC20/deploy.ts b/packages/zevm-app-contracts/scripts/withdrawERC20/deploy.ts index 2680458a..84ba6b55 100644 --- a/packages/zevm-app-contracts/scripts/withdrawERC20/deploy.ts +++ b/packages/zevm-app-contracts/scripts/withdrawERC20/deploy.ts @@ -1,4 +1,5 @@ import { isProtocolNetworkName } from "@zetachain/protocol-contracts"; +import { BigNumber } from "ethers"; import { ethers, network } from "hardhat"; import { WithdrawERC20__factory } from "../../typechain-types/factories/contracts/withdrawErc20/withdrawErc20.sol"; @@ -6,7 +7,7 @@ import { getSystemContractAddress, saveAddress } from "../address.helpers"; const networkName = network.name; -const SYSTEM_CONTRACT = getSystemContractAddress(); +const SYSTEM_CONTRACT = getSystemContractAddress(networkName); async function main() { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); diff --git a/packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts b/packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts index a74413b4..38634cce 100644 --- a/packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts +++ b/packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts @@ -7,14 +7,14 @@ import { getZEVMAppAddress } from "../address.helpers"; const networkName = network.name; -const ZUSDC_ADDRESS = "0x0cbe0dF132a6c6B4a2974Fa1b7Fb953CF0Cc798a"; -const AMOUNT = ethers.utils.parseUnits("0.5", 6); +const ZUSDC_ADDRESS = "0x05BA149A7bd6dC1F937fA9046A9e05C05f3b18b0"; +const AMOUNT = ethers.utils.parseUnits("0.5", 18); async function main() { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); const [signer] = await ethers.getSigners(); - const withdrawERC20Address = getZEVMAppAddress("withdrawERC20"); + const withdrawERC20Address = getZEVMAppAddress("withdrawERC20", networkName); const WithdrawERC20Factory = (await ethers.getContractFactory("WithdrawERC20")) as WithdrawERC20__factory; const WithdrawERC20 = WithdrawERC20Factory.attach(withdrawERC20Address); @@ -22,7 +22,14 @@ async function main() { const ERC20Factory = (await ethers.getContractFactory("ERC20")) as ERC20__factory; const erc20 = ERC20Factory.attach(ZUSDC_ADDRESS); - await erc20.approve(WithdrawERC20.address, AMOUNT); + const balance = await erc20.balanceOf(signer.address); + + if (balance.lt(AMOUNT)) { + console.log(`Not enough balance to withdraw ${AMOUNT}`); + process.exit(1); + } + const t1 = await erc20.approve(WithdrawERC20.address, AMOUNT); + await t1.wait(); const tx = await WithdrawERC20.withdraw(erc20.address, AMOUNT, signer.address); console.log(`Sending transaction ${tx.hash}`); } diff --git a/packages/zevm-app-contracts/scripts/zeta-swap/deploy.ts b/packages/zevm-app-contracts/scripts/zeta-swap/deploy.ts index 85e7affe..d8f4a548 100644 --- a/packages/zevm-app-contracts/scripts/zeta-swap/deploy.ts +++ b/packages/zevm-app-contracts/scripts/zeta-swap/deploy.ts @@ -10,7 +10,7 @@ const main = async () => { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); console.log(`Deploying ZetaSwap...`); - const SYSTEM_CONTRACT = getSystemContractAddress(); + const SYSTEM_CONTRACT = getSystemContractAddress(networkName); const Factory = (await ethers.getContractFactory("ZetaSwap")) as ZetaSwap__factory; const contract = (await Factory.deploy(SYSTEM_CONTRACT)) as ZetaSwap; diff --git a/packages/zevm-app-contracts/scripts/zeta-swap/get-system-data.ts b/packages/zevm-app-contracts/scripts/zeta-swap/get-system-data.ts index ccc303cc..a962caa5 100644 --- a/packages/zevm-app-contracts/scripts/zeta-swap/get-system-data.ts +++ b/packages/zevm-app-contracts/scripts/zeta-swap/get-system-data.ts @@ -1,9 +1,11 @@ import { ZetaProtocolNetwork } from "@zetachain/protocol-contracts"; -import { ethers } from "hardhat"; +import { ethers, network } from "hardhat"; import { SystemContract, SystemContract__factory } from "../../typechain-types"; import { getChainId, getGasSymbolByNetwork, getSystemContractAddress } from "../address.helpers"; +const networkName = network.name; + const getZRC20Address = async (systemContract: SystemContract, network: ZetaProtocolNetwork) => { const tokenAddress = await systemContract.gasCoinZRC20ByChainId(getChainId(network)); console.log(`${getGasSymbolByNetwork(network)}`, tokenAddress); @@ -13,7 +15,7 @@ const getZRC20Address = async (systemContract: SystemContract, network: ZetaProt async function main() { const [deployer] = await ethers.getSigners(); - const SYSTEM_CONTRACT = getSystemContractAddress(); + const SYSTEM_CONTRACT = getSystemContractAddress(networkName); console.log(`SYSTEM CONTRACT:`, SYSTEM_CONTRACT); const systemContract = await SystemContract__factory.connect(SYSTEM_CONTRACT, deployer); diff --git a/packages/zevm-app-contracts/scripts/zeta-swap/stress-swap.ts b/packages/zevm-app-contracts/scripts/zeta-swap/stress-swap.ts index 6ea9127b..1afecbb3 100644 --- a/packages/zevm-app-contracts/scripts/zeta-swap/stress-swap.ts +++ b/packages/zevm-app-contracts/scripts/zeta-swap/stress-swap.ts @@ -65,7 +65,7 @@ const main = async () => { const [signer] = await ethers.getSigners(); - const zetaSwapAddress = getZEVMAppAddress("zetaSwap"); + const zetaSwapAddress = getZEVMAppAddress("zetaSwap", networkName); const tssAddress = getAddress("tss", swappableNetwork); diff --git a/packages/zevm-app-contracts/scripts/zeta-swap/swap-on-zevm.ts b/packages/zevm-app-contracts/scripts/zeta-swap/swap-on-zevm.ts index 82e9ed29..0b8e29e3 100644 --- a/packages/zevm-app-contracts/scripts/zeta-swap/swap-on-zevm.ts +++ b/packages/zevm-app-contracts/scripts/zeta-swap/swap-on-zevm.ts @@ -16,7 +16,7 @@ const main = async () => { if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name"); const [signer] = await ethers.getSigners(); - const zetaSwap = getZEVMAppAddress(USE_BTC_SWAP ? "zetaSwapBtcInbound" : "zetaSwap"); + const zetaSwap = getZEVMAppAddress(USE_BTC_SWAP ? "zetaSwapBtcInbound" : "zetaSwap", networkName); const amount = parseUnits("0.00001", 8); const sourceToken = getZRC20Address("btc_testnet"); diff --git a/packages/zevm-app-contracts/scripts/zeta-swap/swap.ts b/packages/zevm-app-contracts/scripts/zeta-swap/swap.ts index 2a5ced1b..666977b8 100644 --- a/packages/zevm-app-contracts/scripts/zeta-swap/swap.ts +++ b/packages/zevm-app-contracts/scripts/zeta-swap/swap.ts @@ -6,6 +6,8 @@ import { ethers, network } from "hardhat"; import { getZEVMAppAddress } from "../address.helpers"; import { getSwapData } from "./helpers"; +const networkName = network.name; + const main = async () => { if (!isProtocolNetworkName(network.name)) throw new Error("Invalid network name"); @@ -16,7 +18,7 @@ const main = async () => { const [signer] = await ethers.getSigners(); - const zetaSwapAddress = getZEVMAppAddress("zetaSwap"); + const zetaSwapAddress = getZEVMAppAddress("zetaSwap", networkName); const tssAddress = getAddress("tss", network.name);