Web3 Fundamentals · 6 min read
Transactions, Gas, and Storage
What is inside a NEAR transaction, how gas pays for computation in Tgas, and how storage staking locks NEAR for the bytes you use.
Transactions and Gas
A transaction is a signed instruction that changes the state of the blockchain: sending NEAR, transferring a token, or calling a smart contract method.
What is inside a NEAR transaction?
json
{
"signer_id": "alice.testnet",
"public_key": "ed25519:8hSHprDq2StXwMtNd43wDTXQYsjXcD4MJTXQYsjXcc",
"nonce": 42,
"receiver_id": "diploma.retr0school.testnet",
"block_hash": "6eB9…recent block…",
"actions": [
{ "FunctionCall": { "method_name": "mint_diploma", "args": "{…}", "gas": "100000000000000", "deposit": "20000000000000000000000" } }
]
}- signer_id / public_key: who signs, and with which of that account's access keys.
- nonce: a per-key counter. It keeps transactions in order and stops the same one being replayed.
- receiver_id: the account the actions run on, a person or a contract.
- block_hash: a recent block. A transaction that references an old block (about a day) is rejected, so signed transactions cannot be held and sent much later.
- actions: one transaction can batch several actions:
Transfer,FunctionCall,CreateAccount,AddKey,DeleteKey,DeployContract,Stake,DeleteAccount. - Amounts are in yoctoNEAR: 1 NEAR = 10^24 yocto. The deposit above is 0.02 NEAR.
The life of a transaction
- Your wallet builds the transaction and signs it with your private key.
- It sends it to an RPC node, which forwards it to the validators of your shard.
- The transaction becomes a receipt that is executed on the shard of the receiver account.
- If the contract calls another contract, that creates new receipts, possibly on other shards, in later blocks.
- Blocks are produced about every second and are final in about two seconds.
What is gas?
Every operation a transaction triggers (verifying the signature, running contract code, writing state) costs gas, a unit of computational work. Gas stops spam and infinite loops, and pays for the network's work.
- NEAR measures gas in Tgas (10^12 gas). A NEAR transfer uses about 0.45 Tgas; a contract call typically uses 2 to 30 Tgas.
- A single function call can attach up to 300 Tgas. If it runs out, the call fails.
- The gas price is set by the protocol, at least 0.0001 NEAR per Tgas (10^8 yocto per gas). It only rises when blocks are very full, so fees stay low and predictable.
- You pay up front for the gas you attach; unused gas is refunded.
- 30% of the gas burned by a contract call is paid to that contract's account, so builders earn from usage.
bash
fee (NEAR) = gas used (Tgas) × gas price (NEAR per Tgas)
transfer: 0.45 Tgas × 0.0001 = 0.000045 NEARTry it: NEAR fee and storage calculator
<div class="calc">
<label>Gas used (Tgas) <input id="gas" type="number" value="20" step="0.05"></label>
<label>Gas price (yocto per gas) <input id="price" type="number" value="100000000"></label>
<label>New state (bytes) <input id="bytes" type="number" value="600"></label>
<label>NEAR price (USD) <input id="usd" type="number" value="3"></label>
<div id="out"></div>
<p class="hint">Try 0.45 Tgas for a transfer, or 300 for the maximum a call can attach.</p>
</div>Edit this example in the Playground.
The NEAR price above is only an example. The gas price and the storage rate come from the protocol.
Storage: bytes on chain
State is not paid per transaction. The account that holds the data must keep enough NEAR locked: 1 NEAR per 100 KB (10^19 yocto per byte). This is called storage staking. Delete the data and the NEAR unlocks again.
That is why many contracts ask you to attach a small deposit. The Retr0 Academy certificate contract does this: you attach 0.02 NEAR when you mint, the contract measures how many bytes your certificate used, keeps exactly that, and refunds the rest in the same call.
rust
// from contracts/diploma/src/lib.rs
let token = self.tokens.internal_mint_with_refund(
token_id, account_id.clone(), Some(metadata), Some(account_id),
);Failed transactions
A function call can fail if:
- it runs out of the gas you attached, or
- the contract rejects it (
panic!/require!, for example "not the owner" or "deposit too small").
The failed receipt's state changes are rolled back and attached deposits are refunded, but you still pay for the gas burned. Cross-contract calls are asynchronous: if the second contract fails, the first contract's changes are not undone automatically, which is why callbacks matter (see Promises and Callbacks in the contracts course).
Reading a block explorer
On NearBlocks (testnet) a transaction page shows:
- Status: Success or Failure, for the transaction and each receipt
- Signer and Receiver
- Actions (transfer, function call with method and arguments)
- Deposit, gas burned and transaction fee
- Logs printed by the contract, such as NEP-297 events
Every transaction has a unique base58 transaction hash (like MoTocnN8iUHt…). Try it with a real certificate mint.
Quick numbers
| Thing | Typical cost |
|---|---|
| Transfer NEAR | about 0.45 Tgas ≈ 0.000045 NEAR |
| Mint one certificate | under 20 Tgas, plus about 0.01 NEAR locked for storage |
| 100 KB of contract state | 1 NEAR locked |
| Maximum gas for one call | 300 Tgas |
Summary
- A NEAR transaction has a signer, a key, a nonce, a receiver, a recent block hash and a list of actions.
- Gas is measured in Tgas, priced by the protocol, and unused gas is refunded.
- Storage is paid by locking NEAR for the bytes an account holds, not per transaction.
- Executed transactions are permanent, and failed ones still cost the gas burned.
Exercise
Open the calculator and find the fee for a transfer (0.45 Tgas) and how much NEAR 2 KB of new state locks. Then say whether reading a certificate with verify_diploma needs gas from your wallet, and why.
Saved in this browser.