Skip to content

Commit

Permalink
ZetaChain withdraw ZETA function and task
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 9, 2024
1 parent 51b8705 commit 751ed4d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
zetachainCall,
zetachainWithdraw,
zetachainWithdrawAndCall,
zetachainWithdrawZETA,
} from ".";

export interface ZetaChainClientParamsBase {
Expand Down Expand Up @@ -140,4 +141,5 @@ export class ZetaChainClient {
evmDepositAndCall = evmDepositAndCall;
evmCall = evmCall;
evmDeposit = evmDeposit;
zetachainWithdrawZETA = zetachainWithdrawZETA;
}
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from "./withdraw";
export * from "./zetachainCall";
export * from "./zetachainWithdraw";
export * from "./zetachainWithdrawAndCall";
export * from "./zetachainWithdrawZETA";
66 changes: 66 additions & 0 deletions packages/client/src/zetachainWithdrawZETA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ethers } from "ethers";

import GatewayABI from "./abi/GatewayZEVM.sol/GatewayZEVM.json";
import { ZetaChainClient } from "./client";
import type { revertOptions, txOptions } from "./types";

/**
* @function zetachainWithdrawZETA
* @description Withdraws a specified amount of ZETA tokens from ZetaChain to a connected chain.
*
* @param {ZetaChainClient} this - The instance of the ZetaChain client that contains the signer information.
* @param {object} args - The function arguments.
* @param {string} args.amount - The amount of ZETA tokens to withdraw.
* @param {string} args.gatewayZetaChain - The address of the ZetaChain gateway contract.
* @param {string} args.receiver - The address that will receive the withdrawn ZETA tokens.
* @param {string} args.chainId - The chain ID of the connected chain.
* @param {txOptions} args.txOptions - Transaction options such as gasPrice, nonce, etc.
* @param {revertOptions} args.revertOptions - Options to handle call reversion, including revert address and message.
*
* @returns {object} - Returns an object containing the transaction, gas token, and gas fee.
* @property {object} tx - The transaction object for the withdrawal.
*/

export const zetachainWithdrawZETA = async function (
this: ZetaChainClient,
args: {
amount: string;
gatewayZetaChain: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
chainId: string;

Check failure on line 32 in packages/client/src/zetachainWithdrawZETA.ts

View workflow job for this annotation

GitHub Actions / build

Expected interface keys to be in ascending order. 'chainId' should be before 'txOptions'
}
) {
const signer = this.signer;
const { utils } = ethers;

const gateway = new ethers.Contract(
args.gatewayZetaChain,
GatewayABI.abi,
signer
);

const revertOptions = {
abortAddress: "0x0000000000000000000000000000000000000000",
callOnRevert: args.revertOptions.callOnRevert,
onRevertGasLimit: args.revertOptions.onRevertGasLimit,
revertAddress: args.revertOptions.revertAddress,
revertMessage: utils.hexlify(
utils.toUtf8Bytes(args.revertOptions.revertMessage)
),
};

const value = utils.parseUnits(args.amount, 18);

const method =
"withdraw(bytes,uint256,uint256,(address,bool,address,bytes,uint256))";
const tx = await gateway[method](
utils.hexlify(args.receiver),
value,
args.chainId,
revertOptions,
args.txOptions
);
return { tx };
};
1 change: 1 addition & 0 deletions packages/tasks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { withdrawTask } from "./withdraw";
export { zetachainCall } from "./zetachainCall";
export { zetachainWithdraw } from "./zetachainWithdraw";
export { zetachainWithdrawAndCall } from "./zetachainWithdrawAndCall";
export { zetachainWithdrawZETA } from "./zetachainWithdrawZETA";
6 changes: 5 additions & 1 deletion packages/tasks/src/zetachainWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const zetachainWithdraw = async (
}
};

task("zetachain-withdraw", "Withdraw tokens from ZetaChain", zetachainWithdraw)
task(
"zetachain-withdraw",
"Withdraw ZRC-20 tokens from ZetaChain",
zetachainWithdraw
)
.addOptionalParam(
"gatewayZetaChain",
"contract address of gateway on ZetaChain",
Expand Down
77 changes: 77 additions & 0 deletions packages/tasks/src/zetachainWithdrawZETA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";

import { ZetaChainClient } from "../../client/src/";

export const zetachainWithdrawZETA = async (
args: any,
hre: HardhatRuntimeEnvironment
) => {
try {
const [signer] = await hre.ethers.getSigners();
const client = new ZetaChainClient({ network: "testnet", signer });
const response = await client.zetachainWithdrawZETA({
amount: args.amount,
gatewayZetaChain: args.gatewayZetaChain,
receiver: args.receiver,
chainId: args.chainId,

Check failure on line 17 in packages/tasks/src/zetachainWithdrawZETA.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'chainId' should be before 'receiver'
revertOptions: {
callOnRevert: args.callOnRevert,
onRevertGasLimit: args.onRevertGasLimit,
revertAddress: args.revertAddress,
revertMessage: args.revertMessage,
},
txOptions: {
gasLimit: args.txOptionsGasLimit,
gasPrice: args.txOptionsGasPrice,
},
});

const receipt = await response.tx.wait();
console.log("Transaction hash:", receipt.transactionHash);
} catch (e) {
console.error("Transaction error:", e);
}
};

task(
"zetachain-withdraw-zeta",
"Withdraw tokens from ZetaChain",
zetachainWithdrawZETA
)
.addOptionalParam(
"gatewayZetaChain",
"contract address of gateway on ZetaChain",
"0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0"
)
.addOptionalParam("chainId", "The chain ID of the connected chain")
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
"Revert address",
"0x0000000000000000000000000000000000000000"
)
.addOptionalParam(
"txOptionsGasPrice",
"The gas price for the transaction",
10000000000,
types.int
)
.addOptionalParam(
"txOptionsGasLimit",
"The gas limit for the transaction",
7000000,
types.int
)
.addOptionalParam("revertMessage", "Revert message", "0x")
.addParam(
"receiver",
"The address of the receiver contract on a connected chain"
)
.addOptionalParam(
"onRevertGasLimit",
"The gas limit for the revert transaction",
7000000,
types.int
)
.addParam("amount", "The amount of tokens to send");

0 comments on commit 751ed4d

Please sign in to comment.