복잡한 교차 컨트랙트 호출(Cross Contract Call)
이 예제는 복잡한 교차 컨트랙트 호출의 3가지 인스턴스를 나타냅니다. 특히 다음과 같은 것들을 보여줍니다.
- 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
일괄 Action
동일한 컨트랙트에 대한 여러 작업을 하나의 트랜잭션으로 모을 수 있습니다. 일괄 Action은 순차적으로 실행되며, 하나가 실패 하면 모두 되돌려진다는 추가 이점이 있습니다.
- 🌐 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. 이렇게 하면 모두 병렬로 실행되는 여러 트랜잭션이 생성됩니다. 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). 이는 단순히 promise_result
어레이에 직접 액세스하여 결과를 확인하는 다른 방법을 보여줍니다.
이 경우, 동일한 자료형을 반환하는 여러 컨트랙트를 호출합니다.
- 🌐 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.
- 🌐 JavaScript
- 🦀 Rust
# Optional - create an account
near create-account <accountId> --useFaucet
# Deploy the contract
cd contract-advanced-ts
yarn build
near deploy <accountId> ./build/cross_contract.wasm --initFunction init --initArgs '{"hello_account":"hello.near-example.testnet","guestbook_account":"guestbook_account.near-example.testnet","counter_account":"counter_account.near-example.testnet"}'
# Optional - create an account
near create-account <accountId> --useFaucet
# Deploy the contract
cd contract-advanced-rs
cargo near build
# During deploying pass {"hello_account":"hello.near-example.testnet","guestbook_account":"guestbook_account.near-example.testnet","counter_account":"counter_account.near-example.testnet"} as init arguments
cargo near deploy <accountId>
CLI: Interacting with the Contract
To interact with the contract through the console, you can use the following commands:
# 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
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