Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jan 23, 2024
1 parent 8c4edb8 commit 145f501
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 58 deletions.
4 changes: 4 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
networks: {
...getHardhatConfigNetworks(),
zeta_testnet: {
...getHardhatConfigNetworks()["zeta_testnet"],
url: "https://zetachain-testnet-evm.itrocket.net",
},
},
solidity: {
compilers: [
Expand Down
131 changes: 73 additions & 58 deletions helpers/sendZRC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,65 @@ import ZRC20 from "@zetachain/protocol-contracts/abi/zevm/ZRC20.sol/ZRC20.json";
import { ethers } from "ethers";
import { getForeignCoins } from "../helpers/balances";
import { getChainId } from "@zetachain/networks";
import { fetchFees } from "../helpers/fees";

export const withdraw = async ({
signer,
amount,
to,
zrc20,
}: {
signer: any;
amount: string;

Check failure on line 16 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'amount' should be before 'signer'
to: any;
zrc20: string;
}) => {
const targetContract = new ethers.Contract(zrc20, ZRC20.abi, signer);
const targetDecimals = await targetContract.decimals();

const [gasAddress, gasFee] = await targetContract.withdrawGasFee();
const gasContract = new ethers.Contract(gasAddress, ZRC20.abi, signer);

const targetValue = ethers.utils.parseUnits(amount, targetDecimals);
await (await gasContract.connect(signer).approve(gasAddress, gasFee)).wait();
return await targetContract.connect(signer).withdraw(to, targetValue);
};

export const deposit = async ({
signer,
amount,
to,
erc20,
message,
}: {
signer: any;
amount: string;

Check failure on line 39 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'amount' should be before 'signer'
to: string;
erc20?: string;

Check failure on line 41 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'erc20' should be before 'to'
message?: string;
}) => {
const { chainId } = signer.provider.network;
const chain = Object.entries(networks).find(
(x) => x[1].chain_id === chainId
)?.[0];
const tss = getAddress("tss", chain as any);
if (erc20) {
const contract = new ethers.Contract(erc20, ERC20_ABI.abi, signer);
const balance = await contract.balanceOf(signer.address);
if (balance.lt(amount)) {
throw new Error("Insufficient token balance.");
}
const decimals = await contract.decimals();
const value = ethers.utils.parseUnits(amount, decimals);
const approveTx = await contract.approve(tss, value);
return await approveTx.wait();
} else {
return await signer.sendTransaction({
to: tss,
value: ethers.utils.parseUnits(amount, 18),
data: `${to}${message ?? ""}`,

Check failure on line 63 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'data' should be before 'value'
});
}
};

export const sendZRC20 = async (
signer: any,
Expand All @@ -25,68 +83,25 @@ export const sendZRC20 = async (
}

const foreignCoins = await getForeignCoins();
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;
const counterparty = destination === "zeta_testnet" ? network : destination;
const chainId =
destination === "btc_testnet" ? 18332 : getChainId(counterparty); // https://github.com/zeta-chain/networks/pull/34/
const { zrc20_contract_address: zrc20, asset: erc20 } = foreignCoins.find(
(c: any) =>
parseInt(c.foreign_chain_id) === chainId &&
c.symbol.toLocaleLowerCase() === token.toLocaleLowerCase()
);
console.log("recipient", recipient);
console.log("to utf8", ethers.utils.toUtf8Bytes(recipient));
if (network === "zeta_testnet") {
const { zrc20_contract_address } = foreignCoins.find(
(c: any) =>
parseInt(c.foreign_chain_id) === getChainId(destination) &&
c.symbol.toLocaleLowerCase() === token.toLocaleLowerCase()
);
const targetContract = new ethers.Contract(
zrc20_contract_address,
ZRC20.abi,
signer
);
const targetDecimals = await targetContract.decimals();
const to =
destination === "btc_testnet"
? ethers.utils.toUtf8Bytes(recipient)
: signer.address;

const [gasAddress, gasFee] = await targetContract.withdrawGasFee();
const gasContract = new ethers.Contract(gasAddress, ZRC20.abi, signer);

const targetValue = ethers.utils.parseUnits(amount, targetDecimals);
await (
await gasContract.connect(signer).approve(gasAddress, gasFee)
).wait();
return await targetContract.connect(signer).withdraw(to, targetValue);
: recipient;
return await withdraw({ signer, amount, to, zrc20 });

Check failure on line 101 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'amount' should be before 'signer'
} else if (destination === "zeta_testnet") {
const TSSAddress = getAddress("tss", network as any);
const zrc20 = foreignCoinsFiltered.find(
(coin: any) => coin.symbol.toLowerCase() === token.toLowerCase()
);
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);
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;
return await deposit({ signer, amount, to: recipient, erc20 });

Check failure on line 103 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'amount' should be before 'signer'

Check failure on line 103 in helpers/sendZRC20.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'erc20' should be before 'to'
} else {
throw new Error("Either --network or --destination should be zeta_testnet");
throw new Error("Either network or destination should be zeta_testnet");
}
};

0 comments on commit 145f501

Please sign in to comment.