4-Cross contract calling

Prerequisites

Cargo contract installed: https://polkaverse.com/9768/technical-guide-install-cargo-contract-37738

We are going to reuse the tutorial: https://polkaverse.com/11143/38055

At least on the following node is ready :

This tutorial uses Ubuntu 22.04, cargo 1.68, cargo contract 2.0 and swanky node

Introduction

In the previous tutorial, we implemented the smart contracts Addition and Subtraction. We will reuse it.

In this tutorial we will implement the contract Calculator which call other smart contracts.

There are a few approaches to perform these cross-contract calls in ink!:

  1. Contract references (i.e ContractRef)
  2. Builders (i.e CreateBuilder and CallBuilder)

Contract References

Contract references refer to structs that are generated by the ink! code generation for the purposes of cross-contract calls.

They give developers a type-safe way of interacting with a contract.

A downside to using them is that you need to import the contract you want to call as a dependency of your own contract.

If you want to interact with a contract that is already on-chain you will need to use the Builders approach instead.

Export AdditionRef and SubstractionRef

We need to make sure that the ink! generated Contract Ref is available to other pieces of code.

We do this by re-exporting the contract reference as follows:

In the contract “Addition”

and in the contract “Subtraction”

Implementation - the smart contract "Calculator”

1) Create the project

You can use the command “cargo contract new” to create a new contract based on the template “flipper“.

cargo contract new calculator


2) Add the contracts as dependency in the cargo.toml

To use the Contract References (ie AdditionRef and SubtractionRef), we have to add the crates as dependencies with the feature ink-as-dependency .

And

3) Store the contract references in the storage struct

We store the Contract References (ie AdditionRef and SubtractionRef) in the storage struct to use them in the different methods.

3) Instantiate the contract reference

You can instantiate a contract using the constructor of the AdditionRef.

For the contract “Addition“, we will:

  • call the constructor new of AdditionRef
  • with a given code_hash

Or you can instantiate a contract using the CreateBuilder.

For the contract “Subtraction“, we will:

  • instantiate the uploaded contract with a given code_hash
  • with no gas limit specified (0 means unlimited)
  • instantiating with the new constructor
  • without arguments

Note: You can get the code hash when you upload a contract via the command cargo contract upload (see https://polkaverse.com/11143/37782) .

You can retrieve the code hash from the Contracts UI.

3) Call the contract via the Contract Reference

Use the contract reference (ie AdditionRef and SubtractionRef) to call the contracts.

3) Call the contract via the Call Builder

The CallBuilder gives you a couple of ways to call messages from other contracts.

When using Calls the CallBuilder requires an already instantiated contract.

Here will:

  • make a regular Call
  • to a contract at the given address
  • with no gas limit specified (0 means unlimited)
  • calling the Operator::compute message
  • with 2 arguments of type u128
  • and we expect it to return a value of type u128

4) Build the project

In the directory “calculator“, use the command “cargo contract build” to compile the project.

cargo contract build

5) Deploy the contract

You can deploy your contract in local node or swanky node or a testnet.

Here we will start a swanky node and use Contracts UI to interact with the contract.

The result in Contracts UI:

Conclusion

Here we have see two approaches to perform cross-contract calls in ink!:

  1. Contract references (i.e ContractRef)
  2. Builders (i.e CreateBuilder and CallBuilder)

Interoperability is key and required to build sophisticed applications!

Git repository

https://github.com/GuiGou12358/astar-tutorials/tree/main/tuto4

Source

https://use.ink/basics/cross-contract-calling

0
GuiGouPost author

Crypto-enthusiast, Defi & NFT believer, Dotsam Fam Astar Tech Amb & Phala Amb Web2 builder gradually migrating to Web3

Tutorials to write Smart Contracts in Rust and Ink!

0 comments

Tutorials to write Smart Contracts in Rust and Ink!