Invoke method in a Smart Contract

Modified on Thu, 15 Dec 2022 at 12:31 PM

TABLE OF CONTENTS


To work with invoking a method in a Smart Contract (SC), we have two methods either read or write.

This is done through the ABI (Application Binary Interface) method.


Examples:

  • Read: could be something like reading the balance of a specific token on that smart contract.
  • Write: could be something like burn, to burn 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.


Troubleshooting issues in successfully invoking a method in an SC is out of the scope of Tatum's staff. Users may check the related SC code, and ABI documentation and reach out to the SC creator.



How to invoke a Smart Contract

Example with a minted smart contract for ERC20 on BSC. 

  1. Deploy Smart Contract
    curl --location --request POST 'https://api.tatum.dev/v3/blockchain/token/deploy' \
    --header 'X-Api-Key: ##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": "0x200cc2960b8b75e1fe4cf461e6e02d1b9da869c7d46995709671232b228e6346"
    }'
    //Response
    {
        "txId": "0x86fe1d7f65c7c80a67ad85aeb5ec92df4b5cbabae82955da1bf3eb1fae1eb2cb"
    }
  2. Get the SC address from the explorer.
    Example: [0x3fc5facec56e5b40df4a8abcebf7873259e340b6] 
  3. Click on the [Contract] tab:
  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. Go to Postman:
    •  READ method example: balanceOf >> to get balance of that token
      {
          "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": [
              "{{address1}}"
          ]
      }
      //Response
      {
          "data": "10000000000000000000000000" >> which matches the original minted tokens
      }

    • WRITE method example: burn >> to decrease #original minted tokens
      {
          "contractAddress": "0x3fc5facec56e5b40df4a8abcebf7873259e340b6",
          "methodName": "burn",
          "methodABI": {
              "constant": false,
              "inputs": [
                  {
                      "name": "to",
                      "type": "uint256"
                  }
              ],
              "name": "burn",
              "outputs": [
                  {
                      "name": "",
                      "type": "bool"
                  }
              ],
              "payable": false,
              "stateMutability": "nonpayable",
              "type": "function"
          },
          "params": [
              10
          ],
          "fromPrivateKey": "{{pk1}}"
      }
      //Response
      {
          "txId": "0x1d77e9ed8a8ed84444f0b818f666c488eb6de77aef7329e324685cd2e84fd968"
      }

    • Check in the Explorer to find burn transaction



ERC-20 ABI Documentation



About Smart Contract code format

Smart Contracts are application 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 corresponding method available to call. The only difference comparing to usual method call in 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 is the same as this:
transferFrom(_from: address, _to: address, _value: uint256): bool


Other flags show if stateMutability is required (we may say tx on chain, for further reading - solidity state mutability), or if it is a constant of SC or more.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article