Invoke method in a Smart Contract

Call read or write methods of a deployed smart contract through the Tatum API by passing the contract address, method name, ABI and parameters.

You can invoke a method in a Smart Contract (SC) to read or write. This is done through the ABI (Application Binary Interface) method.

  • Read: could be used to read the balance of a specific token on that smart contract.
  • Write: could be used to do something like burning some of the total minted tokens when the contract was originally deployed.

Disclaimer

Tatum ensures the endpoint "Invoke a method in an SC" works against the blockchain.

🚧

Tatum staff does not troubleshoot issues related to invoking a method in an SC. Users may refer to the related contract code, ABI documentation or reach out to the SC creator.

How to invoke a Smart Contract

Example with a minted smart contract for ERC20 on BSC. The read and write calls use the BNB Smart Chain invoke endpoint (POST /v3/bsc/smartcontract).

  1. Deploy Smart Contract with the deploy ERC-20 endpoint

    curl --location --request POST 'https://api.tatum.io/v3/blockchain/token/deploy' \
    --header 'x-api-key: {YOUR_API_KEY}' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "chain": "BSC",
        "symbol": "invoke_Token",
        "name": "invToken",
        "totalCap": "10000000",
        "supply": "10000000",
        "digits": 18,
        "address": "0x6b598e8be2629380c7dc0a95ca5b59923a136436",
        "fromPrivateKey": "{PRIVATE_KEY}"
    }'

    Response:

    {
        "txId": "0x86fe1d7f65c7c80a67ad85aeb5ec92df4b5cbabae82955da1bf3eb1fae1eb2cb"
    }
  2. Get the SC address from the explorer, or from the deployment txId with the get contract address endpoint. Example: 0x3fc5facec56e5b40df4a8abcebf7873259e340b6

  3. Click on the [Contract] tab:

    BscScan testnet contract page with the Decompile ByteCode button highlighted on the Contract tab
    BscScan bytecode decompilation result listing the contract's storage variables and methods
  4. On the "Code Decompilation result", grab the related info from decompiled code. If you know the type of SC, use its public ABI to figure out what the JSON body looks like for a specific method.

  5. READ method example: balanceOf returns the token balance of the address in params (replace {ADDRESS}).

    curl --location 'https://api.tatum.io/v3/bsc/smartcontract' \
    --header 'x-api-key: {YOUR_API_KEY}' \
    --header 'Content-Type: application/json' \
    --data '{
        "contractAddress": "0x3fc5facec56e5b40df4a8abcebf7873259e340b6",
        "methodName": "balanceOf",
        "methodABI": {
            "constant": true,
            "inputs": [
                {
                    "name": "owner",
                    "type": "address"
                }
            ],
            "name": "balanceOf",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        "params": [
            "{ADDRESS}"
        ]
    }'

    Response (in the token's smallest unit: 10,000,000 tokens with 18 decimals, which matches the original minted supply):

    {
        "data": "10000000000000000000000000"
    }
  6. WRITE method example: burn decreases the original minted supply. The amount is in the token's smallest unit, and fromPrivateKey belongs to the address that holds the tokens and pays the fee.

    curl --location 'https://api.tatum.io/v3/bsc/smartcontract' \
    --header 'x-api-key: {YOUR_API_KEY}' \
    --header 'Content-Type: application/json' \
    --data '{
        "contractAddress": "0x3fc5facec56e5b40df4a8abcebf7873259e340b6",
        "methodName": "burn",
        "methodABI": {
            "constant": false,
            "inputs": [
                {
                    "name": "amount",
                    "type": "uint256"
                }
            ],
            "name": "burn",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        "params": [
            "10"
        ],
        "fromPrivateKey": "{PRIVATE_KEY}"
    }'

    Response:

    {
        "txId": "0x1d77e9ed8a8ed84444f0b818f666c488eb6de77aef7329e324685cd2e84fd968"
    }
  • Check in the Explorer to find burn transaction:

    BscScan testnet token transfers list with the Burn method transaction highlighted

ERC-20 ABI Documentation

About Smart Contract code format

Smart Contracts are applications inside the blockchain with an interaction interface, where ABI can be described with JSON.

For ERC-20 and using this interface, you can do some actions with it. Every entry of it shows the corresponding method available to call. The only difference compared to the usual method call in a programming language is that it has expanded definitions.

Example:

{
    "constant": false,
    "inputs": [
        {
            "name": "_from",
            "type": "address"
        },
        {
            "name": "_to",
            "type": "address"
        },
        {
            "name": "_value",
            "type": "uint256"
        }
    ],
    "name": "transferFrom",
    "outputs": [
        {
            "name": "",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}

This ABI entry describes the following Solidity function (as declared in the ERC-20 interface):

function transferFrom(address _from, address _to, uint256 _value) external returns (bool);

The stateMutability field tells you whether a method needs a transaction: pure and view methods only read data and return a result, while nonpayable and payable methods change the state, so you send them as a transaction signed with a private key. constant and payable are legacy fields that older compilers emit; current ABIs describe the same with stateMutability. See the Solidity ABI specification.


Did this page help you?