This example performs the simplest cross-contract call possible: it calls our Hello NEAR example to set and retrieve a greeting. It is one of the simplest examples on making a cross-contract call, and the perfect gateway to the world of interoperative contracts.
Check the tutorial on how to perform cross-contract calls in batches and in parallel
Starting with the Project
You have two options to start using the project. The first and recommended is to use the app through Gitpod, which will open a web-based interactive environment. The second option is to clone the repository locally, for which you will need to install all the Prerequisites.
- 🦀 Rust
Interacting With the Contract
Since this example does not have a frontend, we will interact with it through the NEAR CLI.
Check the README.md. Briefly, you will need to:
1. Build and Deploy the Contract
You can automatically compile and deploy the contract in the NEAR testnet by running:
Once finished, check the
neardev/dev-account file to find the address in which the contract was deployed:
cat ./contract/neardev/dev-account # e.g. dev-1659899566943-21539992274727
2. Get the Greeting
query_greeting performs a cross-contract call, calling the
get_greeting() method from
Call methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction.
# Use near-cli to ask the contract to query the greeting
near call <dev-account> query_greeting --accountId <dev-account>
The contract exposes methods to query the greeting and change it. These methods do nothing but calling
set_greeting in the
- 🦀 Rust
When writing smart contracts it is very important to test all methods exhaustively. In this
project you have two types of tests: unit and integration. Before digging in them,
go ahead and perform the tests present in the dApp through the command
Unit tests check individual functions in the smart contract. They are written in the same language as the smart contract is.
Since this example handles Cross-contract calls, in the unit tests we only test the
method works. This is because unit tests are cannot test cross-contract calls.
In this project in particular, the integration tests first deploy the
hello-near contract. Then,
they test that the cross-contract call correctly sets and retrieves the message. You will find the integration tests
A nice way to learn is by trying to expand a contract. Modify the cross contract example to use the guest-book contract!. In this way, you can try to make a cross-contract call that attaches money. Remember to correctly handle the callback, and to return the money to the user in case of error.
Advanced Cross Contract Calls
Your contract can perform multiple cross-contract calls in simultaneous, creating promises that execute in parallel, or as a batch transaction. Check the advanced cross contract calls tutorial to learn more.