-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 735fd57
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
/Cargo.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "solana-sandwich" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
solana-program = "1.11.10" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
mod errors; | ||
mod structs; | ||
mod utils; | ||
|
||
use crate::structs::{AllowedRules, InstructionsData}; | ||
use crate::utils::load_instruction_data; | ||
use solana_program::account_info::AccountInfo; | ||
use solana_program::program_error::ProgramError; | ||
use solana_program::sysvar::instructions::load_instruction_at_checked; | ||
|
||
pub fn allowed_only<'a>( | ||
instruction_sysvar_account: &'a AccountInfo<'a>, | ||
rules: Vec<AllowedRules>, | ||
) -> Result<bool, ProgramError> { | ||
let instruction_data = load_instruction_data(instruction_sysvar_account)?; | ||
|
||
for index in 0..instruction_data.num_of_instructions { | ||
let instruction = load_instruction_at_checked(index as usize, instruction_sysvar_account)?; | ||
let rule: &AllowedRules = match rules | ||
.iter() | ||
.find(|rule| rule.program == instruction.program_id) | ||
{ | ||
Some(r) => r, | ||
None => { | ||
return Ok(false); | ||
} | ||
}; | ||
|
||
if !rule.before && index > instruction_data.current_index { | ||
return Ok(false); | ||
} | ||
if !rule.after && index < instruction_data.current_index { | ||
return Ok(false); | ||
} | ||
} | ||
|
||
Ok(true) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
let result = 2 + 2; | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use solana_program::account_info::AccountInfo; | ||
use solana_program::pubkey::Pubkey; | ||
|
||
pub struct AllowedRules { | ||
pub program: Pubkey, | ||
pub before: bool, | ||
pub after: bool, | ||
} | ||
|
||
pub struct InstructionsData<'a> { | ||
pub current_program: Pubkey, | ||
pub current_index: u16, | ||
pub num_of_instructions: u16, | ||
pub instruction_sysvar_account: &'a AccountInfo<'a>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::InstructionsData; | ||
use solana_program::account_info::AccountInfo; | ||
use solana_program::program_error::ProgramError; | ||
use solana_program::serialize_utils::read_u16; | ||
use solana_program::sysvar::instructions::{ | ||
load_current_index_checked, load_instruction_at_checked, | ||
}; | ||
|
||
pub fn load_instruction_data<'a>( | ||
instruction_sysvar_account: &'a AccountInfo<'a>, | ||
) -> Result<InstructionsData<'a>, ProgramError> { | ||
let data = instruction_sysvar_account.try_borrow_data()?; | ||
let current_index = load_current_index_checked(instruction_sysvar_account)?; | ||
let instruction = | ||
load_instruction_at_checked(current_index as usize, instruction_sysvar_account)?; | ||
let mut current = 0; | ||
let num_of_instructions= match read_u16(&mut current, &**data) { | ||
Ok(index) => { | ||
index | ||
} | ||
Err(_) => { | ||
return Err(ProgramError::InvalidArgument); | ||
// return Err(ReadingNumInstructionsError); | ||
} | ||
}; | ||
|
||
Ok(InstructionsData { | ||
current_program: instruction.program_id, | ||
current_index, | ||
num_of_instructions, | ||
instruction_sysvar_account, | ||
}) | ||
} |