@meterio/contract-toolkit

0.0.6 • Public • Published

Contract Toolkit

This is a toolkit library for contract debugging

Prerequisite

You need to create providers.json.

A typical providers.json looks like this, where key is provider-name and value is rpc url config. provider-name is defined in src/const/network.js:

{
  "mainnet": { "url": "http://rpc.meter.io", "meterifyUrl": "http://mainnet.meter.io" },
  "testnet": { "url": "http://rpctest.meter.io", "meterifyUrl": "http://testnet.meter.io" },

  "ethereum": { "url": "https://speedy-nodes-nyc.moralis.io/[key]/eth/mainnet" },
  "bsctest": { "url": "https://data-seed-prebsc-1-s1.binance.org:8545/" },

  "localhost": { "url": "http://localhost:8545", "meterifyUrl": "http://localhost:8669"},
}

Put these providers.json file under safe folders, such as /Users/simon/safe/. And configure .env with

PROVIDERS_CONFIG=/Users/simon/safe/providers.json

And then, please put your private key in keystore and name it with name.keystore pattern, put them under safe folders, such as /Users/simon/keys, And add one more line in .env file

ACCOUNTS_DIR=/Users/simon/keys

Please notice that system will recursively find the keystore file and every time you load the private key, you'll need to type in the passphrase you set when you create the keystore.

Read Usage (call)

Calling a contract is as simple as the following, notice that you could easily load the account you just set in accounts.json by loadAccount. Here's an example calling balanceOf on ERC20.

const { loadAccount, loadWeb3Contract, Network } = require('../const');
const { inst } = loadWeb3Contract(Network.metermain, 'ERC20', 'VOLT_AIR');
(async () => {
  const userAcct = await loadAccount('bob');
  const owner = userAcct.addr;

  const r = await inst.methods.balanceOf(owner).call({});
  console.log(r);
})();

Write Usage (send)

send is a little bit more complex then call, but it's still pretty straightforward. Here's an example calling approve on ERC20.

const { enableAccount, loadAddress, loadWeb3Contract, Network } = require('../const');
const { utils } = require('ethers');

const network = Network.metermain;
const { web3, inst } = loadWeb3Contract(network, 'ERC20', 'VOLT_AIR');

const spender = loadAddress(network, 'Geyser_MTRG_USDC_LP');
const amount = utils.parseUnits('100', 18).toHexString();

(async () => {
  const ownerAcct = await enableAccount(web3, 'bob');
  const r = await inst.methods.approve(spender, amount).send({ from: ownerAcct.addr, gasLimit: 4700000 });
  console.log(r);
})();

Readme

Keywords

none

Package Sidebar

Install

npm i @meterio/contract-toolkit

Weekly Downloads

0

Version

0.0.6

License

MIT

Unpacked Size

20.2 MB

Total Files

866

Last publish

Collaborators

  • simonzg
  • yanfeizuo