Gas Cost of Solidity Functions
This article discusses the (sometimes surprising) cost of using contract and library functions in Solidity, the defacto smart contract language for the Ethereum blockchain.
Background
During development of Datona Labs’ Identity contract templates, we wanted to provide helpful error messages, which required string operations such as concatenation, for example:
function TransferTo(address _address, uint amount) public onlyOwner {
require(amount <= unallocated, concat("Invalid amount. "
"Available:", stringOfUint(unallocated))); // whatever
}
String concatenation is facilitated by the Solidity compiler using:
string memory result = string(abi.encodePacked("trust", "worthy"))
But we would like to wrap that with a more meaningful name and include it with other useful string utility functions such as string of integer.
Of course we want to use as little gas (few cycles) as possible because blockchain languages like Solidity are very expensive to run compared with normal systems and the gas actually costs a measurable amount of money.
Linkage Options
For adding the string concatenation feature, which is a simple ‘pure’ function which does…