Skip to content

fazzatti/stellar-cross-contract-example

Repository files navigation

Cross-Contract Example

This repo demonstrates cross-contract calls in Soroban smart contracts with different execution patterns and authorization requirements. It showcases how contracts can invoke other contracts in tree-like structures.

Requirements

  • Deno - Modern runtime for JavaScript and TypeScript
  • Stellar CLI - The command line interface to Stellar smart contracts

Setup

  1. Copy the environment configuration:
cp .env.example .env

The .env is configured by default to use testnet. Feel free to adjust its parameters to use a different target network.

  1. Build the contract:
stellar contract build

Examples

This project includes three different cross-contract execution patterns:

1. Simple Chain (simple)

A basic linear chain of contract calls:

A -> B -> C
  • A calls B
  • B calls C
  • C is a leaf node
  • No authorization required

Deploy and run:

deno task deploy:simple
deno task call:simple

2. Complex Tree (no-auth)

A complex tree structure with multiple branches:

                    A
                 /  |  \
                B   C   D
               /|   |   |\
              E F   G   H I
                        |
                        J
  • A calls B, C, D
  • B calls E, F
  • C calls G
  • D calls H, I
  • H calls J
  • E, F, G, I, J are leaf nodes
  • No authorization required

Deploy and run:

deno task deploy:no-auth
deno task call:no-auth

3. Auth Tree (auth)

A balanced tree with distributed authorization requirements:

                         A (u1)
                    /         \
               B (u2)           C
              /       \         /  \
         D (u3)        E       F    G (u1, u3)
         /     \      /       /      \
        H       I    J (u2)  K        L (u2)
  • A calls B, C (requires user1 authorization)
  • B calls D, E (requires user2 authorization)
  • C calls F, G (no auth required)
  • D calls H, I (requires user3 authorization)
  • E calls J (no auth required)
  • F calls K (no auth required)
  • G calls L (requires user1 + user3 authorization)
  • H, I, J, K, L are leaf nodes

Authorization Summary:

  • A: requires u1
  • B: requires u2
  • D: requires u3
  • G: requires u1 + u3 (multi-signature)
  • J: requires u2
  • L: requires u2

Deploy and run:

deno task deploy:auth
deno task call:auth

How It Works

Each contract instance is configured during deployment with:

  • Next contracts: Which contracts to call when execute() is invoked
  • Auth addresses: Which user addresses must provide authorization

When you call execute() on the root contract:

  1. It checks for required authorizations
  2. Calls all configured next contracts
  3. Each next contract repeats the process
  4. Returns a list of all contract addresses that were invoked in the execution chain

The contract source lives at contracts/cross/src/contract.rs and implements a flexible pattern for cross-contract invocation testing.

Output

Each example will show:

  • The tree structure being executed
  • All contract addresses in the execution order
  • Authorization requirements (for auth example)
  • User keypairs (for auth example)

This allows you to test different cross-contract call patterns and understand how authorization flows through contract invocation chains in Soroban.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published