I'm trying to figure out how the transaction costs are calculated for a transaction that is storing data on the Ethereum Blockchain. It seems I'm always off. Here is my way of calculating (I'm using the Kovan Testnet but I guess the calculation should be identical):
I uploaded the follwing smart contract
pragma solidity ^0.5.12;
contract NewContract {
struct Location {
string JSON;
}
Location[] locations;
function createLocation(string memory _json) public {
locations.push(Location(_json));
}
function viewLocation(uint _id) public view returns(string memory) {
string memory result = locations[_id].JSON;
return result;
}
}
When I save the String: "Test123" I assumed the price would be made up out of the following stats according to the yeelow paper: https://ethereum.github.io/yellowpaper/paper.pdf Appendix G. Fee Schedule
68: Paid for every non-zero byte of data or code for a transaction.
21000: Paid for every transaction.
Since the size of the string "Test123" is 7 Byte my calculation was: (21.000 + (7*68))*9(Gwei)= 21.476 gas * 9 Gwei = 193.284 Gwei (transaction costs)
Which would be 0.000193284 Ether My test calculation: https://kovan.etherscan.io/tx/0x8098ddd06cefe06280a4a499c5ff04d9fa93af64a7b77d060173607c32b54c4c said that it used 64.244 Gas and it cost 0.000578196 Ether
What calculation does the EVM do that I'm not aware of?
Thanks in advance :)
"Test123"
, then write a function which saves that string directly and explicitly. In your code, you are doing a lot more than just saving that specific string, for example: 1. You receive it as input. 2. You push it into an array.