Advanced Cross-Contract Calls
The final part of this tutorial shows 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 written in Rust and has the following structure: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.
- 🦀 Rust (low level)
- 🦀 Rust (high level)
low_level.rs
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.
low_level.rs
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:hello-near and the cross-contract example. They check the results of both high-level and low-level calls. The TestSandbox helper lives in test-utils/mod.rs.
tests.rs
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-simple-rs), build and deploy it:
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 combine promises to execute several actions sequentially or call several contracts in parallel. The advanced examples live in the same cross-contract-calls repository, undercontract-advanced-rs.
Batch actions
You can combine several actions for the same contract into a batch. The actions execute sequentially, and if one fails, they are all reverted.batch_actions.rs
batch_actions.rs
Call multiple contracts in parallel
A contract can call several contracts in parallel. If one call fails, the other calls are not reverted.multiple_contracts.rs
multiple_contracts.rs
similar_contracts.rs
Test and deploy the advanced contract
Run its unit and Sandbox tests locally:If a call reports
Exceeded the prepaid gas, review the gas attached to the child calls and callbacks before increasing the gas on the initiating transaction.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