Invoke method in a Smart Contract
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 invoke 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.
-
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" }
-
Get the SC address from the explorer. Example: [
0x3fc5facec56e5b40df4a8abcebf7873259e340b6
] -
Click on the [Contract] tab:
-
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.
-
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 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 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.
Updated 6 months ago