From 41f5fde8195c0a63d546854f8a2f9c1f3cd0ae3d Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Mon, 16 Oct 2023 15:28:52 +0400 Subject: [PATCH] send zeta --- helpers/sendZETA.ts | 54 +++++++++++++++++++++++++++++++++++++++ tasks/sendZETA.ts | 62 ++++++++------------------------------------- 2 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 helpers/sendZETA.ts diff --git a/helpers/sendZETA.ts b/helpers/sendZETA.ts new file mode 100644 index 00000000..853df237 --- /dev/null +++ b/helpers/sendZETA.ts @@ -0,0 +1,54 @@ +import { getAddress } from "@zetachain/protocol-contracts"; +import ZetaEthContract from "@zetachain/protocol-contracts/abi/evm/Zeta.eth.sol/ZetaEth.json"; +import ZetaConnectorEth from "@zetachain/protocol-contracts/abi/evm/ZetaConnector.eth.sol/ZetaConnectorEth.json"; +import ZetaConnectorZEVM from "@zetachain/protocol-contracts/abi/zevm/ConnectorZEVM.sol/ZetaConnectorZEVM.json"; +import { ethers } from "ethers"; +import networks from "@zetachain/networks/dist/src/networks"; + +export const sendZETA = async ( + signer: any, + amount: string, + from: string, + destination: string, + recipient: string +) => { + let connectorContract: any; + const destinationChainId = networks[destination]?.chain_id; + if (!destinationChainId) { + throw new Error("Invalid destination chain"); + } + const destinationAddress = recipient || signer.address; + + const fromZetaChain = from === "zeta_testnet"; + + const connectorAddress = getAddress("connector", from as any); + const zetaTokenAddress = getAddress("zetaToken", from as any); + connectorContract = new ethers.Contract( + connectorAddress, + fromZetaChain ? ZetaConnectorZEVM.abi : ZetaConnectorEth.abi, + signer + ); + const zetaTokenContract = new ethers.Contract( + zetaTokenAddress, + ZetaEthContract.abi, + signer + ); + const value = ethers.utils.parseEther(amount); + + if (fromZetaChain) { + await signer.sendTransaction({ to: zetaTokenAddress, value }); + } + + await ( + await zetaTokenContract.connect(signer).approve(connectorAddress, value) + ).wait(); + + return await connectorContract.connect(signer).send({ + destinationAddress, + destinationChainId, + destinationGasLimit: 5000000, + message: ethers.utils.arrayify([]), + zetaParams: ethers.utils.arrayify([]), + zetaValueAndGas: value, + }); +}; diff --git a/tasks/sendZETA.ts b/tasks/sendZETA.ts index 249e457f..36b8dee4 100644 --- a/tasks/sendZETA.ts +++ b/tasks/sendZETA.ts @@ -1,62 +1,21 @@ -import { getAddress } from "@zetachain/protocol-contracts"; -import ZetaEthContract from "@zetachain/protocol-contracts/abi/evm/Zeta.eth.sol/ZetaEth.json"; -import ZetaConnectorEth from "@zetachain/protocol-contracts/abi/evm/ZetaConnector.eth.sol/ZetaConnectorEth.json"; -import ZetaConnectorZEVM from "@zetachain/protocol-contracts/abi/zevm/ConnectorZEVM.sol/ZetaConnectorZEVM.json"; -import { parseEther } from "ethers/lib/utils"; import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; +import { sendZETA } from "../helpers/sendZETA"; declare const hre: any; const main = async (args: any, hre: HardhatRuntimeEnvironment) => { - let connectorContract: any; + const [signer] = await hre.ethers.getSigners(); - const { ethers } = hre as any; + const { amount, destination, recipient } = args; + const from = hre.network.name; - const [signer] = await ethers.getSigners(); - - const destinationChainId = hre.config.networks[args.destination]?.chainId; - if (!destinationChainId) { - throw new Error("Invalid destination chain"); + const tx = await sendZETA(signer, amount, from, destination, recipient); + if (args.json) { + console.log(JSON.stringify(tx, null, 2)); + } else { + console.log(`Transaction hash: ${tx.hash}`); } - const destinationAddress = args.recipient || signer.address; - - const fromZetaChain = hre.network.name === "zeta_testnet"; - - const connectorAddress = getAddress("connector", hre.network.name as any); - const zetaTokenAddress = getAddress("zetaToken", hre.network.name as any); - connectorContract = new ethers.Contract( - connectorAddress, - fromZetaChain ? ZetaConnectorZEVM.abi : ZetaConnectorEth.abi, - signer - ); - const zetaTokenContract = new ethers.Contract( - zetaTokenAddress, - ZetaEthContract.abi, - signer - ); - const amount = parseEther(args.amount); - - if (fromZetaChain) { - await signer.sendTransaction({ - to: zetaTokenAddress, - value: amount, - }); - } - - await ( - await zetaTokenContract.connect(signer).approve(connectorAddress, amount) - ).wait(); - - const tx = await connectorContract.connect(signer).send({ - destinationAddress, - destinationChainId, - destinationGasLimit: 5000000, - message: ethers.utils.arrayify([]), - zetaParams: ethers.utils.arrayify([]), - zetaValueAndGas: amount, - }); - console.log(`Transaction hash: ${tx.hash}`); }; export const sendZETATask = task( @@ -66,4 +25,5 @@ export const sendZETATask = task( ) .addParam("amount", "Amount of ZETA to send") .addParam("destination", "Destination chain") - .addOptionalParam("recipient", "Recipient address"); + .addOptionalParam("recipient", "Recipient address") + .addFlag("json", "Output JSON");