Advanced Cross-Contract Calls
Check the tutorial on how to perform cross-contract calls in batches and in parallel
Clone the Example
You have two options to start the project:- You can use the app through
Github Codespaces, which will open a web-based interactive environment. - Clone the repository locally and use it from your computer.
Structure of the Example
The smart contract is available in two flavors: Rust and JavaScript- 🌐 JavaScript
- 🦀 Rust
Smart Contract
The contract exposes methods to query the greeting and change it. These methods do nothing but callingget_greeting and set_greeting in the hello-near example.
Querying for the Greeting
The contract performs a cross-contract call tohello.near-example.testnet to get the greeting message, and then handles the response in a callback function.
- 🌐 JavaScript
- 🦀 Rust (low level)
- 🦀 Rust (high level)
Callback Function
The callback function processes the result of the cross-contract call. In this case, it simply returns the greeting message obtained from thehello-near contract.
Notice that the callback function is marked as private, meaning it can only be called by the contract itself.
- 🌐 JavaScript
- 🦀 Rust
Testing the Contract
The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands:- 🌐 JavaScript
- 🦀 Rust
hello-near contract. Then,
they test that the cross-contract call correctly sets and retrieves the message. You will find the integration tests
in sandbox-ts/ for the JavaScript version and in tests/ for the Rust version.
Deploying the Contract to the NEAR network
In order to deploy the contract you will need to create a NEAR account.- Short
- Full
cd contract-advanced-ts or cd contract-advanced-rs), build and deploy it:
- 🌐 JavaScript
- 🦀 Rust
CLI: Interacting with the Contract
To interact with the contract through the console, you can use the following commands:- Short
- Full
Moving Forward
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.Versioning for this articleAt the time of this writing, this example works with the following versions:
- near-cli:
4.0.13 - node:
18.19.1 - rustc:
1.77.0