1

I'm using hardhat and have deployed some contracts. That's created a directory deployments which containes meta data of the deployed smart contracts. Is it called artifacts?

Now, via the library ethers.js, I want to instantiate those contracts and interract with them. The contracts are on ronkeby.

I've able to find small pieces, some in web3.js and some in ethers.js and it's been confusing.

How to do it?

Here's a part of those contracts from ./deployments/rinkeby/MyContract123.json

    {
      "address": "0x<12345>",
      "abi": [
        {
          "inputs": [
            {
              "internalType": "address",
              "name": "_factory",
              "type": "address"
            },
            {
              "internalType": 
              //.............

      "transactionHash": "0x<some data....>",
      "receipt": {
        //.....
      },
      "args": [
        "0x<some data....>",
        "0x<some data....>"
      ],
      "solcInputHash": "<some data....>",
      "metadata": "....",
      "bytecode": "...",
      "deployedBytecode": "....",
      "devdoc": {
      },
      "userdoc": {
      },
      "storageLayout": {
      }
    }

How can I read the ABI of this contract and instantiate it, to be then able to interract with it?

2 Answers 2

0

I'm just importing the json file right in my .ts file and referencing it directly from ethers

Import looks like this

import myContract from '../../../artifacts/contracts/MyContract.sol/MyContract.json';

Then to create the ethers contract I do

contract = new ethers.Contract(
  address,
  myContract.abi,
  provider
);
0

Another option for Hardhat could be the plugin @0xweb/hardhat, which generates TypeScript classes encapsulating the dequanto library. This library is based on ethers.js and web3js.

When the contracts are compiled, the TypeScript classes are automatically generated. These classes include strongly typed methods for:

  • Reading data from the blockchain (call methods)
  • Submitting data to the network (submit transactions)
  • Listening to events and retrieving strongly typed arguments
  • Fetching passed strongly typed logs emitted by the contract

To prepare the development environment:

$ npm i 0xweb -g
$ 0xweb init --hardhat
$ npx hardhat compile --watch
# compiles sol contracts and listens for changes to recompile

Here is an example of reading/writing to an ERC20 contract:

import { MyContract123 } from './0xweb/hardhat/MyContract123'
import { HardhatProvider } from '@dequanto/hardhat/HardhatProvider'
import { Web3ClientFactory } from '@dequanto/clients/Web3ClientFactory'

let provider = new HardhatProvider();
let client = await provider.client('localhost');
let contract = new MyContract123('0x...', client);
// or you can deploy directly from code using hardhat signer
let contract = await provider.deployClass(MyContract123, { arguments: [ 1, 'foo' ], client });

// or you can target the mainnet
let client = Web3ClientFactory.get('eth');
let contract = new MyContract123('0x...', client);

// Read, Write, Fetch logs:

let decimals = await contract.decimals();

let account = { address: '0x...' }
let tx = contract.transfer(account, '0x...', 5n ** decimals);
let receipt = await tx.wait();

let transfers = await contract.getPastLogsTransfer();
console.log(transfers);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.