Giter Site home page Giter Site logo

zeta-chain / toolkit Goto Github PK

View Code? Open in Web Editor NEW
34.0 34.0 19.0 3.36 MB

A library of ZetaChain helper utilities, contracts, and Hardhat tasks for smart-contract development

License: MIT License

TypeScript 96.09% Handlebars 1.95% Solidity 1.39% JavaScript 0.19% Shell 0.38%

toolkit's People

Contributors

dependabot[bot] avatar fadeev avatar lucas-janon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

toolkit's Issues

cctx: add timeout

Add a configurable timeout (should be possible to disable the timeout). After X number of tries it exists with code 1. Could be useful for CI.

Fix templating to lowercase all params

npx hardhat interact --contract 0xe6663Ea61512630438ADC89dB7fD9aE5Ccb28D7B --network mumbai_testnet --destination 5 --tokenId 2 --amount 1
.5 --to 0x2cD3D070aE1BD365909dD859d29F387AA96911e1
Error HH310: Invalid param --tokenId. Command line params must be lowercase.

CCM: can't send messages from BSC using native tokens for fees

Contract with native fees

npx hardhat messaging Message message:string --fees native

npx hardhat compile --force

npx hardhat deploy --networks goerli_testnet,bsc_testnet

BSC: https://testnet.bscscan.com/address/0x91d800CD2a88cF7E453FfDd29A8E1194BC59AA1f
Goerli: https://goerli.etherscan.io/address/0xa8c98d5B5f20875F42d784485B327CDf8bb3051B

When I'm trying to send a message from BSC to Goerli it fails:

npx hardhat interact --contract 0x91d800CD2a88cF7E453FfDd29A8E1194BC59AA1f --amount 0.1 --network bsc_testnet --destination goerli_testnet --message hello

โŒ https://testnet.bscscan.com/tx/0x26339a2852ce8c978592fee23565e7ae7ed620725a1bef1ab847b67ca3fdbf0a

When I'm sending from Goerli to BSC using the same contract, it's fine:

npx hardhat interact --contract 0xa8c98d5B5f20875F42d784485B327CDf8bb3051B --amount 0.1 --network goerli_testnet --destination bsc_testnet --message hello

โœ… https://goerli.etherscan.io/tx/0xb2930e88cc0cf58d61597e8abd41e62c3f614bbab8c750b61c57397ffc0c7390

Contract with ZETA fees

The same simple CCM contract with ZETA as fees works just fine both ways.

npx hardhat messaging Message message:string --fees zeta

https://testnet.bscscan.com/address/0x5C1d498E4200402CAAe9c6BFF4e3891A19686599

cctx: replace socket with interval polling

WebSocket is not reliable. The connection gets closed all the time, which makes the dev experience worse. It's also not possible to use CCTX tracking in scripts, because the script fails before getting all the info about a tx.

One solution is to make WSS reconnecting, but it's additional overhead. There is no reason to use WSS for this, polling should work just fine.

Omnichain template: add a task to call the contract from Bitcoin

Either modify the interact task or introduce a new one that will allow calling the contract from Bitcoin.

This will require making sure that the logic behind send-btc can be called programmatically from the template.

toolkit/tasks/sendBTC.ts

Lines 101 to 144 in d7b6c52

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const TESTNET = bitcoin.networks.testnet;
const API = getEndpoints("blockcypher", "btc_testnet")[0].url;
const pk = process.env.PRIVATE_KEY as any;
const ECPair = ECPairFactory(ecc);
const key = ECPair.fromPrivateKey(Buffer.from(pk, "hex"), {
network: TESTNET,
});
const { address } = bitcoin.payments.p2wpkh({
network: TESTNET,
pubkey: key.publicKey,
});
if (address === undefined) throw new Error("Address is undefined");
const utxos = await fetchUtxos(address);
const tx = await makeTransaction(
args.recipient,
key,
parseFloat(args.amount) * 100000000,
utxos,
address,
args.memo
);
const decoded = JSON.stringify(await decodeTransaction(tx), null, 2);
console.log(`\nTransaction:\n\n${tx}\n`);
console.log(`Decoded transaction:\n\n${decoded}\n`);
await confirm(
{
message: `Send ${parseFloat(args.amount)} tBTC to ${args.recipient}?`,
},
{ clearPromptOnDone: true }
);
const p1 = await fetch(`${API}/txs/push`, {
body: JSON.stringify({ tx }),
method: "POST",
});
const data = await p1.json();
const txhash = data?.tx?.hash;
console.log(`Transaction hash: ${txhash}`);
};

Add confirmation before token transfers

The toolkit currently features 3 token transfer commands:

  • send-btc
  • send-zeta
  • send-zrc20

We need make the output of these commands consistent and add a confirmation.

? Send 0.001 tBTC to tb1qy9pqmk2pd9sv63g27jt8r657wy0d9ueeh0nqur? (Y/n)
? Send 2 ZETA from zeta_testnet to 0x4257d7F29FD5a4f9c5b8E6F0e3e8EA0A241a1D00 on goerli_testnet? (Y/n)
? Send 0.1 gETH from goerli_testnet to 0x4257d7F29FD5a4f9c5b8E6F0e3e8EA0A241a1D00 on zeta_testnet? (Y/n)

Set up a Github action to run the main commands nightly

We need to ensure that most of the tools in the toolkit are working properly. To do this we can set up a Github action that runs commands and checks the output. For templating, for example, we need to run the template gen code, compile, deploy, interact with the contract and check that the changes have propagated to the destination successfully.

WIP: #36

send-btc: check if private key is not defined and print a more descriptive error message

An unexpected error occurred:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at new NodeError (node:internal/errors:399:5)
    at Function.from (node:buffer:335:9)
    at SimpleTaskDefinition.main [as action] (/Users/fadeev/template/node_modules/@zetachain/toolkit/dist/tasks/sendBTC.js:114:46)
    at Environment._runTaskDefinition (/Users/fadeev/template/node_modules/hardhat/src/internal/core/runtime-environment.ts:333:35)
    at Environment.run (/Users/fadeev/template/node_modules/hardhat/src/internal/core/runtime-environment.ts:166:25)
    at main (/Users/fadeev/template/node_modules/hardhat/src/internal/cli/cli.ts:280:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Happens when you don't have a private key in .env (or an env variable).

cctx: make tracking work in a browser environment

Right now trackCCTX depends on a Node.js package for spinners.

https://github.com/zeta-chain/toolkit/blob/cd1637d3f01d9c774edaf6c1b17c5c8535ee9b9a/helpers/tx.ts

Would be great to remove all Node.js logic from this function to be able to use it on the frontend. I imagine we could emit events from trackCCTX and catch them in a wrapper function that depends on spinners. So, trackCCTX will just emit events and return JSON and will be frontend-compatible, and the wrapper function will be Node.js-dependent and will show spinners in the terminal.

Helper functions like `prepareData` should be importable from browser context

Right now there are dependencies on Node.js libs, which makes it impossible to do imports:

// this doesn't work from a browser
import { prepareData } from "@zetachain/toolkit/helpers";

One simple temporary solution would be to add the following to package.json:

  "exports": {
    "./helpers/evm": "./dist/helpers/evm.js"
  },

Ideally, nothing in helpers/ should depend on Node.js. We can move all these things under lib/.

Task `omnichain`: add confirmation prompt before overwriting files

When following the tutorial:
https://www.zetachain.com/docs/developers/omnichain/tutorials/withdraw/#set-up-your-environment
The command

npx hardhat omnichain Withdraw recipient

will erase the file content to be written if they already exist.

It might eventually be a concern since there are many sections of the tutorial that write into deploy.ts and the command erase deploy.ts content.

My suggestion would be to add a verification for the command to ask for confirmation.

npx hardhat omnichain Withdraw recipient
deploy.ts already exist, do you want to continue and overwrite the file?

Make helper functions compatible with custom RPC endpoints

The toolkit ships with a number of helper functions that are useful for ZetaChain dapp devs. For example, getBalances, which fetches balances of all supported tokens from all connected chains. This is very straightforward and config-free when using public endpoints, but for production we recommend using dedicated RPC endpoints, such as the ones provided by BlockPi or Ankr.

The question is, what's the most dev-friendly way of making functions like getBalances configurable to use custom endpoints?

One option, is to have a "Client" class that you can instantiate with custom endpoints.

class Client {
  private rpcEndpoints: RpcEndpoints;

  constructor(customRpcEndpoints?: RpcEndpoints) {
    const defaultRpcEndpoints: RpcEndpoints = {
      blockchainA: 'https://public-node.blockchainA.com',
      blockchainB: 'https://public-node.blockchainB.com',
      // ... other blockchains
    };

    // Merge user RPC endpoints with default ones, prioritizing user's custom endpoints
    this.rpcEndpoints = { ...defaultRpcEndpoints, ...customRpcEndpoints };
  }

  async getBalances(): Promise<any[]> {
    // Logic to fetch balances from each blockchain using this.rpcEndpoints
    return balances;
  }
  //...
}

Second option is to make getBalances accept an optional argument with endpoints.

async function getBalances(userRpcEndpoints?: RpcEndpoints): Promise<any[]> {
  //...
  return balances;
}

In both scenarios API keys will be passed alongside endpoints most likely stored in env variables.

I think the second option is a bit more lightweight and since helper functions most likely will not require shared state, the class approach seems like an overkill.

Any better approaches?

Update `@zetachain/toolkit` for Athens 3

Hardhat tasks

npm

Related PR: #10

Tasks

send-btc: ReferenceError: fetch is not defined

npx hardhat send-btc --amount 0.001 --memo 629eEe97B95Bd6e04B0885De58eF016177a709Ae2cD3D070aE1BD365909dD859d29F387AA96911e1 --recipient tb1qy9pqmk2pd9sv63g27jt8r657wy0d9ueeh0nqur
An unexpected error occurred:

ReferenceError: fetch is not defined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.