Setting up the Advanced Cross-Contract Calls Project
Let's set up the project environment with the main contract and test contracts.
Obtaining the Project
git clone https://github.com/near-examples/cross-contract-calls
cd cross-contract-calls
Choose your language:
- JavaScript
- Rust
cd contract-advanced-ts
npm install
cd contract-advanced-rs
cargo check
Project Structure
├── tests/ # Test environment
│ └── external-contracts/ # Pre-compiled contracts
│ ├── counter.wasm # Simple counter
│ ├── guest-book.wasm # Message storage
│ └── hello-near.wasm # Greeting contract
├── src/ # Main contract code
│ ├── batch_actions.* # Batch operations
│ ├── multiple_contracts.* # Parallel execution
│ └── similar_contracts.* # Same-type calls
└── README.md
Test Contracts Overview
Hello NEAR Contract
get_greeting()- Returns current greetingset_greeting(message: string)- Updates greeting
Counter Contract
get_num()- Returns current countincrement()- Increases by 1decrement()- Decreases by 1
Guest Book Contract
add_message(text: string)- Adds messageget_messages()- Returns all messages
Building the Contract
- JavaScript
- Rust
npm run build
# Output: build/cross_contract.wasm
cargo near build
# Output: target/wasm32-unknown-unknown/release/cross_contract.wasm
Running Tests
Verify everything works:
- JavaScript
- Rust
npm test
cargo test
Expected output:
✓ Test batch actions
✓ Test multiple contracts
✓ Test similar contracts
Contract Initialization
The main contract needs external contract addresses:
- JavaScript
- Rust
@NearBindgen({})
export class CrossContractCall {
hello_account: AccountId;
counter_account: AccountId;
guestbook_account: AccountId;
@initialize({})
init({
hello_account,
counter_account,
guestbook_account,
}: {
hello_account: AccountId;
counter_account: AccountId;
guestbook_account: AccountId;
}) {
this.hello_account = hello_account;
this.counter_account = counter_account;
this.guestbook_account = guestbook_account;
}
}
#[near_bindgen]
impl Contract {
#[init]
pub fn init(
hello_account: AccountId,
counter_account: AccountId,
guestbook_account: AccountId,
) -> Self {
Self {
hello_account,
counter_account,
guestbook_account,
}
}
}
Deploy Your Contract
# Create your account
near account create-account sponsor-by-faucet-service xcc.YOUR_NAME.testnet autogenerate-new-keypair save-to-keychain network-config testnet create
# Deploy with initialization
near contract deploy xcc.YOUR_NAME.testnet use-file ./build/cross_contract.wasm with-init-call init json-args '{
"hello_account":"hello.near-examples.testnet",
"counter_account":"counter.near-examples.testnet",
"guestbook_account":"guestbook.near-examples.testnet"
}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' network-config testnet sign-with-keychain send
Now let's explore batch actions in the next chapter!