Interaction with other SOLANA chain programs
As a developer building a Solana chain program, you are probably familiar with the definition of the language interface (IDL) for your particular case of use. However, interaction with another program requires more than just IDL knowledge. In this article, we will study how to communicate with other programs and interact with their functionality using SLANA SDK.
Why do I need more than just idl?
Although IDL provides an abstraction at a high level of program functionality, it does not cover low -level details such as contract interactions, events or intelligent contract calls. To build stable interactions between programs, you must use API and Solana Libraries.
In this article we will focus on two main topics:
- Interaction with other programs using contracts : We will discuss how to call for functions from the contract of another program using the ‘call
function.
- Interaction with the Solana blockchain using SDKS : We will cover how to access the Solana blockchain, create accounts and make transactions.
Calling features from another program
In order to interact with another program, you must:
- Create a new account in the target contract chain : You can use theCreate_account function provided by the target contract to create a new account.
- Call a function of the other program’s contract using
Call
: Use the Callfunction to call the specified contract function of the other program.
Here's an example:
Rust
Use solana_program :: {Account_info :: Accountinfo, Entry_Point};
Use Solana_sdk :: Program :: Contract :: Programid;
Use STD :: sync :: arc;
// IDL for another program (eg data storage contract)
Structure another ProgramContract {
// Other required fields ...
}
Impl Solana_program :: idl :: ONIDL for another Programconctt {
FN Call (& Self, Account: & Accountinfo) -> Result
Let target_contract = rainbow :: new (Self.clone ());
Let program_id = programid :: from_slice ("ornig_program");
// Call a function of the other program contract
Let the 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) -> Result
// Create a new program context
Let context = solana_sdk :: program :: context :: new ()?;
// CallCreate_accountto 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)
}
Interaction with blockchain Solana
In order to access the Solana blockchain, create accounts and make transactions, you must:
- Create a new SOLANA MAINNET or DEVNET account: Use the ‘Create_account feature provided by the target program contract.
- Make transactions using SDKS
: You can use the
Create_transction
feature provided by the Target Transaction Target Contract.
Here’s an example:
“ Rust
Use solana_program :: idl :: idl;
Use STD :: sync :: arc;
// IDL for a regular transaction contract
Struct TransactionContract {
// Other required fields …
}
Impl Solana_program :: idl :: ONIDL for transaction Conctt {
FN Call (& Self, Account: & Accountinfo) -> Result
Let target_contract = rainbow :: new (Self.clone ());
Let program_id = programid :: from_slice (“transaction”);
// perform a transaction
Let (result, _) = Target_Contract
.Contract ()
.Call (& [& Program_id], & [Account])?