Crates.io | solana-system-interface |
lib.rs | solana-system-interface |
version | 1.0.0 |
source | src |
created_at | 2024-11-21 17:53:24.336912 |
updated_at | 2024-11-21 17:53:24.336912 |
description | Instructions and constructors for the System program |
homepage | https://anza.xyz/ |
repository | https://github.com/solana-program/system |
max_upload_size | |
id | 1456505 |
size | 78,086 |
This crate contains instructions and constructors for interacting with the System program.
The System program can be used to create new accounts, allocate account data, assign accounts to owning programs, transfer lamports from System Program owned accounts and pay transaction fees.
From your project folder:
cargo add solana-system-interface --features bincode
This will add the solana-system-interface
dependency with the bincode
feature enabled to your Cargo.toml
file. The bincode
feature contains the instruction constructors to create instructions for the System program.
Creating an account:
use solana_rpc_client::rpc_client::RpcClient;
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction;
use anyhow::Result;
fn create_account(
client: &RpcClient,
payer: &Keypair,
new_account: &Keypair,
owning_program: &Pubkey,
space: u64,
) -> Result<()> {
let rent = client.get_minimum_balance_for_rent_exemption(space.try_into()?)?;
let instr = instruction::create_account(
&payer.pubkey(),
&new_account.pubkey(),
rent,
space,
owning_program,
);
let blockhash = client.get_latest_blockhash()?;
let tx = Transaction::new_signed_with_payer(
&[instr],
Some(&payer.pubkey()),
&[payer, new_account],
blockhash,
);
let _sig = client.send_and_confirm_transaction(&tx)?;
Ok(())
}
Transfer lamports between accounts:
use solana_rpc_client::rpc_client::RpcClient;
use solana_pubkey::Pubkey;
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction;
use anyhow::Result;
fn transfer(
client: &RpcClient,
from: &Keypair,
recipient: &Pubkey,
lamports: u64,
) -> Result<()> {
let instr = instruction::transfer(
&from.pubkey(),
recipient,
lamports,
);
let blockhash = client.get_latest_blockhash()?;
let tx = Transaction::new_signed_with_payer(
&[instr],
Some(&from.pubkey()),
&[from],
blockhash,
);
let _sig = client.send_and_confirm_transaction(&tx)?;
Ok(())
}
More examples can be found on the crate documentation.