Will Chen 陈俊宏 🌖
Will Chen 陈俊宏 🌖

@stablechen

12 Tweets 2,456 reads Oct 12, 2021
HOW GAS ON TERRA WORKS (short🧵)
1/ If you're coming from @ethereum, you may be tempted to think that gas on @terra_money works the exact same. Unfortunately, this assumption will likely throw you off guard as there are a couple key differences between the two platforms.
2/ FEE STRUCTURE
All transactions on Terra must include a fee to compensate the validator that spends computing resources to include it in a block. Fees on Terra can be broken down into 2 parts:
1. tax (aka. "stability fee"), on all coin transfers
2. "gas" (compute resource)
3/ WHAT IS GAS?
"gas" is a unit for measuring roughly the amount of compute resources required to process an operation.
Gas used in a transaction is computed deterministically -- a transaction will always need the same amount of gas on any node running a compliant Terra Core.
4/ GAS PRICE
A separate but related concept is that of "gas price".
- gas = unit of resources used
- gas price = price per unit gas
gas price = gas / coins paid
e.g. If I pay 2 LUNA for a tx requiring 2000000 gas, the gas price for the tx = 0.000001 LUNA per unit gas.
5/ MIN GAS PRICE
Nodes (both regular and validators) can set a min gas price.
Transactions with a fee implying a gas price below this threshold will not get processed or propagated by that node.
Most validators set their min gas price according to fcd.terra.dev
6/ GAS IN TX'S
When building a tx, you are required to specify in the fee how much gas you will consume.
This can be tricky -- if you put too little, your transaction might run out of gas. If you put too much, you risk wasting (excess fee for gas declared is not refunded).
7/ GAS ESTIMATION
One way is to dynamically estimate the gas required for the tx via automatic estimation.
This will simulate the tx by running it and reporting the gas used.
It's a dumb method, but it's all we have atm. That's why estimation via TFL's public nodes is capped.
8/ RULES FOR GAS
- tx w/ fee using gas price < min. will not get processed
- tx w/ fee using gas price >= min. will try run until complete or out of gas
- setting higher gas prices gives no priority (yet)
- fees paid for unused gas will not be refunded
9/ TAKEAWAYS
Tips:
- make each smart contract operation as predictable in gas consumption as possible (avoid branches with large variance)
- run your own nodes to ensure dedicated access to gas estimation for your dApps
- err toward more gas to counter risk of out of gas
10/ CONCLUSION
Learn how gas works and test your assumptions. lest it be a source of frustration in your @terra_money development journey.
Even TFL engineers get sometimes tripped up on these issues, so no shame in getting confused on such a "basic" topic.
FML. flipped it
** gas price = coins paid / gas declared
2 LUNA / 2000000 gas = 0.000001 LUNA / 1 gas
:(
11/ APPENDIX
Contract gas:
- the Wasmer VM that gets run inside @terra_money's CosmWasm integration is instrumented with its own gas meter that uses a table that assigns gas price to each WASM operation
- reported WASM gas is translated to Terra gas by multiplying by a param

Loading suggestions...