Complex Cross Contract Call

To Share and +4 nLEARNs

This example presents 3 instances of complex cross-contract calls. Particularly, it shows:

  1. How to batch multiple function calls to a same contract.
  2. How to call multiple contracts in parallel, each returning a different type.
  3. Different ways of handling the responses in the callback.

Obtaining the Cross Contract Call Example

You have two options to start the Donation Example:

  1. You can use the app through Github Codespaces, which will open a web-based interactive environment.
  2. Clone the repository locally and use it from your computer.
Codespaces Clone locally
Open in GitHub Codespaces 🌠https://github.com/near-examples/cross-contract-calls

Structure of the Example

The smart contract is available in two flavors: Rust and JavaScript

“`bash
┌── sandbox-ts # sandbox testing
│ ├── external-contracts
│ │ ├── counter.wasm
│ │ ├── guest-book.wasm
│ │ └── hello-near.wasm
│ └── main.ava.ts
├── src # contract’s code
│ ├── internal
│ │ ├── batch_actions.ts
│ │ ├── constants.ts
│ │ ├── multiple_contracts.ts
│ │ ├── similar_contracts.ts
│ │ └── utils.ts
│ └── contract.ts
├── package.json
├── README.md
└── tsconfig.json
“`

“`bash
┌── tests # sandbox testing
│ ├── external-contracts
│ │ ├── counter.wasm
│ │ ├── guest-book.wasm
│ │ └── hello-near.wasm
│ └── main.ava.ts
├── src # contract’s code
│ ├── batch_actions.rs
│ ├── lib.rs
│ ├── multiple_contracts.rs
│ └── similar_contracts.rs
├── Cargo.toml # package manager
├── README.md
└── rust-toolchain.toml
“`


Smart Contract

Batch Actions

You can aggregate multiple actions directed towards one same contract into a batched transaction.
Methods called this way are executed sequentially, with the added benefit that, if one fails then
they all get reverted.









Getting the Last Response

In this case, the callback has access to the value returned by the last
action
from the chain.











Calling Multiple Contracts

A contract can call multiple other contracts. This creates multiple transactions that execute
all in parallel. If one of them fails the rest ARE NOT REVERTED.









Getting All Responses

In this case, the callback has access to an array of responses, which have either the
value returned by each call, or an error message.











Multiple Calls – Same Result Type

This example is a particular case of the previous one (Calling Multiple Contracts).
It simply showcases a different way to check the results by directly accessing the promise_result array.

In this case, we call multiple contracts that will return the same type:









Getting All Responses

In this case, the callback again has access to an array of responses, which we can iterate checking the
results.











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:


“`bash
cd contract-advanced-ts
yarn
yarn test
“`


“`bash
cd contract-advanced-rs
cargo test
“`


Deploying the Contract to the NEAR network

In order to deploy the contract you will need to create a NEAR account.


“`bash
# Create a new account pre-funded by a faucet
near create-account –useFaucet
“`

“`bash
# Create a new account pre-funded by a faucet
near account create-account sponsor-by-faucet-service .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","guestbook_account":"guestbook_account.near-example.testnet","counter_account":"counter_account.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:


“`bash
# Execute contracts sequentially
# Replace with your account ID
near call batch_actions –accountId –gas 300000000000000

# Execute contracts in parallel
# Replace with your account ID
near call multiple_contracts –accountId –gas 300000000000000

# Execute multiple instances of the same contract in parallel
# Replace with your account ID
near call similar_contracts –accountId –gas 300000000000000
“`

“`bash
# Execute contracts sequentially
# Replace with your account ID
near contract call-function as-transaction batch_actions json-args ‘{}’ prepaid-gas ‘300.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as network-config testnet sign-with-keychain send

# Execute contracts in parallel
# Replace with your account ID
near contract call-function as-transaction multiple_contracts json-args ‘{}’ prepaid-gas ‘300.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as network-config testnet sign-with-keychain send

# Execute multiple instances of the same contract in parallel
# Replace with your account ID
near contract call-function as-transaction similar_contracts json-args ‘{}’ prepaid-gas ‘300.0 Tgas’ attached-deposit ‘0 NEAR’ sign-as network-config testnet sign-with-keychain send
“`

Generate comment with AI 2 nL
Scroll to Top