Overview page background
What are cross-program invocations on Solana?

What are cross-program invocations on Solana?

Written by Tau

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on 2023-01-255 min read

Runtime cross-program invocation on Solana enables more efficient and flexible program development, making it a key feature of the platform. Cross Program Invocations (CPIs) refer to the ability of a program on the Solana blockchain to call another program and execute its code.

This article will help you understand what cross program invocations on Solana are, their specific use cases, and how you can use them in your Solana programs.

What are cross-program invocations (CPIs)?

In a runtime cross-program invocation, a program on Solana can call another program and pass data to it, allowing for the creation of complex and modular smart contract systems.

The Solana runtime provides a secure and decentralized environment for executing these cross-program invocations. Solana’s runtime ensures that the called program is executed correctly and that the results are returned to the calling program in a verifiable manner. This allows for the creation of trustless and decentralized applications on the Solana blockchain.

What are use cases of cross-program invocations?

Cross-program invocations enable a wide range of use cases on the Solana blockchain, such as multisig transactions, token transfers, state channels, interoperability between dapps, new transaction types, and sharing common functionality. 

1. Multi-signature (Multisig) Transactions

A multi-signature program is where multiple parties must sign a transaction before it is executed on the blockchain. With cross-program invocations, the multisig program can be invoked by other programs to check if a transaction has the required number of signatures before it is processed.

2. Token Transfers

Solana has token programs and accounts for all transactions related to a token. These token programs can be invoked by other programs to transfer tokens from one account to another, allowing for the creation of applications like decentralized exchanges.

3. State Channels

Solana programs can be used to implement state channels, that allow off-chain transactions that are settled on-chain. For example, a program can be used to implement a state channel for a simple blockchain game, where the state program can be invoked by other programs when players make moves and then settle the final state on-chain.

4. Interoperability Between dApps

Cross-program invocations enable dApps to communicate with each other and share data, allowing for more seamless interactions between different dapps on the Solana network.

5. Sharing of Common Functionality

dApps can use cross-program invocations to share common functionality, such as cryptographic functions or data storage. This can reduce the amount of code that each dApp needs to implement, and can make it easier to build dApps on Solana.

6. Enabling New Transaction Types

Cross-program invocations enable new types of interactions between dapps, such as the ability for one dApp to request data from another dapp or trigger an action in another dApp.

Whether you want to enforce complex business logic that involves multiple programs such as in decentralized exchanges, create multi-sig wallets that require multiple signatures from different programs, or build for scale with composable, reusable programs, implementing cross-program invocations is the best approach.

What are some examples of cross-program invocations?

Cross-program invocations can be used in a variety of ways including a multi-sig wallet, NFT marketplace and modular programs.

1. Solana Multisig Wallet

Using cross-program invocations, we can create a multi-sig wallet like Snowflake that requires two signatures to execute a transaction. This can be done by creating two programs: one for each signature.

When a transaction is initiated, the first program verifies the 1st signature, calls the second program to verify the 2nd signature, and if both signatures are valid, the transaction is executed.

2. Solana NFT Marketplace

Cross-program invocations also help in the development of Solana NFT marketplaces for buying and selling non-fungible tokens (NFTs). Developers can accomplish this by creating a program for managing the marketplace, and separate programs for each type of NFT that is traded on the marketplace. The marketplace program can call the appropriate NFT program to verify the uniqueness and ownership of the NFT, and then execute the trade if everything is valid.

3. Modular Programs

Cross-program invocations directly help in creating a complex program that involves multiple steps and interactions. This is done by creating multiple programs, each handling a specific step or aspect of the contract. The main contract program can then call the other programs as needed to execute the various steps of the contract.

How to Run Cross-Program Invocations

Building runtime cross-program invocation on Solana would involve the following steps: writing, compiling, deploying, and invoking.

Step 1: Write Your Solana Programs

First, you would need to write the programs that you want to be able to invoke each other. These programs can be written in any native Rust, using the Anchor framework, or in Python using the Seahorse framework.

Step 2: Compile Your Programs to Wasm

Once you have written the programs, you will need to compile them to Wasm using a suitable compiler. This will produce a Wasm binary that can be executed by the Solana runtime.

Step 3: Deploy Your Programs to Solana

Next, you need to deploy the compiled Wasm binaries to the Solana blockchain. This can be done using the solana-deploy command-line tool, which is provided as part of the Solana software development kit (SDK).

Step 4: Write Transactions to Invoke Your Programs

After deploying the programs, you can use the Solana SDK to write a transaction that invokes one of the programs and pass data to it. This transaction would be submitted to the Solana blockchain, where it would be verified and executed by the runtime environment.

When the called program is executed, it can perform any computation that is possible within the Wasm runtime, including calling other programs. The results of the computation can be returned to the calling program and stored on the Solana blockchain.

Overall, building runtime cross-program invocations on Solana involves writing programs, compiling them to Wasm, deploying them to the blockchain, and using the Solana SDK to invoke and pass data between them. This allows for the creation of complex and modular smart contract systems on the Solana platform.

Note: the Solana program rent fees would vary based on how many programs are present.

Simple Example of the Implementation

As an example, suppose you have two programs in Solana, program1, and program2, and you want to invoke the function process_data in program1 from program2

Here is how you could do this:

In program1, add a new function process_data_interface that takes the input data as a parameter and returns the output data:

fn process_data_interface(input: &[u8]) -> Vec {     let output = process_data(input);     return output; }

In program2, add code to invoke process_data_interface in program1, passing the input data and receiving the output data:

let input = ...;  // obtain input data let output = program1::process_data_interface(&input);

You can then use the output data in program2 as needed.

Note: this is just a simple example, and the exact implementation will depend on the specific programs and functions that you are working with. You may need to add additional code to handle errors, ensure proper authorization, etc.

Get Started with Solana Runtime Cross Program Invocations

Solana has a key feature called runtime Cross Program Invocations that allows smart contracts running on its platform to invoke other programs in a secure and decentralized manner. In Solana, every program has a unique address on the blockchain, and CPIs are achieved by calling the address of the program that you want to execute. This allows programs to interact with each other and enables complex program functionality on the Solana blockchain. 

Overall, Solana runtime CPIs are an important part of the Solana developer skill set, as they allow for the creation of complex and powerful decentralized applications.

If you want to get started on your journey with Solana development and CPIs, sign up for an Alchemy account today!

Overview cards background graphic
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key