How to view data in NEAR protocol contract for free?

1k Views Asked by At

Should I pay for every read from NEAR protocol?

How do I view the value stored in NEAR protocol smart contract? (e.g. staking pool fees)

What is the difference between view and change methods?

1

There are 1 best solutions below

0
On BEST ANSWER

Should I pay for every read from NEAR protocol?

TL;DR: No, you should not.

In NEAR protocol there are to ways to interact with smart contracts:

  1. Submit a transaction with a FunctionCall action, which will get the specified method executed on the chunk producing nodes and the result will be provable through the blockchain (in terms of near-api-js these are "change methods")
  2. Call query(call_function) JSON RPC method, which will get the specified method executed on the RPC node itself in a read-only environment, and the call will never be recorded/proved through the blockchain (in terms of near-api-js these are "view methods")

You can change the state and chained operations (e.g. cross-contract calls, tokens transfer, or access key addition/deletion) only through the first approach since blockchain expects the user to cover the execution costs, so the user should sign their transaction, and they will get charged for the execution.

Sometimes, you don't need to change the state, instead, you only want to read a value stored on the chain, and paying for it is suboptimal (though if you need to prove that the operation has been made it might still be desirable). In this case, you would prefer the second approach. Calling a method through JSON RPC is free of charge and provides a limited context during the contract execution, but it is enough in some scenarios (e.g. when you want to check what is the staking pool fee, or who is the owner of the contract, etc).