-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add instructions for token and system program #12
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "system" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[dependencies] | ||
solana-program = { workspace = true } | ||
solana-nostd-entrypoint = { workspace = true } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use solana_nostd_entrypoint::{InstructionC, NoStdAccountInfo}; | ||
|
||
use crate::{invoke_signed::invoke_signed, ProgramResult}; | ||
|
||
pub struct AdvanceNonceAccount<'a> { | ||
/// Nonce account. | ||
pub account: &'a NoStdAccountInfo, | ||
|
||
/// RecentBlockhashes sysvar. | ||
pub recent_blockhashes_sysvar: &'a NoStdAccountInfo, | ||
|
||
/// Nonce authority. | ||
pub authority: &'a NoStdAccountInfo, | ||
} | ||
|
||
impl<'a> AdvanceNonceAccount<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = [ | ||
self.account.to_meta_c(), | ||
self.recent_blockhashes_sysvar | ||
.to_meta_c(), | ||
self.authority.to_meta_c_signer(), | ||
]; | ||
|
||
let account_infos = [ | ||
self.account.to_info_c(), | ||
self.recent_blockhashes_sysvar | ||
.to_info_c(), | ||
self.authority.to_info_c(), | ||
]; | ||
|
||
let mut instruction_data = [0; 4]; | ||
instruction_data[0] = 4; | ||
|
||
let instruction = InstructionC { | ||
program_id: &crate::ID, | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use solana_nostd_entrypoint::{InstructionC, NoStdAccountInfo}; | ||
|
||
use crate::{invoke_signed::invoke_signed, ProgramResult}; | ||
|
||
pub struct Allocate<'a> { | ||
pub account: &'a NoStdAccountInfo, | ||
pub space: u64, | ||
} | ||
|
||
impl<'a> Allocate<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = [self.account.to_meta_c_signer()]; | ||
let account_infos = [self.account.to_info_c()]; | ||
|
||
let mut instruction_data = [0; 12]; | ||
instruction_data[0] = 8; | ||
instruction_data[4..12] | ||
.copy_from_slice(&self.space.to_le_bytes()); | ||
|
||
let instruction = InstructionC { | ||
program_id: &crate::ID, | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use solana_nostd_entrypoint::{InstructionC, NoStdAccountInfo}; | ||
use solana_program::pubkey::Pubkey; | ||
|
||
use crate::{invoke_signed::invoke_signed, ProgramResult}; | ||
|
||
pub struct Assign<'a> { | ||
pub account: &'a NoStdAccountInfo, | ||
pub owner: &'a Pubkey, | ||
} | ||
|
||
impl<'a> Assign<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = [self.account.to_meta_c_signer()]; | ||
let account_infos = [self.account.to_info_c()]; | ||
|
||
let mut instruction_data = [0; 36]; | ||
instruction_data[0] = 1; | ||
instruction_data[4..36].copy_from_slice(self.owner.as_ref()); | ||
|
||
let instruction = InstructionC { | ||
program_id: &crate::ID, | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use solana_nostd_entrypoint::{ | ||
solana_program::pubkey::Pubkey, InstructionC, NoStdAccountInfo, | ||
}; | ||
|
||
use crate::{invoke_signed::invoke_signed, ProgramResult}; | ||
|
||
pub struct CreateAccount<'a> { | ||
pub from: &'a NoStdAccountInfo, | ||
pub to: &'a NoStdAccountInfo, | ||
pub lamports: u64, | ||
pub space: u64, | ||
pub owner: &'a Pubkey, | ||
} | ||
|
||
impl<'a> CreateAccount<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = | ||
[self.from.to_meta_c_signer(), self.to.to_meta_c_signer()]; | ||
let account_infos = | ||
[self.from.to_info_c(), self.to.to_info_c()]; | ||
|
||
let mut instruction_data = [0; 52]; | ||
// create account instruction has a '0' discriminator | ||
instruction_data[4..12] | ||
.copy_from_slice(&self.lamports.to_le_bytes()); | ||
instruction_data[12..20] | ||
.copy_from_slice(&self.space.to_le_bytes()); | ||
instruction_data[20..52].copy_from_slice(self.owner.as_ref()); | ||
|
||
let instruction = InstructionC { | ||
program_id: &crate::ID, | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_slice().as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod allocate; | ||
pub mod assign; | ||
pub mod create_account; | ||
pub mod transfer; | ||
pub mod advance_nonce_account; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use solana_nostd_entrypoint::{InstructionC, NoStdAccountInfo}; | ||
|
||
use crate::{invoke_signed::invoke_signed, ProgramResult}; | ||
|
||
pub struct Transfer<'a> { | ||
pub from: &'a NoStdAccountInfo, | ||
pub to: &'a NoStdAccountInfo, | ||
pub lamports: u64, | ||
} | ||
|
||
impl<'a> Transfer<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = | ||
[self.from.to_meta_c_signer(), self.to.to_meta_c()]; | ||
let account_infos = | ||
[self.from.to_info_c(), self.to.to_info_c()]; | ||
|
||
let mut instruction_data = [0; 12]; | ||
instruction_data[0] = 2; | ||
instruction_data[4..12] | ||
.copy_from_slice(&self.lamports.to_le_bytes()); | ||
|
||
let instruction = InstructionC { | ||
program_id: &crate::ID, | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_slice().as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use solana_nostd_entrypoint::{AccountInfoC, InstructionC}; | ||
|
||
use crate::ProgramResult; | ||
|
||
pub fn invoke_signed( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling this method invoke signed is a bit misleading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how so? can you elaborate more ser There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, in the verified call, we check whether the metas accounts agree with the accounts in the instruction. Since .to_metas-C is account-based, I don't know if it's accurate in this context @cavemanloverboy any ideas? |
||
instruction: &InstructionC, | ||
infos: &[AccountInfoC], | ||
seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
#[cfg(target_os = "solana")] | ||
unsafe { | ||
solana_program::syscalls::sol_invoke_signed_c( | ||
instruction as *const InstructionC as *const u8, | ||
infos.as_ptr() as *const u8, | ||
infos.len() as u64, | ||
seeds.as_ptr() as *const u8, | ||
seeds.len() as u64, | ||
); | ||
} | ||
|
||
// For clippy | ||
#[cfg(not(target_os = "solana"))] | ||
core::hint::black_box(&(&instruction, &infos, &seeds)); | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use solana_nostd_entrypoint::solana_program::pubkey::Pubkey; | ||
use solana_program::program_error::ProgramError; | ||
|
||
pub mod instructions; | ||
pub mod invoke_signed; | ||
|
||
pub const ID: Pubkey = solana_nostd_entrypoint::solana_program::pubkey!( | ||
"11111111111111111111111111111111" | ||
); | ||
|
||
pub type ProgramResult = Result<(), ProgramError>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "token" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[dependencies] | ||
solana-program = { workspace = true } | ||
solana-nostd-entrypoint = { workspace = true } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use solana_nostd_entrypoint::{ | ||
solana_program::entrypoint::ProgramResult, InstructionC, | ||
NoStdAccountInfo, | ||
}; | ||
|
||
use crate::invoke_signed::invoke_signed; | ||
|
||
pub struct CloseAccount<'a> { | ||
pub token_program: &'a NoStdAccountInfo, | ||
pub account: &'a NoStdAccountInfo, | ||
pub destination: &'a NoStdAccountInfo, | ||
pub authority: &'a NoStdAccountInfo, | ||
} | ||
|
||
impl<'a> CloseAccount<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let instruction_data = &[9]; | ||
|
||
let account_metas = [ | ||
self.account.to_meta_c(), | ||
self.destination.to_meta_c(), | ||
self.authority.to_meta_c_signer(), | ||
]; | ||
|
||
let account_infos = [ | ||
self.account.to_info_c(), | ||
self.destination.to_info_c(), | ||
self.authority.to_info_c(), | ||
]; | ||
|
||
let instruction = InstructionC { | ||
program_id: self.token_program.key(), | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use solana_nostd_entrypoint::{ | ||
solana_program::entrypoint::ProgramResult, | ||
solana_program::pubkey::Pubkey, InstructionC, NoStdAccountInfo, | ||
}; | ||
|
||
use crate::invoke_signed::invoke_signed; | ||
|
||
pub struct InitializeAccount3<'a> { | ||
pub token_program: &'a NoStdAccountInfo, | ||
pub token: &'a NoStdAccountInfo, | ||
pub mint: &'a NoStdAccountInfo, | ||
pub owner: &'a Pubkey, | ||
} | ||
|
||
impl<'a> InitializeAccount3<'a> { | ||
pub fn invoke_signed( | ||
&self, | ||
signer_seeds: &[&[&[u8]]], | ||
) -> ProgramResult { | ||
let account_metas = | ||
[self.token.to_meta_c(), self.mint.to_meta_c()]; | ||
let account_infos = | ||
[self.token.to_info_c(), self.mint.to_info_c()]; | ||
|
||
// Preallocate vec with exact size: 1 (discriminator) + 32 (pubkey) | ||
let mut instruction_data = Vec::with_capacity(33); | ||
instruction_data.push(18); | ||
instruction_data.extend_from_slice(self.owner.as_ref()); | ||
|
||
let instruction = &InstructionC { | ||
program_id: self.token_program.key(), | ||
accounts: account_metas.as_ptr(), | ||
accounts_len: account_metas.len() as u64, | ||
data: instruction_data.as_slice().as_ptr(), | ||
data_len: instruction_data.len() as u64, | ||
}; | ||
|
||
invoke_signed(&instruction, &account_infos, signer_seeds) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe here organize to use the
crates
folder and theexamples