Interacting with other on-chain programs on Solana
As a developer building an on-chain program on Solana, you are likely familiar with the Interface Definition Language (IDL) for your specific use case. However, interacting with another program requires more than just IDL knowledge. In this article, we will explore how to communicate with other programs and their functionality using the Solana SDKs.
Why do I need more than just IDL?
While IDL provides a high-level abstraction of the functionality of a program, it does not cover low-level details such as contract interactions, event emission, or smart contract invocations. To build robust interactions between programs, you need to leverage the Solana APIs and libraries.
In this article, we will focus on two main topics:
- Interacting with other programs using contracts
: We will discuss how to call functions from another program’s contract using the
call
function.
- Interacting with the Solana blockchain using SDKs: We will show you how to access the Solana blockchain, create accounts, and execute transactions.
Calling functions from another program
To interact with another program, you need to do the following:
- Create a new account in the target contract’s chain: You can create a new account using the « create account » function provided by the target contract.
- Calling a function from another program’s contract using
call
: Use thecall' function to call a function specified in the other program's contract.
Here is an example:
create_account
use solana_program::{account_info::AccountInfo, entry_point};
use solana_sdk::program::contract::ProgramId;
use std::sync::Arc;
// IDL for another program (e.g. data storage contract)
struct AnotherProgramContract {
// Other required fields...
}
impl solana_program::idl::OnIDL for AnotherProgramContract {
fn call(&self, account: &AccountInfo) -> Result
let target_contract = Arc::new(self.clone());
let program_id = ProgramId::from_slice("other_program");
// Call a function from another program's contract
let result = target_contract
.contract()
.call(&[&program_id], &[account])?;
ok (result)
}
}
// Create a new account in the target contract chain
fn create_account(target_program_id: &Program_id) -> Result
// Create a new program context
let context = solana_sdk::program::context::new()?;
// Call
to create a new account
let (result, _) = context.program.create_account(
&[
AccountInfo::new_id(&[]),
ProgramId::from_slice(target_program_id).clone(),
],
&[&target_program_id],
)?;
ok (result)
}
Interacting with the Solana Blockchain
To access the Solana blockchain, create accounts, and execute transactions, you need to do the following:
- Create a new account on the Solana mainnet or devnet: Use the create_account
function provided in the target program contract.
- Executing transactions using SDKs: You can use thecreate_transaction
function provided in the target program contract to execute transactions.
Here is an example:
« rust
use solana_program::idl::idl;
use std::sync::Arc;
// IDL for a simple transaction contract
struct TransactionContract {
// Other required fields…
}
impl solana_program::idl::OnIDL for TransactionContract {
fn call(&self, account: &AccountInfo) -> Result
let target_contract = Arc::new(self.clone());
let program_id = ProgramId::from_slice(« transaction »);
// Execute transaction
let (result, _) = target_contract
.contract()
.call(&[&program_id], &[account])?