Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating the toolkit to a client #93

Merged
merged 76 commits into from
Mar 10, 2024
Merged

Migrating the toolkit to a client #93

merged 76 commits into from
Mar 10, 2024

Conversation

fadeev
Copy link
Member

@fadeev fadeev commented Feb 2, 2024

Example:

import { ZetaChainClient } from "@zetachain/toolkit";

const client = new ZetaChainClient({network: "testnet"});
await client.getPools()

Endpoints can be customized:

const client = new ZetaChainClient({
  chains: {
    zeta_testnet: {
      api: [
        {
          type: "evm",
          url: "https://rpc.ankr.com/zetachain_evm_athens_testnet",
        },
      ],
    },
  },
  network: "testnet",
});

The design is very simple, ZetaChainClient is just a class for storing state (right now, that's network and chains). Methods are using the following this pattern to a) have access to class variables through this b) be defined in separate files for readability.

export const getEndpoints = function (
  this: ZetaChainClient, // <-- this
  type: any,
  network: any
) {
  if (!(this.chains as any)[network]) {
    throw new Error(`Network ${network} does not exist.`);
  }

  return (this.chains as any)[network].api.filter(
    (api: any) => api.type === type
  )[0]?.url;
};

SDK

import { ZetaChainClient } from "@zetachain/toolkit/client";
import { ethers } from "ethers";

const client = new ZetaChainClient({
  network: "testnet",
  wallet: ethers.Wallet.fromMnemonic(process.env.MNEMONIC as string),
});

/**
 * Initiates a deposit transaction of native gas or ERC-20 assets as ZRC-20 from
 * a connected chain to ZetaChain.
 *
 * @param this - ZetaChainClient instance.
 * @param options - Deposit options.
 * @param options.chain - Label of the connected chain from which the deposit is
 * made.
 * @param options.amount - Amount to be deposited in human readable form.
 * @param options.erc20 - If an ERC-20 token is being deposited, the address of
 * the ERC-20 token contract. If not provided, the deposit is assumed to be in
 * native gas token.
 * @param options.message - If a message is specified, ZetaChain will deposit
 * tokens into the `recipient` contract and call with with the message as an argument.
 * @param options.recipient - Recipient address for the deposit. If not provided,
 * the deposit is made to the signer's address. If the message is provided, the
 * recipient is assumed to be a contract address.
 *
 * @returns A promise that resolves with the transaction details upon success.
 */
const { hash } = client.deposit({
  chain: "goerli_testnet",
  amount: "10000",
});

client.trackCCTX({ hash })

// Deposit ERC-20 to ZetaChain as ZRC-20 and call an omnichain contract (recipient) with a message
client.deposit({
  chain: "goerli_testnet",
  amount: "10000",
  erc20: "0x07865c6e87b9f70255377e024ace6630c1eaa37f", // if erc20 is provided, send erc20, otherwise: native gas
  recipient: "0x2cD3D070aE1BD365909dD859d29F387AA96911e1", // if recipient is not provided, deposit to sender
  message: [["string"], ["hello"]] // if a message is provided it's a "deposit and call"
}

/**
 * Initiates a withdraw transaction of a ZRC-20 token from ZetaChain to a
 * connected chain as a native gas or ERC-20 token.
 *
 * @param this - ZetaChainClient instance.
 * @param options - Withdrawal options.
 * @param options.amount - Amount to be withdrawn in human readable form.
 * @param options.zrc20 - ZRC-20 token contract address.
 * @param options.recipient - Recipient address for the withdrawal. If not provided,
 * the withdrawal is made to the signer's address.
 *
 * @returns A promise that resolves with the transaction details upon success.
 */
const tx = await client.withdraw({
  amount: "1",
  zrc20: "0x0cbe0dF132a6c6B4a2974Fa1b7Fb953CF0Cc798a",
});

/**
 *
 * Initiates a cross-chain transfer of ZETA tokens from the source chain to the
 * destination chain.
 *
 * @param this - ZetaChainClient instance.
 * @param options - Send ZETA options.
 * @param options.chain - Source chain label.
 * @param options.destination - Destination chain label.
 * @param options.amount - Amount of ZETA tokens to be sent in human readable form.
 * @param options.recipient - Optional recipient address for the token transfer. If not
 * provided, the token transfer is made to the signer's address.
 * @param options.gasLimit - Optional gas limit on the destination chain.
 *
 * @returns A promise that resolves with the transaction details upon success.
 */
const tx = await client.sendZeta({
  chain: "goerli_testnet",
  destination: "zeta_testnet",
  recipient: "0x4955a3F38ff86ae92A914445099caa8eA2B9bA32",
  amount: "3",
});

client.getBalances({
  evm: "0x2cD3D070aE1BD365909dD859d29F387AA96911e1"
})

I wanted to add an ability to send cross-chain messages as a method, but since the best practice is for contracts to have approved counterparty addresses (interactors), this means virtually no contract will be able to accept a message sent from a EOA.

@fadeev fadeev marked this pull request as ready for review February 22, 2024 08:23
Copy link

socket-security bot commented Mar 5, 2024

@fadeev
Copy link
Member Author

fadeev commented Mar 6, 2024

@lucas-janon @andresaiello can you, please, review? I need codeowners review before merging. Thanks!

@fadeev fadeev merged commit f264c64 into main Mar 10, 2024
7 checks passed
@fadeev fadeev deleted the client branch March 10, 2024 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants