0

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:

  1. User has 10 tokens, sends 5 to a new wallet: 5k + 20k = 25k in gas? (above 21k)
  2. User has 10 tokens, sends 5 to an existing wallet : 5k + 5k = 10k in gas? (but since less than 21k then 21k is used?)
  3. 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?)
  4. 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:

  1. UserA (sender) sends a token to UserB (receiver)
  2. A fee is calculated by the SmartContract
  3. The fee amount is transferred to a fee wallet
  4. 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):

  1. update timestamp variable UserA (SSTORE) : 5k gas (or 20k if 1st time)
  2. update timestamp variable UserB (SSTORE) : 5k gas (or 20k if 1st time)
  3. send fee to fee wallet (update fee wallet balance using SSTORE internally?) : 5k gas
  4. update UserA balance from sending tokens to UserB & Fee wallet (using SSTORE internally?): 5k gas
  5. 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:

  1. update timestamp variable UserA (SSTORE) : 5k gas (or 20k if 1st time)
  2. update timestamp variable UserB (SSTORE) : 5k gas (or 20k if 1st time)
  3. send fee to fee wallet : 21k gas
  4. tx cost of sending tokens from UserA to UserB (original amount - fees) : 21k gas

Total : 52k - 82k gas

Thank you! Gab

6
  • The 'minimum 21k gas' limit is for a transaction sent from the off-chain (i.e., using an externally-owned account). – goodvibration Dec 28 '20 at 10:47
  • Thanks, so in the SmartContract the transactions will cost the two SSTORE operations? – Gabriel Dec 29 '20 at 8:31
  • Your question is too long and generally unreadable. Try to minimize it and focus on a single issue (i.e., ask a single question). I don't even understand what "two SSTORE operations" your talking about. Do I have to search it all over your (very long) question? – goodvibration Dec 29 '20 at 8:42
  • BTW - where did you get the 5k gas in "User has 10 tokens, sends 5 to a new wallet: 5k + 20k = 25k in gas?" from??? – goodvibration Dec 29 '20 at 8:42
  • I got it from the Ethereum Yellow Paper (ethereum.github.io/yellowpaper/paper.pdf). I am assuming that since transactions are really two global memory write operations that they are SSTORE operations. An update (writing to a non-zero global var) is 5k, 20k for a write (writing to a zero value global var), 5k for a delete (setting to zero) and you get a 15k reimbursement. – Gabriel Dec 29 '20 at 9:35
0

I am not sure I understand where the 21k limit comes from / how it is calculated

21K gas is not a gas-limit, but the minimum gas-cost for a transaction which is sent from an externally-owned account (i.e., from the off-chain).

This restriction does not apply for a transaction which is sent from a smart-contract account (i.e., from the on-chain).

1
  • Ok, so is my understanding correct when a say: 1. update timestamp variable UserA (SSTORE) : 5k gas (or 20k if 1st time) 2. update timestamp variable UserB (SSTORE) : 5k gas (or 20k if 1st time) 3. send fee to fee wallet (update fee wallet balance using SSTORE internally?) : 5k gas 4. update UserA balance from sending tokens to UserB & Fee wallet (using SSTORE internally?): 5k gas 5. update UserB balance from receiving tokens sent by UserA (using SSTORE internally?): 5k gas (or 20k if 1st time) Total : 25k - 70k gas – Gabriel Dec 29 '20 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.