Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deploy updated contracts on mainnet for erc-20 #163

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion packages/zevm-app-contracts/data/addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
"zetaSwap": "0xA8168Dc495Ed61E70f5c1941e2860050AB902cEF",
"zetaSwapBtcInbound": "0x358E2cfC0E16444Ba7D3164Bbeeb6bEA7472c559",
"invitationManager": "0x3649C03C472B698213926543456E9c21081e529d",
"withdrawERC20": "0xa349B9367cc54b47CAb8D09A95836AE8b4D1d84E"
"withdrawERC20": "0x1E8Dc7529FB4CD8711dd846bb59E6c2547a37Bf5"
},
"zeta_mainnet": {
"disperse": "",
"rewardDistributorFactory": "",
"zetaSwap": "",
"zetaSwapBtcInbound": "",
"invitationManager": "",
"withdrawERC20": "0xe074D5AA7322D6701fAc29743f833A32312eB4Eb"
}
}
}
21 changes: 16 additions & 5 deletions packages/zevm-app-contracts/scripts/address.helpers.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion packages/zevm-app-contracts/scripts/uniswap/sell-zeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";
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");
Expand Down
15 changes: 11 additions & 4 deletions packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ 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);

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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/zevm-app-contracts/scripts/zeta-swap/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion packages/zevm-app-contracts/scripts/zeta-swap/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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);

Expand Down
Loading