Skip to content

Commit

Permalink
Refactor: Gateway functions to get Gateway address dynamically (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukema95 authored Dec 26, 2024
1 parent 23bd0fe commit c537397
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 90 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"@solana/web3.js": "^1.95.3",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/faucet-cli": "^4.1.1",
"@zetachain/networks": "10.0.0",
"@zetachain/networks": "v10.0.0-rc1",
"@zetachain/protocol-contracts": "11.0.0-rc3",
"axios": "^1.4.0",
"bech32": "^2.0.0",
Expand Down
71 changes: 71 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { Wallet as SolanaWallet } from "@coral-xyz/anchor";
import type { WalletContextState } from "@solana/wallet-adapter-react";
import { PublicKey } from "@solana/web3.js";
import { networks } from "@zetachain/networks";
import mainnetAddresses from "@zetachain/protocol-contracts/dist/data/addresses.mainnet.json";
import testnetAddresses from "@zetachain/protocol-contracts/dist/data/addresses.testnet.json";
import type { Signer, Wallet } from "ethers";
import merge from "lodash/merge";

Expand Down Expand Up @@ -32,6 +34,7 @@ import {

export interface ZetaChainClientParamsBase {
chains?: { [key: string]: any };
contracts?: LocalnetAddress[] | MainnetTestnetAddress[];
network?: string;
}

Expand Down Expand Up @@ -69,13 +72,28 @@ export type ZetaChainClientParams = ZetaChainClientParamsBase &
}
);

interface MainnetTestnetAddress {
address: string;
category: string;
chain_id: number;
chain_name: string;
type: string;
}

interface LocalnetAddress {
address: string;
chain: string;
type: string;
}

export class ZetaChainClient {
public chains: { [key: string]: any };
public network: string;
public wallet: Wallet | undefined;
public signer: any | undefined;
public solanaWallet: SolanaWallet | undefined;
public solanaAdapter: WalletContextState | undefined;
private contracts: LocalnetAddress[] | MainnetTestnetAddress[];

/**
* Initializes ZetaChainClient instance.
Expand Down Expand Up @@ -137,6 +155,16 @@ export class ZetaChainClient {
this.chains = { ...networks };
this.network = params.network || "";

if (params.contracts) {
this.contracts = params.contracts;
} else if (this.network === "localnet" || this.network === "localhost") {
throw new Error("Localnet contracts are required");
} else {
this.contracts = this.network.includes("test")
? testnetAddresses
: mainnetAddresses;
}

this.mergeChains(params.chains);
}

Expand All @@ -148,6 +176,49 @@ export class ZetaChainClient {
});
}

public getGatewayAddress(): string {
if (this.network === "localnet" || this.network === "localhost") {
const gateway = (this.contracts as LocalnetAddress[]).find(
(item) => item.type === "gatewayZEVM"
);

if (!gateway) {
throw new Error("Gateway address not found in localnet configuration");
}

return gateway.address;
} else {
let gateway;
if (this.wallet) {
try {
gateway = (this.contracts as MainnetTestnetAddress[]).find(
async (item) =>
(await this.wallet!.getChainId()) === item.chain_id &&
item.type === "gateway"
);
} catch (error) {
throw new Error("Failed to get gateway address: " + error);
}
} else {
try {
gateway = (this.contracts as MainnetTestnetAddress[]).find(
async (item) =>
(await this.signer!.getChainId()) === item.chain_id &&
item.type === "gateway"
);
} catch (error) {
throw new Error("Failed to get gateway address: " + error);
}
}

if (!gateway) {
throw new Error(`Gateway address not found in signer or wallet`);
}

return gateway.address;
}
}

public getChains(): { [key: string]: any } {
return this.chains;
}
Expand Down
9 changes: 7 additions & 2 deletions packages/client/src/evmCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { revertOptions, txOptions } from "./types";
export const evmCall = async function (
this: ZetaChainClient,
args: {
gatewayEvm: string;
gatewayEvm?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
Expand All @@ -34,7 +34,12 @@ export const evmCall = async function (
) {
const signer = this.signer;
const { utils } = ethers;
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);
const gatewayEvmAddress = args.gatewayEvm || this.getGatewayAddress();
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const valuesArray = args.values.map((value, index) => {
const type = args.types[index];
Expand Down
11 changes: 8 additions & 3 deletions packages/client/src/evmDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ export const evmDeposit = async function (
args: {
amount: string;
erc20: string;
gatewayEvm: string;
gatewayEvm?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
}
) {
const signer = this.signer;
const { utils } = ethers;
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);
const gatewayEvmAddress = args.gatewayEvm || this.getGatewayAddress();
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const revertOptions = {
abortAddress: "0x0000000000000000000000000000000000000000", // not used
Expand All @@ -61,7 +66,7 @@ export const evmDeposit = async function (
);
const decimals = await erc20Contract.decimals();
const value = utils.parseUnits(args.amount, decimals);
await erc20Contract.connect(signer).approve(args.gatewayEvm, value);
await erc20Contract.connect(signer).approve(gatewayEvmAddress, value);
const method =
"deposit(address,uint256,address,(address,bool,address,bytes,uint256))";
tx = await gateway[method](
Expand Down
11 changes: 8 additions & 3 deletions packages/client/src/evmDepositAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const evmDepositAndCall = async function (
args: {
amount: string;
erc20: string;
gatewayEvm: string;
gatewayEvm?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
Expand All @@ -39,7 +39,12 @@ export const evmDepositAndCall = async function (
) {
const signer = this.signer;
const { utils } = ethers;
const gateway = new ethers.Contract(args.gatewayEvm, GatewayABI.abi, signer);
const gatewayEvmAddress = args.gatewayEvm || this.getGatewayAddress();
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const revertOptions = {
abortAddress: "0x0000000000000000000000000000000000000000", // not used
Expand Down Expand Up @@ -87,7 +92,7 @@ export const evmDepositAndCall = async function (
);
const decimals = await erc20Contract.decimals();
const value = utils.parseUnits(args.amount, decimals);
await erc20Contract.connect(signer).approve(args.gatewayEvm, value);
await erc20Contract.connect(signer).approve(gatewayEvmAddress, value);
const method =
"depositAndCall(address,uint256,address,bytes,(address,bool,address,bytes,uint256))";
tx = await gateway[method](
Expand Down
9 changes: 5 additions & 4 deletions packages/client/src/zetachainCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const zetachainCall = async function (
args: {
callOptions: any;
function: string;
gatewayZetaChain: string;
gatewayZetaChain?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
Expand All @@ -43,9 +43,10 @@ export const zetachainCall = async function (
) {
const signer = this.signer;
const { utils } = ethers;

const gatewayZetaChainAddress =
args.gatewayZetaChain || this.getGatewayAddress();
const gateway = new ethers.Contract(
args.gatewayZetaChain,
gatewayZetaChainAddress,
GatewayABI.abi,
signer
);
Expand Down Expand Up @@ -92,7 +93,7 @@ export const zetachainCall = async function (
);
const gasZRC20Contract = new ethers.Contract(gasZRC20, ZRC20ABI.abi, signer);
const approve = await gasZRC20Contract.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
gasFee,
args.txOptions
);
Expand Down
12 changes: 7 additions & 5 deletions packages/client/src/zetachainWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const zetachainWithdraw = async function (
this: ZetaChainClient,
args: {
amount: string;
gatewayZetaChain: string;
gatewayZetaChain?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
Expand All @@ -38,8 +38,10 @@ export const zetachainWithdraw = async function (
const signer = this.signer;
const { utils } = ethers;

const gatewayZetaChainAddress =
args.gatewayZetaChain || this.getGatewayAddress();
const gateway = new ethers.Contract(
args.gatewayZetaChain,
gatewayZetaChainAddress,
GatewayABI.abi,
signer
);
Expand All @@ -60,7 +62,7 @@ export const zetachainWithdraw = async function (
const [gasZRC20, gasFee] = await zrc20.withdrawGasFee();
if (args.zrc20 === gasZRC20) {
const approveGasAndWithdraw = await zrc20.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
value.add(gasFee),
args.txOptions
);
Expand All @@ -72,13 +74,13 @@ export const zetachainWithdraw = async function (
signer
);
const approveGas = await gasZRC20Contract.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
gasFee,
args.txOptions
);
await approveGas.wait();
const approveWithdraw = await zrc20.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
value,
args.txOptions
);
Expand Down
12 changes: 7 additions & 5 deletions packages/client/src/zetachainWithdrawAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const zetachainWithdrawAndCall = async function (
amount: string;
callOptions: any;
function?: string;
gatewayZetaChain: string;
gatewayZetaChain?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
Expand All @@ -46,8 +46,10 @@ export const zetachainWithdrawAndCall = async function (
const signer = this.signer;
const { utils } = ethers;

const gatewayZetaChainAddress =
args.gatewayZetaChain || this.getGatewayAddress();
const gateway = new ethers.Contract(
args.gatewayZetaChain,
gatewayZetaChainAddress,
GatewayABI.abi,
signer
);
Expand Down Expand Up @@ -102,7 +104,7 @@ export const zetachainWithdrawAndCall = async function (
);
if (args.zrc20 === gasZRC20) {
const approveGasAndWithdraw = await zrc20.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
value.add(gasFee),
args.txOptions
);
Expand All @@ -114,13 +116,13 @@ export const zetachainWithdrawAndCall = async function (
signer
);
const approveGas = await gasZRC20Contract.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
gasFee,
args.txOptions
);
await approveGas.wait();
const approveWithdraw = await zrc20.approve(
args.gatewayZetaChain,
gatewayZetaChainAddress,
value,
args.txOptions
);
Expand Down
9 changes: 3 additions & 6 deletions packages/tasks/src/evmCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ZetaChainClient } from "../../client/src/";
export const evmCall = async (args: any, hre: HardhatRuntimeEnvironment) => {
try {
const [signer] = await hre.ethers.getSigners();
const client = new ZetaChainClient({ network: "testnet", signer });
const network = hre.network.name;
const client = new ZetaChainClient({ network, signer });
const tx = await client.evmCall({
gatewayEvm: args.gatewayEvm,
receiver: args.receiver,
Expand All @@ -32,11 +33,7 @@ export const evmCall = async (args: any, hre: HardhatRuntimeEnvironment) => {

task("evm-call", "Call a universal app", evmCall)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addOptionalParam("gatewayEvm", "contract address of gateway on EVM")
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
Expand Down
9 changes: 3 additions & 6 deletions packages/tasks/src/evmDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ZetaChainClient } from "../../client/src/";
export const evmDeposit = async (args: any, hre: HardhatRuntimeEnvironment) => {
try {
const [signer] = await hre.ethers.getSigners();
const client = new ZetaChainClient({ network: "testnet", signer });
const network = hre.network.name;
const client = new ZetaChainClient({ network, signer });
const tx = await client.evmDeposit({
amount: args.amount,
erc20: args.erc20,
Expand Down Expand Up @@ -34,11 +35,7 @@ export const evmDeposit = async (args: any, hre: HardhatRuntimeEnvironment) => {

task("evm-deposit", "Deposit tokens", evmDeposit)
.addParam("receiver", "Receiver address on ZetaChain")
.addOptionalParam(
"gatewayEvm",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addOptionalParam("gatewayEvm", "contract address of gateway on EVM")
.addFlag("callOnRevert", "Whether to call on revert")
.addOptionalParam(
"revertAddress",
Expand Down
Loading

0 comments on commit c537397

Please sign in to comment.