Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaddahan committed Sep 2, 2022
0 parents commit 735fd57
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/Cargo.lock
.idea
9 changes: 9 additions & 0 deletions Cargo.toml
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 added src/errors.rs
Empty file.
47 changes: 47 additions & 0 deletions src/lib.rs
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);
}
}
15 changes: 15 additions & 0 deletions src/structs.rs
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>,
}
33 changes: 33 additions & 0 deletions src/utils.rs
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,
})
}

0 comments on commit 735fd57

Please sign in to comment.