Skip to content
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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["solana-nostd-entrypoint", "example-program"]
members = ["solana-nostd-entrypoint", "example-program", "system", "token"]
Copy link
Contributor

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 the examples

resolver = "2"


Expand Down
13 changes: 13 additions & 0 deletions system/Cargo.toml
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 }
48 changes: 48 additions & 0 deletions system/src/instructions/advance_nonce_account.rs
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)
}
}
33 changes: 33 additions & 0 deletions system/src/instructions/allocate.rs
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)
}
}
33 changes: 33 additions & 0 deletions system/src/instructions/assign.rs
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)
}
}
43 changes: 43 additions & 0 deletions system/src/instructions/create_account.rs
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)
}
}
5 changes: 5 additions & 0 deletions system/src/instructions/mod.rs
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;
36 changes: 36 additions & 0 deletions system/src/instructions/transfer.rs
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)
}
}
26 changes: 26 additions & 0 deletions system/src/invoke_signed.rs
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this method invoke signed is a bit misleading

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how so? can you elaborate more ser

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/Aursen/nostdinfo-programs/blob/274906c4c3d18f6fa9e3fe8d286106c395ed45b4/crates/invoke/src/lib.rs#L18

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(())
}
11 changes: 11 additions & 0 deletions system/src/lib.rs
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>;
13 changes: 13 additions & 0 deletions token/Cargo.toml
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 }
44 changes: 44 additions & 0 deletions token/src/instructions/close_account.rs
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)
}
}
40 changes: 40 additions & 0 deletions token/src/instructions/initialize_account_3.rs
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)
}
}
Loading