I am trying to design a SmartContract for a Token and I am having some issues with the gas calculations. When a user sends a token to someone, I would like there to be a fee. Where it gets fuzzy for me is the gas. I read that a minimum of 21k in gas must be paid for all transactions but it is not clear what is included in this. I read that storing data (SSTORE) costs 20k in gas the first time, 5k in gas for updates and 5k to reset (set to zero) + you get a 15k in gas refund. From my understanding when you send tokens this is just updating the balance on two wallets (using SSTORE?), thus it should be one of the following:
- User has 10 tokens, sends 5 to a new wallet: 5k + 20k = 25k in gas? (above 21k)
- User has 10 tokens, sends 5 to an existing wallet : 5k + 5k = 10k in gas? (but since less than 21k then 21k is used?)
- User has 10 tokens, sends 10 to an existing wallet : 5k + 5k = 10k in gas? (but since less than 21k then 21k is used?), when tx done 15k in gas reimbursed? (does this reimbursement happen because of the 21k limit?)
- User has 10 tokens, sends 10 to a new wallet : 5k + 20k = 25k in gas (above 21k), when tx done 15k in gas reimbursed? (does this reimbursement happen because of the 21k limit?)
so I am not sure I understand where the 21k limit comes from / how it is calculated. The above is important for me to understand, because it will determine how I write my code.
I have not been able to find this information anywhere, someone told me that internal transactions cost less than 21k, someone else told me that all transactions are SSTORE operations and 21k is just a lower limit to the cost of a transaction, so if you had a user sending some tokens to an existing wallet (vanilla ERC-20 SmartContract) it would be 5k + 5k + 11k (network fee) = 21k but if you had some other operations (incl. sending inner transactions) making the gas above 21k you would have 5k + 5k + other_ops >= 21k
The algorithm I am trying to write is:
- UserA (sender) sends a token to UserB (receiver)
- A fee is calculated by the SmartContract
- The fee amount is transferred to a fee wallet
- UserB receives the amount sent minus the fees
There are timestamps involved in the calculations, thus I broke this down the gas cost to (excluding all other calculation costs):
- update timestamp variable UserA (SSTORE) : 5k gas (or 20k if 1st time)
- update timestamp variable UserB (SSTORE) : 5k gas (or 20k if 1st time)
- send fee to fee wallet (update fee wallet balance using SSTORE internally?) : 5k gas
- update UserA balance from sending tokens to UserB & Fee wallet (using SSTORE internally?): 5k gas
- update UserB balance from receiving tokens sent by UserA (using SSTORE internally?): 5k gas (or 20k if 1st time)
Total : 25k - 70k gas
Is this correct? Or is step 3 going to cost 21k in gas and steps 4 + 5 (together) going to also cost 21k in gas, like this:
- update timestamp variable UserA (SSTORE) : 5k gas (or 20k if 1st time)
- update timestamp variable UserB (SSTORE) : 5k gas (or 20k if 1st time)
- send fee to fee wallet : 21k gas
- tx cost of sending tokens from UserA to UserB (original amount - fees) : 21k gas
Total : 52k - 82k gas
Thank you! Gab