-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZetaChain withdraw ZETA function and task
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
) { | ||
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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"); |