Skip to content

Commit

Permalink
Refactor gateway functions (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 9, 2024
1 parent 11980fc commit 51b8705
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 175 deletions.
49 changes: 32 additions & 17 deletions packages/client/src/evmCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@ import { ethers } from "ethers";

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

/**
* @function evmCall
* @description Calls a universal app contract on ZetaChain.
*
* @param {ZetaChainClient} this - The instance of the ZetaChain client that contains the signer information.
* @param {object} args - The function arguments.
* @param {string} args.gatewayEvm - The address of the EVM gateway contract.
* @param {string} args.receiver - The address of the target contract or account to call on the EVM chain.
* @param {string} args.types - JSON string representing the types of the function parameters (e.g., ["uint256", "address"]).
* @param {Array} args.values - The values to be passed to the function (should match the types).
* @param {txOptions} args.txOptions - Transaction options such as gasLimit, gasPrice, etc.
* @param {revertOptions} args.revertOptions - Options to handle call reversion, including revert address, message, and gas limit for the revert scenario.
*
* @returns {object} - Returns the transaction object.
* @property {object} tx - The transaction object that represents the function call.
*/

export const evmCall = async function (
this: ZetaChainClient,
args: {
callOnRevert: boolean;
gasLimit: number;
gasPrice: ethers.BigNumber;
gatewayEvm: string;
onRevertGasLimit: number;
receiver: string;
revertAddress: string;
revertMessage: string;
types: string;
revertOptions: revertOptions;
txOptions: txOptions;
types: string[];
values: any[];
}
) {
const signer = this.signer;
const { utils } = ethers;
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);

const typesArray = JSON.parse(args.types);
const valuesArray = args.values.map((value, index) => {
const type = typesArray[index];
const type = args.types[index];

if (type === "bool") {
try {
Expand All @@ -40,7 +53,7 @@ export const evmCall = async function (
});

const encodedParameters = utils.defaultAbiCoder.encode(
typesArray,
args.types,
valuesArray
);

Expand All @@ -50,15 +63,17 @@ export const evmCall = async function (
args.receiver,
encodedParameters,
{
abortAddress: "0x0000000000000000000000000000000000000000",
callOnRevert: args.callOnRevert,
onRevertGasLimit: args.onRevertGasLimit,
revertAddress: args.revertAddress,
revertMessage: utils.hexlify(utils.toUtf8Bytes(args.revertMessage)),
abortAddress: "0x0000000000000000000000000000000000000000", // not used
callOnRevert: args.revertOptions.callOnRevert,
onRevertGasLimit: args.revertOptions.onRevertGasLimit,
revertAddress: args.revertOptions.revertAddress,
revertMessage: utils.hexlify(
utils.toUtf8Bytes(args.revertOptions.revertMessage)
),
},
{
gasLimit: args.gasLimit,
gasPrice: args.gasPrice,
gasLimit: args.txOptions.gasLimit,
gasPrice: args.txOptions.gasPrice,
}
);

Expand Down
42 changes: 29 additions & 13 deletions packages/client/src/evmDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,54 @@ import { ethers } from "ethers";

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

/**
* @function evmDeposit
* @description Deposits a specified amount of ERC-20 or native gas tokens to a receiver on ZetaChain.
*
* @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 ERC20 tokens or native currency to deposit.
* @param {string} args.erc20 - The address of the ERC20 token contract. If depositing native currency (e.g., ETH), this can be left empty or undefined.
* @param {string} args.gatewayEvm - The address of the ZetaChain gateway contract on the EVM-compatible blockchain.
* @param {string} args.receiver - The address of the receiver or target contract for the deposit.
* @param {txOptions} args.txOptions - Transaction options, including gasLimit, gasPrice, etc.
* @param {revertOptions} args.revertOptions - Options to handle call reversion, including revert address, message, and gas limit for the revert scenario.
*
* @returns {object} - Returns the transaction object.
* @property {object} tx - The transaction object representing the deposit transaction.
*/

export const evmDeposit = async function (
this: ZetaChainClient,
args: {
amount: string;
callOnRevert: boolean;
erc20: string;
gasLimit: number;
gasPrice: ethers.BigNumber;
gatewayEvm: string;
onRevertGasLimit: number;
receiver: string;
revertAddress: string;
revertMessage: string;
revertOptions: revertOptions;
txOptions: txOptions;
}
) {
const signer = this.signer;
const { utils } = ethers;
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);

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

const txOptions = {
gasLimit: args.gasLimit,
gasPrice: args.gasPrice,
gasLimit: args.txOptions.gasLimit,
gasPrice: args.txOptions.gasPrice,
};
let tx;
if (args.erc20) {
Expand Down
51 changes: 34 additions & 17 deletions packages/client/src/evmDepositAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@ import { ethers } from "ethers";

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

/**
* @function evmDepositAndCall
* @description Deposits a specified amount of ERC-20 or native gas tokens and calls a universal app contract on ZetaChain.
*
* @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 ERC20 tokens or native currency to deposit.
* @param {string} args.erc20 - The address of the ERC20 token contract. If depositing native currency (e.g., ETH), this can be left empty or undefined.
* @param {string} args.gatewayEvm - The address of the ZetaChain gateway contract on the EVM-compatible blockchain.
* @param {string} args.receiver - The address of the receiver contract or account where the function call will be executed.
* @param {string} args.types - JSON string representing the types of the function parameters (e.g., ["uint256", "address"]).
* @param {Array} args.values - The values to be passed to the function (should match the types).
* @param {txOptions} args.txOptions - Transaction options, including gasLimit, gasPrice, etc.
* @param {revertOptions} args.revertOptions - Options to handle call reversion, including revert address, message, and gas limit for the revert scenario.
*
* @returns {object} - Returns the transaction object.
* @property {object} tx - The transaction object representing the deposit and function call.
*/

export const evmDepositAndCall = async function (
this: ZetaChainClient,
args: {
amount: string;
callOnRevert: boolean;
erc20: string;
gasLimit: number;
gasPrice: ethers.BigNumber;
gatewayEvm: string;
onRevertGasLimit: number;
receiver: string;
revertAddress: string;
revertMessage: string;
types: string;
revertOptions: revertOptions;
txOptions: txOptions;
types: string[];
values: any[];
}
) {
Expand All @@ -26,22 +42,23 @@ export const evmDepositAndCall = async function (
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);

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

const txOptions = {
gasLimit: args.gasLimit,
gasPrice: args.gasPrice,
gasLimit: args.txOptions.gasLimit,
gasPrice: args.txOptions.gasPrice,
};

const typesArray = JSON.parse(args.types);
const valuesArray = args.values.map((value, index) => {
const type = typesArray[index];
const type = args.types[index];

if (type === "bool") {
try {
Expand All @@ -57,7 +74,7 @@ export const evmDepositAndCall = async function (
});

const encodedParameters = utils.defaultAbiCoder.encode(
typesArray,
args.types,
valuesArray
);

Expand Down
13 changes: 13 additions & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ethers } from "ethers";

export type revertOptions = {
callOnRevert: boolean;
onRevertGasLimit: number;
revertAddress: string;
revertMessage: string;
};

export type txOptions = {
gasLimit: number;
gasPrice: ethers.BigNumber;
};
62 changes: 38 additions & 24 deletions packages/client/src/zetachainCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,40 @@ import { ethers } from "ethers";
import GatewayABI from "./abi/GatewayZEVM.sol/GatewayZEVM.json";
import ZRC20ABI from "./abi/ZRC20.sol/ZRC20.json";
import { ZetaChainClient } from "./client";
import type { revertOptions, txOptions } from "./types";

/**
* @function zetachainCall
* @description Calls a contract on 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.function - The name of the function to be executed on the target contract.
* @param {string} args.gatewayZetaChain - The address of the ZetaChain gateway contract.
* @param {string} args.receiver - The address of the contract or account that will receive the call.
* @param {string} args.types - JSON string representing the types of the function parameters (e.g., ["uint256", "address"]).
* @param {Array} args.values - The values to be passed to the function (should match the types).
* @param {string} args.zrc20 - The address of the ZRC20 token contract used for paying gas fees.
* @param {number} args.gasLimit - The amount of gas to be used for the call.
* @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 cross-chain call.
* @property {string} gasZRC20 - The address of the ZRC20 gas token.
* @property {ethers.BigNumber} gasFee - The amount of gas fee paid for the transaction.
*/

export const zetachainCall = async function (
this: ZetaChainClient,
args: {
amount: string;
callOnRevert: boolean;
function: string;
gasLimit: number;
gasPrice: ethers.BigNumber;
gatewayZetaChain: string;
onRevertGasLimit: number;
receiver: string;
revertAddress: string;
revertMessage: string;
types: string;
revertOptions: revertOptions;
txOptions: txOptions;
types: string[];
values: any[];
zrc20: string;
}
Expand All @@ -32,24 +51,19 @@ export const zetachainCall = async function (
);

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

const txOptions = {
gasLimit: args.gasLimit,
gasPrice: args.gasPrice,
abortAddress: "0x0000000000000000000000000000000000000000", // not used
callOnRevert: args.revertOptions.callOnRevert,
onRevertGasLimit: args.revertOptions.onRevertGasLimit,
revertAddress: args.revertOptions.revertAddress,
revertMessage: utils.hexlify(
utils.toUtf8Bytes(args.revertOptions.revertMessage)
),
};

const functionSignature = utils.id(args.function).slice(0, 10);

const typesArray = JSON.parse(args.types);
const valuesArray = args.values.map((value, index) => {
const type = typesArray[index];
const type = args.types[index];

if (type === "bool") {
try {
Expand All @@ -65,7 +79,7 @@ export const zetachainCall = async function (
});

const encodedParameters = utils.defaultAbiCoder.encode(
typesArray,
args.types,
valuesArray
);

Expand All @@ -80,7 +94,7 @@ export const zetachainCall = async function (
const approve = await gasZRC20Contract.approve(
args.gatewayZetaChain,
gasFee,
txOptions
args.txOptions
);
await approve.wait();
const tx = await gateway[
Expand All @@ -91,7 +105,7 @@ export const zetachainCall = async function (
message,
args.gasLimit,
revertOptions,
txOptions
args.txOptions
);
return tx;
return { gasFee, gasZRC20, tx };
};
Loading

0 comments on commit 51b8705

Please sign in to comment.