Balance changes

To Share and +4 nLEARNs

Prerequisites

Native NEAR (Ⓝ)

Balance changes on accounts can be tracked by using our changes RPC endpoint. You can test this out by sending tokens to an account using NEAR-CLI and then viewing the changes made.

Send Tokens

near send sender.testnet receiver.testnet 1
  • You should see a result in your terminal that looks something like this:
Sending 1 NEAR to receiver.testnet from sender.testnet
Transaction Id 4To336bYcoGc3LMucJPMk6fMk5suKfCrdNotrRtTxqDy
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/4To336bYcoGc3LMucJPMk6fMk5suKfCrdNotrRtTxqDy

View Balance Changes

Example Query using HTTPie:

http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare 
    method=EXPERIMENTAL_changes 
    'params:={
        "block_id": "CJ24svU3C9FaULVjcNVnWuVZjK6mNaQ8p6AMyUDMqB37",
        "changes_type": "account_changes",
        "account_ids": ["sender.testnet"]
    }'
**Example Response:**

“`json
{
“id”: “dontcare”,
“jsonrpc”: “2.0”,
“result”: {
“block_hash”: “BRgE4bjmUo33jmiVBcZaWGkSLVeL7TTi4ZxYTvJdPbB9”,
“changes”: [
{
“cause”: {
“tx_hash”: “4To336bYcoGc3LMucJPMk6fMk5suKfCrdNotrRtTxqDy”,
“type”: “transaction_processing”
},
“change”: {
“account_id”: “sender.testnet”,
“amount”: “11767430014412510000000000”,
“code_hash”: “11111111111111111111111111111111”,
“locked”: “0”,
“storage_paid_at”: 0,
“storage_usage”: 806
},
“type”: “account_update”
}
]
}
}
“`


Alternatively, you can view account balances by querying view_account which only requires an accountId.

Example HTTPie Request:

  http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare method=query 
  params:='{
    "request_type": "view_account",
    "finality": "final",
    "account_id": "sender.testnet"
  }'

Example Response:

{
  "id": "dontcare",
  "jsonrpc": "2.0",
  "result": {
    "amount": "11767430683960197500000000",
    "block_hash": "HUiscpNyoyR5z1UdnZhAJLNz1G8UjBrFTecSYqCrvdfW",
    "block_height": 50754977,
    "code_hash": "11111111111111111111111111111111",
    "locked": "0",
    "storage_paid_at": 0,
    "storage_usage": 806
  }
}

Note: Gas prices can change between blocks. Even for transactions with deterministic gas cost the cost in NEAR could also be different. You can query the gas price for recent blocks using the gas_price RPC endpoint.


Got a question?

Ask it on StackOverflow!

Scroll to Top