Nhảy đến nội dung chính

Interacting with Near

The components can use the Near object to interact with smart contracts in the NEAR blockchain. There are three methods:


Near.view

Queries a read-only method from a NEAR smart contract, returning:

  • null: If the query is still being processed
  • undefined: If the query is complete and no value was returned by the contract
  • A value: If the query is complete and a value was returned by the contract
Loading...
Parameters
paramrequiredtypedescription
contractNamerequiredstringName of the smart contract
methodNamerequiredstringName of the method to call
argsoptionalobject instanceArguments to pass to the method
blockId/finalityoptionalstringBlock ID or finality of the transaction
subscribeoptionalbooleanThis feature allows users to subscribe to a query, which automatically refreshes the data for all subscribers every 5 seconds.
tip

Notice that the optional parameter subscribe allows users to subscribe to a query, which automatically refreshes the data every 5 seconds.


Avoiding a Common Pitfall

If you want to initialize the state with the result of a Near.view call, be sure to check first that the value was obtained, to avoid initializing the state with null.

Loading...

If you don't want to delay the render of your component, you can also use the useEffect hook to control the value returned by Near.view

Loading...

Near.call

Calls a smart contract method from the blockchain. Since a transaction needs to be signed, the user must be logged in in order to make the call.

Loading...
Parameters
paramrequiredtypedescription
contractNamerequiredstringName of the smart contract to call
methodNamerequiredstringName of the method to call on the smart contract
argsoptionalobject instanceArguments to pass to the smart contract method as an object instance
gasoptionalstring / numberMaximum amount of gas to be used for the transaction (default 300Tg)
depositoptionalstring / numberAmount of NEAR tokens to attach to the call as deposit (in yoctoNEAR units)
tip

Remember that you can login using the Login button at the navigation bar.


Near.block

Queries a block from the blockchain.

Loading...
Parameters
paramrequiredtypedescription
blockHeightOrFinalityoptionalanyThe block height or finality level to use for the blockchain query (desired block height, or one of the following strings: optimistic, final)
  • desired block height: The height of the specific block to query, expressed as a positive integer
  • optimistic: Uses the latest block recorded on the node that responded to your query (< 1 second delay)
  • final: a block that has been validated on at least 66% of the nodes in the network (approx. 2s)
Was this page helpful?