I start developing my own NEAR client because I thought that could be a nice personal project. The problem is with call function, because the near doc only mention view function in the page about rpc endpoints. I dont know if there is some special connection for call function or the page abouts call functions with rpc endpoints in not write. But I need know how call a call function.
-
Have you tried this https://docs.near.org/docs/tools/near-cli#near-call? It has an example on how to call a method, not only view it. – John Feb 15 '22 at 05:09
-
@John I know about it, the problem is that I am creating my own client, and the near-cli uses near-api-js which is really messy to read the source code. Btw, I am using c++ to develop the cli, not javascript – Sebasgr Feb 15 '22 at 15:04
1 Answers
Basically, a "change" method is a transaction made from one account (the caller / signer) to the contract account (receiver). Transactions on the NEAR network are constituted by a collection of Actions
. In the case of making a function call, the relevant action is the FunctionCall
action.
Here's a link to the Transaction
documentation: https://docs.near.org/docs/concepts/transaction#transaction.
near-cli-rs, near-call
and near-api-js are tools that simplify this process, but we can step through it in detail to better understand it.
Before we jump in, take a look at this example in the near-jsonrpc-client repo which is a code implementation of the method described here.
First, you need to make sure the access key you have available is either permissioned for FullAccess
or is a FunctionCall
access key permitted to make that function call. See https://near-sdk.io/zero-to-hero/beginner/logging-in#access-keys.
We can start off by viewing the access keys via the query
RPC method.
http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare method=query \
params:='{
"request_type": "view_access_key",
"finality": "final",
"account_id": "signer.testnet",
"public_key": "ed25519:AeKTfwxjMxbroVoW3HuueKdqJN5vHxrXjmoUvK2oCT2D"
}'
The response of which would be something like this;
{
"result": {
"nonce": 80,
"permission": {
"FunctionCall": { ... }
},
...
"block_hash": "87HZEuCyDp8WQd7ixEHLUmM1LwKKAQAqLUiPASWojeqx"
},
}
Seeing as we now have our nonce
and block_hash
, we are good to go.
So, everything we need for our transaction is available. The last little piece is the FunctionCall
action itself, which is an object containing the following fields;
method_name
: the change method of the contract you're calling.args
: JSON-formatted argument list.gas
: https://docs.near.org/docs/concepts/gasdeposit
: (for payable methods) https://www.near-sdk.io/contract-interface/payable-methods
Now, we can see everything start to come together, our transaction now looks something like this;
{
"signer_id": "signer.near",
"public_key": "ed25519:AeKTfwxjMxbroVoW3HuueKdqJN5vHxrXjmoUvK2oCT2D",
"nonce": "<CURRENT_NONCE + 1>",
"receiver_id": "receiver.testnet",
"block_hash": "<LATEST_BLOCK_HASH>",
"actions": [
{
"functionCall": {
"method_name": "greet",
"args": { "name": "John Wick" },
"gas": 100000000000000,
"deposit": 0
}
}
]
}
What's left is to serialize all of that using the borsh binary format with this schema.
Once we've serialized our transaction, we then need to sign it with our private key, convert the bytes to base64 and finally broadcast it using either the broadcast_tx_async
or broadcast_tx_commit
RPC methods.
http post https://rpc.testnet.near.org jsonrpc=2.0 id=dontcare method=broadcast_tx_async \
params:='[
"DwAAAG5lYXJrYXQudGVzdG5ldABuTi5L1rwnlb35hc9tn5WELkxfiGfGh1Q5aeGNQDejo0QAAAAAAAAAEAAAAGpvc2hmb3JkLnRlc3RuZXSiWAc6W9KlqXS5fK+vjFRDV5pAxHRKU0srKX/cmdRTBgEAAAADAAAAoe3MzhvC0wAAAAAAAAB9rOE9zc5zQYLL1j6VTh3I4fQbERs6I07gJfrAC6jo8DB4HolR9Xps3v4qrZxkgZjwv6wB0QOROM4UEbeOaBoB"
]'
Depending on what tools you're using, I recommend you either use the Rust version of the client – near-jsonrpc-client or near-api-js.
Also consider using the interactive near-cli-rs.

- 371
- 3
- 8