Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jan 10, 2025
2 parents a42b0b0 + 70298d0 commit 1345269
Showing 23 changed files with 530 additions and 605 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@uniswap/v2-core": "^1.0.1",
"@zetachain/protocol-contracts-solana": "2.0.0-rc1",
"chai": "^4.2.0",
"cpx": "^1.5.0",
"eslint": "^8.42.0",
@@ -101,7 +102,7 @@
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/faucet-cli": "^4.1.1",
"@zetachain/networks": "10.0.0-rc3",
"@zetachain/protocol-contracts": "11.0.0-rc3",
"@zetachain/protocol-contracts": "11.0.0-rc4",
"axios": "^1.4.0",
"bech32": "^2.0.0",
"bip39": "^3.1.0",
@@ -125,4 +126,4 @@
"ws": "^8.17.1"
},
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
}
71 changes: 71 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
@@ -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";

@@ -23,6 +25,7 @@ import {
getZRC20GasToken,
sendZeta,
solanaDeposit,
solanaDepositAndCall,
trackCCTX,
zetachainCall,
zetachainWithdraw,
@@ -31,6 +34,7 @@ import {

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

@@ -68,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.
@@ -136,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);
}

@@ -147,6 +176,47 @@ export class ZetaChainClient {
});
}

public async getGatewayAddress(): Promise<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 {
const chainId = await this.wallet!.getChainId();
gateway = (this.contracts as MainnetTestnetAddress[]).find(
(item) => chainId === item.chain_id && item.type === "gateway"
);
} catch (error) {
throw new Error("Failed to get gateway address: " + error);
}
} else {
try {
const chainId = await this.signer!.getChainId();
gateway = (this.contracts as MainnetTestnetAddress[]).find(
(item) => chainId === 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;
}
@@ -176,6 +246,7 @@ export class ZetaChainClient {
getZRC20FromERC20 = getZRC20FromERC20;
getZRC20GasToken = getZRC20GasToken;
solanaDeposit = solanaDeposit;
solanaDepositAndCall = solanaDepositAndCall;
zetachainWithdrawAndCall = zetachainWithdrawAndCall;
zetachainWithdraw = zetachainWithdraw;
zetachainCall = zetachainCall;
9 changes: 7 additions & 2 deletions packages/client/src/evmCall.ts
Original file line number Diff line number Diff line change
@@ -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;
@@ -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 || (await this.getGatewayAddress());
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const valuesArray = args.values.map((value, index) => {
const type = args.types[index];
11 changes: 8 additions & 3 deletions packages/client/src/evmDeposit.ts
Original file line number Diff line number Diff line change
@@ -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 || (await this.getGatewayAddress());
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const revertOptions = {
abortAddress: "0x0000000000000000000000000000000000000000", // not used
@@ -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](
11 changes: 8 additions & 3 deletions packages/client/src/evmDepositAndCall.ts
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ export const evmDepositAndCall = async function (
args: {
amount: string;
erc20: string;
gatewayEvm: string;
gatewayEvm?: string;
receiver: string;
revertOptions: revertOptions;
txOptions: txOptions;
@@ -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 || (await this.getGatewayAddress());
const gateway = new ethers.Contract(
gatewayEvmAddress,
GatewayABI.abi,
signer
);

const revertOptions = {
abortAddress: "0x0000000000000000000000000000000000000000", // not used
@@ -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](
Loading

0 comments on commit 1345269

Please sign in to comment.