Complex Cross Contract Call
This example presents 3 instances of complex cross-contract calls. Particularly, it shows:
- How to batch multiple function calls to a same contract.
- How to call multiple contracts in parallel, each returning a different type.
- Different ways of handling the responses in the callback.
Check the tutorial on how to use simple cross-contract calls
Obtaining the Cross Contract Call Exampleโ
You have two options to start the Donation Example:
- 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
โ โโโ 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
โโโ 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.
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- batch_actions.ts
Loading...
Loading...
Loading...
Getting the Last Responseโ
In this case, the callback has access to the value returned by the last action from the chain.
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- batch_actions.ts
- utils.ts
Loading...
Loading...
Loading...
Loading...
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.
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- multiple_contracts.ts
Loading...
Loading...
Loading...
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.
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- multiple_contracts.ts
- utils.ts
Loading...
Loading...
Loading...
Loading...
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:
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- similar_contracts.ts
Loading...
Loading...
Loading...
Getting All Responsesโ
In this case, the callback again has access to an array of responses, which we can iterate checking the results.
- ๐ Javascript
- ๐ฆ Rust
- contract.ts
- similar_contracts.ts
- utils.ts
Loading...
Loading...
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-advanced-ts
yarn
yarn test
cd contract-advanced-rs
cargo test
The integration tests
use a sandbox to create NEAR users and simulate interactions with the contract.
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","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:
- Short
- Full
# Execute contracts sequentially
# Replace <accountId> with your account ID
near call <accountId> batch_actions --accountId <accountId> --gas 300000000000000
# Execute contracts in parallel
# Replace <accountId> with your account ID
near call <accountId> multiple_contracts --accountId <accountId> --gas 300000000000000
# Execute multiple instances of the same contract in parallel
# Replace <accountId> with your account ID
near call <accountId> similar_contracts --accountId <accountId> --gas 300000000000000
# Execute contracts sequentially
# Replace <accountId> with your account ID
near contract call-function as-transaction <accountId> batch_actions json-args '{}' prepaid-gas '300.0 Tgas' attached-deposit '0 NEAR' sign-as <accountId> network-config testnet sign-with-keychain send
# Execute contracts in parallel
# Replace <accountId> with your account ID
near contract call-function as-transaction <accountId> multiple_contracts json-args '{}' prepaid-gas '300.0 Tgas' attached-deposit '0 NEAR' sign-as <accountId> network-config testnet sign-with-keychain send
# Execute multiple instances of the same contract in parallel
# Replace <accountId> with your account ID
near contract call-function as-transaction <accountId> similar_contracts json-args '{}' prepaid-gas '300.0 Tgas' attached-deposit '0 NEAR' sign-as <accountId> network-config testnet sign-with-keychain send
If at some point you get an "Exceeded the prepaid gas" error, try to increase the gas amount used within the functions when calling other contracts
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