LogoLogo
Phron AIopenPhronDocs
  • Build with Phron AI
  • Quick Start
    • Rust toolchain
    • Install
    • Developer CLI Tools
  • Learn
    • Why Substrate
    • Ethereum Compatible
    • Governance on PhronAI
  • Use
    • Wallets
    • Explorer
    • Bridge
    • Staking
      • Menu Overview
      • How to Start Staking with the Phron Dashboard
      • How to Change Nominations
      • How to Stop Staking
      • Staking Rewards
    • Validate
    • Dashboard
  • Build with PhronAI
    • SDKs and Tools
      • Ethereum
        • Contracts
        • Libraries
          • Ethers.js
          • Ethers.rs
          • Web3.js
          • Web3.py
        • Dev Environments
          • OpenZeppelin
            • Overview
            • Contract Wizard
            • Defender
          • Ape
          • Brownie
          • Foundry
          • Hardhat
          • Remix
          • Scaffold-PHR
          • Tenderly
          • thirdweb
          • Waffle & Mars
        • Contract Verification
          • PhronScan
        • RPC APIs
          • Standard Ethereum
          • Non-standard Ethereum: Tracing
      • Substrate
        • Libraries
          • Polkadot.js
          • Py substrate interface
        • Dev Environments
          • Chopsticks by Acala
    • Smart Contracts Development
      • Solidity Contracts
        • Phron API
          • Project Overview
          • Prerequisites
          • Steps to Set Up
          • Contract Architecture
          • Contract Functions
          • Testing
          • Deployment
          • Security Considerations
          • Using Foundry Start to End with Phron
          • How to Build a DApp: Complete DApp Architecture
        • Phron Toolkit
          • Libraries
            • Ethers.js
            • Ethers.rs
            • viem
            • Web3.js
            • Web3.py
          • Dev Environments
            • OpenZeppelin
              • Overview
              • Contract Wizard
              • Defender
            • Ape
            • Brownie
            • Foundry
            • Hardhat
            • Remix
            • Scaffold-PHR
            • Tenderly
            • thirdweb
            • Waffle & Mars
          • Verify Contracts
            • PhronScan
          • JSON-RPC APIs
            • Standard Ethereum
            • Non-standard Ethereum: Tracing
      • Rust Contracts
        • Phron smart contracts basics
          • Setting up a Testnet account
          • Installing required tools
          • Creating your first contract
          • Deploying your contract to Phron Testnet
          • Extending your contract
        • Cross contract calls
          • Using references
          • Using dynamic calls
Powered by GitBook
On this page
Export as PDF
  1. Build with PhronAI
  2. Smart Contracts Development
  3. Rust Contracts

Cross contract calls

This section covers the more advanced topic of calling another contract from your code.

PreviousExtending your contractNextUsing references

Before we start, you may want to check out our : all the code you will find in this tutorial is there in its full form. The examples in there are not limited to cross-contract calls and cover almost every aspect of smart contract development, so we encourage you to study it in addition to reading this guide.

The ability of smart contracts to call methods of another contract is arguably one of the most powerful concepts offered by Ink!. For example, a DEX will need to call the transfer_from method of a PSP22 token. However, this is also a feature that requires a lot of care during implementation.

Basics

Ink! offers two ways of calling another contract, each with a different set of trade-offs:

  • Importing a Reference to Another Contract:

    • Convenience: Using a reference to another contract is very convenient.

    • Compiler Assistance: The compiler can check whether the method you want to call exists and if the parameter types match.

    • Limitations: It doesn’t allow you to dynamically construct calls or send tokens with the call.

    Dynamically Building a Call (using the CallBuilder method):

    • Control: Offers a lot of control over the execution.

    • Token Transfer: Allows for sending tokens along with the call.

    • Dynamic Method Selection: Enables dynamically choosing a method to call.

    • Compiler Limitations: The compiler cannot assist in creating a valid call, so you need to be extra careful.

While the choice of a particular method may be a matter of personal preference, we suggest using the references by default and choosing the CallBuilder only if you really need it.

The examples

In the two subsequent sections, we will use the BulletinBoard contract as an example. The contract allows users to post pieces of text ('bulletins') to the board and highlight selected posts. The highlighted posts are tracked by a separate contract: HighlightedPosts.

The split between the two contracts is pretty arbitrary and not really necessary for this logic to work. However, it serves a higher purpose of demonstrating cross-contract calls in an easy-to-understand example.

Bulletin Board Example repository