Gas fees and gas-free methods
All the methods pertaining to smart contracts can be narrowed down to the methods that change the state of the contract and the methods that don’t.
Let’s have a look at the code of smart contracts and see what's the difference between those. Take the ERC-721 smart contract for example.
No gas fee methods
balanceOf
is a simple example of the method that doesn’t change a contract state.
function balanceOf(address owner) public view returns (uint256) {
...
}
All non-payable methods should be marked as view
. Looking at ABI generated by this contract, we can see the stateMutability
field with the view
value.
{
"constant": true,
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
Read more on the View functions in Solidity docs.
To view a list of methods, call the Contract.GetData method.
Requesting data from contract fields or mappings is also gas free.
Let’s take a look at the Simple Open Auction example from Solidity docs.
uint public auctionEndTime;
mapping(address => uint) public pendingReturns;
In ABI it looks the following way.
...
{
"constant": true,
"inputs": [],
"name": "auctionEndTime",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "pendingReturns",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
...
To read more on Solidity features, follow the mappings and fields links.
To access contract fields and mappings, use the Contract.GetData method.
Gas fee methods
All methods that change a contract state and aren't marked as view
require a gas fee to be paid for mining.
function mint(address to, uint256 tokenId) {
...
}
To check that, have a look at the stateMutability
field of an ABI.
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "mint",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
The stateMutability
value can be either payable or non-payable for the gas fee methods.
To call that kind of contract methods, use CallMethod or Web3SendMethod.