Skip to main content

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.

Advanced Cross-Contract Calls

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.

GitpodClone locally
Open in Gitpod๐ŸŒ

Interacting With the Contractโ€‹

Since this example does not have a frontend, we will interact with it through the NEAR CLI.

Check the 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 hello-nearverse.testnet.

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 get_greeting and set_greeting in the hello-near example.



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 yarn test.

Unit testโ€‹

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 initialize method works. This is because unit tests are cannot test cross-contract calls.

Integration testโ€‹

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 integration-tests/.


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.