- Prevent bad actors from spamming the network with useless transactions
- Burn a minuscule fraction of the token supply on each transaction
- Incentivize developers by giving contracts 30% of the gas they burn while executing
- Implement a wall time by capping how much gas each chunk can burn to
1000Tgas(~1000msof compute time)
$NEAR tokens based on the network’s gas price.
Understanding Gas Fees
For every transaction, users get charged a small NEAR fee which has to be paid upfront. This fee is calculated using deterministic gas units, and transformed into a cost in NEAR using the network’s gas price.Gas Units
Every action in NEAR costs a fixed amount of gas units, meaning that the same operation will always cost the same amount of gas units. Gas units were engineered in such a way that they can be translated into compute resources, where1Tgas gets you approx. 1ms of compute time.
Each chunk can burn at most 1000Tgas, i.e. about 1000ms of compute, which is what allows the network to produce a new block approximately every second.
A single transaction can also attach at most 1000Tgas. It may spend that gas in one receipt or spread it over the receipts it spawns across several blocks.
Gas Price
To determine the actual NEAR fee, the cost of all actions in the transaction is multiplied by a gas price. The gas price is recalculated each block based on the network’s demand and floor at1Tgas = 0.0001Ⓝ.
If the previous block is more than half full the price goes up by 1%, otherwise it goes down by 1% (until it reaches the floor).
What is the gas price now?
What is the gas price now?
You can query how much a gas unit costs in
yoctoNEAR (1Ⓝ = 1e24 yocto) through the RPC. To convert in Tgas per NEAR, you can use the following formula: gas_price * 1e12 / 1e24.Right now, 1 Tgas costs: ⓃBuying vs. Burning Gas
Gas is bought at one price and burned at another. There’smin_gas_purchase_price = 0.001Ⓝ per Tgas.
- Buy price:
max(current_gas_price, min_gas_purchase_price). This is what is deducted from your account upfront for the gas attached to the transaction - Burn price:
min(buy_price, current_gas_price)- This is what the gas actually costs while executing. The gas price at the moment of execution, but never higher than the buy price.
Cost for Common Actions
Knowing that actions have a fixed cost in gas units, we can calculate the cost of common operations at the minimum gas price of1Tgas = 0.0001Ⓝ.
Note that the fee is in NEAR, to obtain the cost in dollars multiply by the current price of
$NEAR
Where do these numbers come from?
Where do these numbers come from?
NEAR is configured with base costs. An example:The “sir” here stands for “sender is receiver”. Yes, these are all identical, but that could change in the future.When you make a request to transfer funds, NEAR immediately deducts the appropriate You can query this value by using the
send amount from your account. Then it creates a receipt, an internal book-keeping mechanism. Creating a receipt has its own associated costs:protocol_config RPC endpoint and search for action_receipt_creation_config.The appropriate amount for creating this receipt is also immediately deducted from your account.The “transfer” action won’t be finalized until the next block. At this point, the execution amount for each of these actions will be deducted from your account.Gas prices can change between the time of purchase and the time of execution, but a transaction is not affected by that. The execution part is bought at the buy price and burned at gas_price, with the difference refunded, so what you end up paying is:Creating an Account
Creating an account costs a fixed0.007Ⓝ, which is burned.
This is not a gas fee. The protocol charges it by making the refund smaller: when a receipt creates an account, it keeps back enough of the price difference refund to bring the total cost of the creation up to 0.007Ⓝ. You don’t pay an extra fee, you simply get less back.
If the gas burned while creating the account is already worth more than 0.007Ⓝ - which happens when the network’s gas price is high - nothing is kept back and you just pay the gas.
The charge applies however the account is created: a CreateAccount action, a transfer to a not-yet-existing implicit account, a DeterministicStateInit action, or a contract creating a sub-account.
Why does creating an account cost extra?Every account occupies state that the nodes running its shard have to keep forever. An account can occupy up to
770 bytes without a storage deposit, and at the storage price of 1Ⓝ per 100kb that space is worth about 0.0077Ⓝ. The charge makes sure that space is paid for. It lands slightly below 0.0077Ⓝ because the protocol can only hold back as much as the price difference on the gas burned while creating the account.How Do I Buy Gas?
You don’t buy gas, instead, the gas fee is automatically removed from your account’s balance when the transaction is first processed based on the action’s gas cost and the buy price.Transactions signed with a gas key instead pay their gas fee from the key’s own prepaid balance, and gas refunds return to the key.
1000Tgas. This amount will be converted to NEAR using the buy price and deducted from your account’s balance.
Anything you overpaid is refunded to your account one block later. This covers both the gas you attached but did not use, and the difference between the buy price and the burn price. Because of that second part, almost every transaction gets a refund, even one that attached exactly the right amount of gas.
Gas Refund Fee
Gas Refund Fee
Since protocol version 78, the unspent gas at the end of receipt execution is subject to a gas refund fee. The fee is still at 0 while we give projects time to adapt. The plan is to move to a fee calculated as
max(1 Tgas, 0.05 * unspent_gas) * gas_price. The gas price used is the burn price.But why introducing such a fee instead of refunding all gas?The reason is that attaching too much gas to function calls makes the network less efficient.Congestion control between shards gets tricky when transactions have much more gas attached than they actually use. The network limits how many cross contract calls can target a single shard per chunk, to avoid huge queues of incoming receipts on a shard. This limit sees the attached gas as an upper boundary for how much work the receipt causes on the receiving shard. Attaching too much gas can cause this limit to become too restrictive, which stalls shards unnecessarily.The other inefficiency comes from the refund receipts that are created for essentially every function call. While each of them is relatively cheap to execute, in the sum they are a significant part of the global traffic on NEAR Protocol. Note that a refund receipt is needed for essentially every function call anyway, to return the price difference, so the fee only helps with congestion control.Gas as a Developer Incentive
In NEAR, 30% of the gas fees burned while executing a contract go to the contract’s account. This is a powerful incentive for developers to create and maintain useful contracts. For example, in this transaction the user calls a function in theguestbook.near-examples.testnet contract.
Executing the function call burned a total of ~0.00032Ⓝ, from which 30% went to the contract’s account. This means that the contract’s account received 0.000096Ⓝ.
Notice that the fee comes from the gas burned during the function execution, and not from the total gas used.
Estimating Costs for a Call
If you’re developing a smart contract, you might want to estimate how much gas a function call will consume. This is useful to estimate limits for your function and avoid running into out-of-gas errors. One of the most accurate ways to estimate gas costs is by running your function intestnet. To know exactly how much gas a specific part of your function uses, you can use the used_gas method from our SDK.
Another option is to use Sandbox Testing (available in Rust and JavaScript), which simulates the NEAR network. There you can access the gas burnt after each function call.