Cross Contract Call
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
Obtaining the Cross Contract Call 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.
Codespaces | Clone locally |
---|---|
๐ https://github.com/near-examples/cross-contract-calls |
Structure of the Exampleโ
The smart contract is available in two flavors: Rust and JavaScript
- ๐ JavaScript
- ๐ฆ Rust
โโโ sandbox-ts # sandbox testing
โ โโโ hello-near
โ โ โโโ hello-near.wasm
โ โโโ main.ava.ts
โโโ src # contract's code
โ โโโ contract.ts
โโโ package.json
โโโ README.md
โโโ tsconfig.json
โโโ tests # sandbox testing
โ โโโ hello-near
โ โ โโโ hello-near.wasm
โ โโโ tests.rs
โโโ src # contract's code
โ โโโ external.rs
โ โโโ lib.rs
โโโ Cargo.toml # package manager
โโโ README.md
โโโ rust-toolchain.toml
Smart Contractโ
Contractโ
The contract exposes methods to query the greeting and change it. These methods do nothing but calling get_greeting
and
set_greeting
in the hello-near
example.
- ๐ Javascript
- ๐ฆ Rust
Loading...
- lib.rs
- external.rs
Loading...
Loading...
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
cd contract-simple-ts
yarn
yarn test
cd contract-simple-rs
cargo test
The integration tests
use a sandbox to create NEAR users and simulate interactions with the contract.
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
in sandbox-ts/
for the JavaScript version and in tests/
for the Rust version.
- ๐ Javascript
- ๐ฆ Rust
Loading...
Loading...
Deploying the Contract to the NEAR networkโ
In order to deploy the contract you will need to create a NEAR account.
- Short
- Full
# Create a new account pre-funded by a faucet
near create-account <accountId> --useFaucet
# Create a new account pre-funded by a faucet
near account create-account sponsor-by-faucet-service <my-new-dev-account>.testnet autogenerate-new-keypair save-to-keychain network-config testnet create
Go into the directory containing the smart contract (cd contract-advanced-ts
or cd contract-advanced-rs
), build and deploy it:
cargo near build
cargo near deploy <accountId> with-init-call new json-args '{"hello_account":"hello.near-example.testnet"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' network-config testnet sign-with-keychain send
CLI: Interacting with the Contractโ
To interact with the contract through the console, you can use the following commands:
- Short
- Full
# Get message from the hello-near contract
# Replace <accountId> with your account ID
near call <accountId> query_greeting --accountId <accountId>
# Set a new message for the hello-near contract
# Replace <accountId> with your account ID
near call <accountId> change_greeting '{"new_greeting":"XCC Hi"}' --accountId <accountId>
# Get message from the hello-near contract
# Replace <accountId> with your account ID
near contract call-function as-transaction <accountId> query_greeting json-args '{}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as <accountId> network-config testnet sign-with-keychain send
# Set a new message for the hello-near contract
# Replace <accountId> with your account ID
near contract call-function as-transaction <accountId> change_greeting json-args '{"new_greeting":"XCC Hi"}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' sign-as <accountId> network-config testnet sign-with-keychain send
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.
At 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