Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ website/i18n/*
website/.docusaurus/*
website/package-lock.json
website/yarn.lock
website/pnpm-lock.yaml
neardev
.idea
.docz
Expand Down
82 changes: 82 additions & 0 deletions docs/tutorials/advanced-xcc/0-introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
id: introduction
title: Advanced Cross-Contract Calls on NEAR
sidebar_label: Introduction
description: "Master complex cross-contract call patterns in NEAR Protocol, including batch operations, parallel execution, and advanced error handling."
---

Cross-contract calls are one of NEAR's most powerful features, enabling smart contracts to interact with each other seamlessly. While basic cross-contract calls allow simple interactions, advanced patterns unlock the full potential of NEAR's composable architecture.

## What You'll Build

By the end of this tutorial, you'll master how to:

- **Batch multiple actions** to the same contract with atomic rollback
- **Execute parallel calls** to different contracts simultaneously
- **Handle complex responses** from multiple contract interactions
- **Manage gas efficiently** across multiple contract calls
- **Implement robust error handling** for multi-contract scenarios

## Why Advanced Cross-Contract Calls Matter

These patterns are essential for:

- **DeFi protocols** that interact with multiple token contracts and AMMs
- **Gaming platforms** that manage assets across different contract systems
- **DAO governance** systems that execute proposals across multiple contracts
- **NFT marketplaces** that coordinate with various collection contracts

:::info Source Code

The complete source code is available at [near-examples/cross-contract-calls](https://github.com/near-examples/cross-contract-calls).

Test contracts are deployed on testnet:
- `hello.near-examples.testnet`
- `counter.near-examples.testnet`
- `guestbook.near-examples.testnet`

:::

## How It Works

Advanced cross-contract calls leverage NEAR's promise-based architecture:

```mermaid
graph TD
A[Your Contract] --> B[Batch Actions]
A --> C[Parallel Calls]

B --> D[Same Contract - Action 1]
B --> E[Same Contract - Action 2]
B --> F[Same Contract - Action 3]

C --> G[Contract A]
C --> H[Contract B]
C --> I[Contract C]

D --> J[Single Result]
E --> J
F --> J

G --> K[Array of Results]
H --> K
I --> K
```

Key concepts:

- **Atomicity**: Batch actions either all succeed or all fail
- **Parallelism**: Multiple contracts execute simultaneously
- **Complex responses**: Handle arrays of results with individual success/failure states
- **Gas optimization**: Efficient gas distribution across multiple calls

## What You Will Learn

This tutorial is organized into focused chapters:

1. **[Project Setup](1-setup.md)** - Get the example project running locally
2. **[Batch Actions](2-batch-actions.md)** - Learn atomic multi-action patterns
3. **[Parallel Execution](3-parallel-execution.md)** - Execute multiple contracts simultaneously
5. **[Testing & Deployment](4-testing-deployment.md)** - Test and deploy your contracts

Let's [get started with the project setup](1-setup.md)!
189 changes: 189 additions & 0 deletions docs/tutorials/advanced-xcc/1-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
---
id: setup
title: Setting up the Advanced Cross-Contract Calls Project
sidebar_label: Project Setup
description: "Get the advanced cross-contract calls example project running locally with all necessary dependencies and test contracts."
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

Let's set up the project environment with the main contract and test contracts.

## Obtaining the Project

```bash
git clone https://github.com/near-examples/cross-contract-calls
cd cross-contract-calls
```

Choose your language:

<Tabs>
<TabItem value="js" label="JavaScript" default>

```bash
cd contract-advanced-ts
npm install
```

</TabItem>
<TabItem value="rust" label="Rust">

```bash
cd contract-advanced-rs
cargo check
```

</TabItem>
</Tabs>

## 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 greeting
- `set_greeting(message: string)` - Updates greeting

### Counter Contract
- `get_num()` - Returns current count
- `increment()` - Increases by 1
- `decrement()` - Decreases by 1

### Guest Book Contract
- `add_message(text: string)` - Adds message
- `get_messages()` - Returns all messages

## Building the Contract

<Tabs>
<TabItem value="js" label="JavaScript" default>

```bash
npm run build
# Output: build/cross_contract.wasm
```

</TabItem>
<TabItem value="rust" label="Rust">

```bash
cargo near build
# Output: target/wasm32-unknown-unknown/release/cross_contract.wasm
```

</TabItem>
</Tabs>

## Running Tests

Verify everything works:

<Tabs>
<TabItem value="js" label="JavaScript" default>

```bash
npm test
```

</TabItem>
<TabItem value="rust" label="Rust">

```bash
cargo test
```

</TabItem>
</Tabs>

Expected output:
```
✓ Test batch actions
✓ Test multiple contracts
✓ Test similar contracts
```

## Contract Initialization

The main contract needs external contract addresses:

<Tabs>
<TabItem value="js" label="JavaScript" default>

```typescript
@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;
}
}
```

</TabItem>
<TabItem value="rust" label="Rust">

```rust
#[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,
}
}
}
```

</TabItem>
</Tabs>

## Deploy Your Contract

```bash
# 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](2-batch-actions.md) in the next chapter!
Loading