Skip to content

Commit

Permalink
send zrc20
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 28, 2023
1 parent 347eb0c commit 60b14b7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 26 deletions.
1 change: 1 addition & 0 deletions helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./fees";
export * from "./pools";
export * from "./prepare";
export * from "./sendZETA";
export * from "./sendZRC20";
export * from "./tx";
79 changes: 79 additions & 0 deletions helpers/sendZRC20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ERC20_ABI from "@openzeppelin/contracts/build/contracts/ERC20.json";
import { getEndpoints } from "@zetachain/networks";
import { networks } from "@zetachain/networks";
import { getAddress } from "@zetachain/protocol-contracts";
import ZRC20 from "@zetachain/protocol-contracts/abi/zevm/ZRC20.sol/ZRC20.json";
import { ethers } from "ethers";
import fetch from "isomorphic-fetch";

export const sendZRC20 = async (
signer: any,
amount: string,
network: string,
destination: string,
recipient: string,
token: string
) => {
let value;
try {
value = ethers.utils.parseEther(amount);
} catch (e) {
throw new Error(
`${value} is not a number and not a valid value for --amount, ${e}`
);
}

const API = getEndpoints("cosmos-http", "zeta_testnet")?.[0]?.url;
const response = await fetch(
`${API}/zeta-chain/zetacore/fungible/foreign_coins`
);
const data = await response.json();
const foreignCoins = data.foreignCoins;
const networkChainID = networks[network as keyof typeof networks]?.chain_id;
const foreignCoinsFiltered = foreignCoins.filter((coin: any) => {
return coin.foreign_chain_id === networkChainID.toString();
});
let tx;
if (network === "zeta_testnet") {
const ZRC20Address = getAddress("zrc20", destination as any);
const contract = new ethers.Contract(ZRC20Address, ZRC20.abi, signer);
await (await contract.connect(signer).approve(ZRC20Address, value)).wait();
tx = await contract.connect(signer).withdraw(signer.address, value);
return tx;
} else if (destination === "zeta_testnet") {
const TSSAddress = getAddress("tss", network as any);
const zrc20 = foreignCoinsFiltered.find(
(coin: any) =>
coin.symbol.toLocaleLowerCase() === token.toLocaleLowerCase()
);
if (zrc20.coin_type.toLocaleLowerCase() === "erc20") {
if (zrc20 === undefined) {
throw new Error(
`Token ${token} is not one of the available tokens to be deposited from ${network} to zeta_testnet`
);
}
const erc20ContractAddress = zrc20.asset;
const erc20TokenContract = new ethers.Contract(
erc20ContractAddress,
ERC20_ABI.abi,
signer
);
const balance = await erc20TokenContract.balanceOf(signer.address);
console.log(ethers.utils.formatEther(balance));
if (balance.lt(value)) {
throw new Error("Insufficient token balance.");
}
const approveTx = await erc20TokenContract.approve(TSSAddress, value);
await approveTx.wait();
tx = await erc20TokenContract.transfer(TSSAddress, value);
} else if (zrc20.coin_type.toLocaleLowerCase() === "gas") {
tx = await signer.sendTransaction({
to: TSSAddress,
value,
});
}
return tx;
} else {
throw new Error("Either --network or --destination should be zeta_testnet");
}
};
38 changes: 12 additions & 26 deletions tasks/sendZRC20.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
import { getAddress } from "@zetachain/protocol-contracts";
import ZRC20 from "@zetachain/protocol-contracts/abi/zevm/ZRC20.sol/ZRC20.json";
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import { sendZRC20 } from "../helpers/sendZRC20";

declare const hre: any;

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const { ethers } = hre as any;
const [signer] = await ethers.getSigners();
let amount;
try {
amount = ethers.utils.parseEther(args.amount);
} catch (e) {
throw new Error(
`${args.amount} is not a number and not a valid value for --amount, ${e}`
);
}
let tx;
if (hre.network.name === "zeta_testnet") {
const ZRC20Address = getAddress("zrc20", args.destination);
const contract = new ethers.Contract(ZRC20Address, ZRC20.abi, signer);
await (await contract.connect(signer).approve(ZRC20Address, amount)).wait();
tx = await contract.connect(signer).withdraw(signer.address, amount);
} else if (args.destination === "zeta_testnet") {
const TSSAddress = getAddress("tss", hre.network.name);
tx = await signer.sendTransaction({
to: TSSAddress,
value: amount,
});
} else {
throw new Error("Either --network or --destination should be zeta_testnet");
}
const tx = (await sendZRC20(
signer,
args.amount,
hre.network.name,
args.destination,
args.recipient,
args.token
)) as any;
console.log(`Transaction hash: ${tx.hash}`);
};

Expand All @@ -40,4 +25,5 @@ export const sendZRC20Task = task(
main
)
.addParam("amount", "Amount of ZRC-20 to send")
.addParam("destination", "Destination chain");
.addParam("destination", "Destination chain")
.addParam("token", "Token to send (geth, usdc, etc)");

0 comments on commit 60b14b7

Please sign in to comment.