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.


This example is purely technical and does not have an frontend

Interacting with the Projectโ€‹

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

GitpodClone locally
Open in Gitpod๐ŸŒ

To try the project you will need to:

  1. Deploy the Hello NEAR contract. A compiled version is available in /test/aux_contracts/hello-near.wasm.
  2. Deploy the Cross Contract Example. A compiled version is available in /out/main.wasm.
  3. Initialize the cross-contract example using the account's address of the hello-near contract deployed in (1).
  4. Call set_greeting and get_greeting in the cross-contract example.

Structure of the Projectโ€‹

The project is organized as follows:

  1. The smart contract code is in the /contract folder.
  2. The compiled smart contract can be found in /out/main.wasm.
  3. A compiled Hello NEAR contract can be found in /test/aux_contracts/hello-near.wasm.


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. For AssemblyScript, you will find the test in the __tests__ folder. If your contract is in Rust you will find the tests at the bottom of each .rs file.

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


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.