Course for request transaction with Smart contract

Learn what an ABI Code is and how to query ABI Code using the EQ Hub API. Also, learn how to encode function parameters defined in the ABI Code.

ABI Code is required to execute the function defined in the smart contract. Through the process below, let's look at what ABI Code is and how to query the ABI Code of a smart contract through the EQ Hub API.

In addition, we will look at how to encode the parameters defined in the function to transmit transactions using the function of the smart contract.

This guide will guide you through the following:

  • What is ABI Code?
  • Search ABI Code
  • Encoding Argument of smart contract

What is ABI Code?

ABI stands for Application Binary Interface. The ABI of a smart contract is a standard way for a blockchain network and a smart contract to interact, and the ABI Code is a JSON interface code in which methods, parameters, and response values defined in the smart contract are written.

Below is an example and brief description of the JSON interface that makes up the ABI Code.

[
    ...
    {
        //The name of the function being executed.
        "name": "transfer",

        "stateMutability": "nonpayable",
        "type": "function"

        //A parameter defined in a function. When entering arguments, they must be entered in the following order. .
        //ex) transfer("Arguments required by to", "Arguments required for amount")
        "inputs": [
            {
                "internalType": "address",

                //The name of the parameter.
                "name": "to",

                //The data type of the parameter. 
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "outputs": [
            {
                "internalType": "bool",
                "name": "",
                "type": "bool"
            }
        ],

    },
    ...
]

Searching ABI Code

The process of searching the ABI Code of a smart contract using the EQ Hub API is as follows.

  1. Call the API to inquire the ABI Code of the smart contract. For more information on the API, see here.
  2. You can check the ABI Code of the smart contract through the API response.

Encoding Argument of Smart Contract

Before to start

There are no restrictions on how to encode the arguments. In this tutorial, we will learn how to encode parameters to send a request to a blockchain network, and how to encode parameters using web3.js among the various encoding methods.
If you want to know more about how to use web3.js, check web3.js docs.

What is EncodeABI?

The process of sending a transaction to a smart contract and inquiring data is an act of executing a function (method) defined in the smart contract. At this time, the process of converting the parameters required by the function into the form required by the smart contract is called factor encoding, and the parameters must be encoded to use the smart contract.

Encoded parameters are used by entering them in the ‘data’ field entered in the Request API related to object creation for transaction signing or gas inquiry.


Encoding arguments using web3.js

🚧

Before performing the steps below, check the pre-conditions.

An API Key is required to use the API to inquire the ABI Code.

  • For information on how to check the API Key of your project, refer to here.
  • Refer to the example code below and here on how to use the API Key when sending requests to the EQ Hub API.

The web3.js version used in the example is 1.10.0. If the library does not work normally, be sure to check the version.
If you are using web3.js version 4.x, please click here to check the changes.

The example code below is an example of the process of encoding an argument using web3.js as an example of 'token transfer'.

to(address)’ and ‘amount(uint256)’ are the parameters required by the ‘transfer’ function.

For a full example code for a token transfer transaction, please see here.

import axios from "axios";
const Web3 = require('web3');
const WEB3 = new Web3();

const main = async () => {

  const API_KEY = 'YOUR_API_KEY';
  const END_POINT = 'https://ag.eqhub.eqbr.com'
  let server;

  server = axios.create({
    baseURL: END_POINT,
  })

  server.defaults.headers.common['x-eq-ag-api-key'] = API_KEY;

  const SENDER_ADDRESS = 'YOUR_WALLET_ADDRESS';
  const SENDER_PRIVATE_KEY = 'YOUR_WALLET_PRIVATE_KEY';

  const RECEIVER_ADDRESS = 'RECEIVER_WALLET_ADDRESS';

  const SEND_AMOUNT = 220;

  //This is an arbitrarily set micro chain id.
  const MY_MICRO_CHAIN_ID = 2000;

  const CONTRACT_ADDRESS = 'TOKEN_CONTRACT_ADDRESS';

  const FUNCTION_NAME = 'transfer';

  const ABICode = await server.get(`/api/v2/contracts/address/${CONTRACT_ADDRESS}/abi-code?microChainId=${MY_MICRO_CHAIN_ID}`)
                              .then(res => res.data)

  const contract = new WEB3.eth.Contract(ABICode);
  
  const transferParameter = [RECEIVER_ADDRESS, WEB3.utils.toHex(WEB3.utils.toWei(SEND_AMOUNT.toString()))]
  
  const encodeParameter = contract.methods[FUNCTION_NAME](...transferParameter).encodeABI();
  
  //This is another way using the encodeABI function.
  const encodedParameter = contract.methods.transfer(RECEIVER_ADDRESS, WEB3.utils.toHex(WEB3.utils.toWei(SEND_AMOUNT.toString()))).encodeABI();
  
  ...
  
  const transaction = {
    nonce,
    to      : CONTRACT_ADDRESS,
    chainId : '2000',
    gasPrice: '0x2540be401,
    gas     : '0x89d3,
    data    : encodeParameter
  }
};

main().then().catch();