Skip to content

Commit

Permalink
send zeta
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 16, 2023
1 parent a7ed59d commit 41f5fde
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
54 changes: 54 additions & 0 deletions helpers/sendZETA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getAddress } from "@zetachain/protocol-contracts";

Check failure on line 1 in helpers/sendZETA.ts

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
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,
});
};
62 changes: 11 additions & 51 deletions tasks/sendZETA.ts
Original file line number Diff line number Diff line change
@@ -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";

Check failure on line 1 in tasks/sendZETA.ts

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
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(
Expand All @@ -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");

0 comments on commit 41f5fde

Please sign in to comment.