@@ -2,6 +2,7 @@ import path from "path";
22import fs from "fs" ;
33import { ethers } from "ethers" ;
44import { Command } from "commander" ;
5+ import ZRC20ABI from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json" ;
56
67export const getAbi = ( name : string ) => {
78 const abiPath = path . resolve (
@@ -27,6 +28,45 @@ export const createRevertOptions = (options: {
2728 } ;
2829} ;
2930
31+ export const approveZRC20 = async (
32+ zrc20Address : string ,
33+ contract : string ,
34+ amount : string ,
35+ signer : ethers . Wallet ,
36+ gasLimit ?: number
37+ ) => {
38+ const zrc20 = new ethers . Contract ( zrc20Address , ZRC20ABI . abi , signer ) ;
39+ const [ gasZRC20 , gasFee ] = gasLimit
40+ ? await zrc20 . withdrawGasFeeWithGasLimit ( gasLimit )
41+ : await zrc20 . withdrawGasFee ( ) ;
42+
43+ const zrc20TransferTx = await zrc20 . approve ( contract , gasFee ) ;
44+ await zrc20TransferTx . wait ( ) ;
45+
46+ const decimals = await zrc20 . decimals ( ) ;
47+ if ( gasZRC20 === zrc20 . target ) {
48+ const targetTokenApprove = await zrc20 . approve (
49+ contract ,
50+ gasFee + ethers . parseUnits ( amount , decimals )
51+ ) ;
52+ await targetTokenApprove . wait ( ) ;
53+ } else {
54+ const targetTokenApprove = await zrc20 . approve (
55+ contract ,
56+ ethers . parseUnits ( amount , decimals )
57+ ) ;
58+ await targetTokenApprove . wait ( ) ;
59+ const gasZRC20Contract = new ethers . Contract (
60+ gasZRC20 ,
61+ ZRC20ABI . abi ,
62+ signer
63+ ) ;
64+ const gasFeeApprove = await gasZRC20Contract . approve ( contract , gasFee ) ;
65+ await gasFeeApprove . wait ( ) ;
66+ }
67+ return { decimals } ;
68+ } ;
69+
3070export const createCommand = ( name : string ) => {
3171 return new Command ( name )
3272 . requiredOption (
@@ -54,11 +94,12 @@ export const createCommand = (name: string) => {
5494 "Gas limit for revert tx" ,
5595 "500000"
5696 )
57- . option ( "-n, --name <contract>" , "Contract name" , "Connected" )
97+ . option ( "-n, --name <contract>" , "Contract name" )
5898 . option (
5999 "--rpc <url>" ,
60100 "RPC endpoint" ,
61101 "https://zetachain-athens-evm.blockpi.network/v1/rpc/public"
62102 )
103+ . option ( "--gas-limit <number>" , "Gas limit for the transaction" , "1000000" )
63104 . option ( "--private-key <key>" , "Private key to sign the transaction" ) ;
64105} ;
0 commit comments