-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
18 changed files
with
745 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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,42 @@ | ||
use solana_program::{instruction::InstructionError, program_error::ProgramError}; | ||
use solana_program_test::BanksClientError; | ||
use solana_sdk::transaction::TransactionError; | ||
use thiserror::Error; | ||
|
||
pub mod restaking_client; | ||
pub mod test_builder; | ||
pub mod tip_router_client; | ||
pub mod vault_client; | ||
|
||
pub type TestResult<T> = Result<T, TestError>; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum TestError { | ||
#[error(transparent)] | ||
BanksClientError(#[from] BanksClientError), | ||
#[error(transparent)] | ||
ProgramError(#[from] ProgramError), | ||
} | ||
|
||
impl TestError { | ||
pub fn to_transaction_error(&self) -> Option<TransactionError> { | ||
match self { | ||
TestError::BanksClientError(e) => match e { | ||
BanksClientError::TransactionError(e) => Some(e.clone()), | ||
BanksClientError::SimulationError { err, .. } => Some(err.clone()), | ||
_ => None, | ||
}, | ||
TestError::ProgramError(_) => None, | ||
} | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
#[track_caller] | ||
pub fn assert_ix_error<T>(test_error: Result<T, TestError>, ix_error: InstructionError) { | ||
assert!(test_error.is_err()); | ||
assert_eq!( | ||
test_error.err().unwrap().to_transaction_error().unwrap(), | ||
TransactionError::InstructionError(0, ix_error) | ||
); | ||
} |
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,132 @@ | ||
use jito_restaking_core::{config::Config, ncn::Ncn}; | ||
use jito_restaking_sdk::sdk::{initialize_config, initialize_ncn}; | ||
use solana_program_test::BanksClient; | ||
use solana_sdk::{ | ||
commitment_config::CommitmentLevel, native_token::sol_to_lamports, pubkey::Pubkey, | ||
signature::Keypair, signer::Signer, system_instruction::transfer, transaction::Transaction, | ||
}; | ||
|
||
use super::TestResult; | ||
|
||
#[derive(Debug)] | ||
pub struct NcnRoot { | ||
pub ncn_pubkey: Pubkey, | ||
pub ncn_admin: Keypair, | ||
} | ||
|
||
pub struct RestakingProgramClient { | ||
banks_client: BanksClient, | ||
payer: Keypair, | ||
} | ||
|
||
impl RestakingProgramClient { | ||
pub const fn new(banks_client: BanksClient, payer: Keypair) -> Self { | ||
Self { | ||
banks_client, | ||
payer, | ||
} | ||
} | ||
|
||
pub async fn process_transaction(&mut self, tx: &Transaction) -> TestResult<()> { | ||
self.banks_client | ||
.process_transaction_with_preflight_and_commitment( | ||
tx.clone(), | ||
CommitmentLevel::Processed, | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn _airdrop(&mut self, to: &Pubkey, sol: f64) -> TestResult<()> { | ||
let blockhash = self.banks_client.get_latest_blockhash().await?; | ||
self.banks_client | ||
.process_transaction_with_preflight_and_commitment( | ||
Transaction::new_signed_with_payer( | ||
&[transfer(&self.payer.pubkey(), to, sol_to_lamports(sol))], | ||
Some(&self.payer.pubkey()), | ||
&[&self.payer], | ||
blockhash, | ||
), | ||
CommitmentLevel::Processed, | ||
) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn do_initialize_config(&mut self) -> TestResult<Keypair> { | ||
let restaking_config_pubkey = Config::find_program_address(&jito_restaking_program::id()).0; | ||
let restaking_config_admin = Keypair::new(); | ||
|
||
self._airdrop(&restaking_config_admin.pubkey(), 1.0).await?; | ||
self.initialize_config(&restaking_config_pubkey, &restaking_config_admin) | ||
.await?; | ||
|
||
Ok(restaking_config_admin) | ||
} | ||
|
||
pub async fn initialize_config( | ||
&mut self, | ||
config: &Pubkey, | ||
config_admin: &Keypair, | ||
) -> TestResult<()> { | ||
let blockhash = self.banks_client.get_latest_blockhash().await?; | ||
self.process_transaction(&Transaction::new_signed_with_payer( | ||
&[initialize_config( | ||
&jito_restaking_program::id(), | ||
config, | ||
&config_admin.pubkey(), | ||
&jito_vault_program::id(), | ||
)], | ||
Some(&config_admin.pubkey()), | ||
&[config_admin], | ||
blockhash, | ||
)) | ||
.await | ||
} | ||
|
||
pub async fn do_initialize_ncn(&mut self) -> TestResult<NcnRoot> { | ||
let ncn_admin = Keypair::new(); | ||
let ncn_base = Keypair::new(); | ||
|
||
self._airdrop(&ncn_admin.pubkey(), 1.0).await?; | ||
|
||
let ncn_pubkey = | ||
Ncn::find_program_address(&jito_restaking_program::id(), &ncn_base.pubkey()).0; | ||
self.initialize_ncn( | ||
&Config::find_program_address(&jito_restaking_program::id()).0, | ||
&ncn_pubkey, | ||
&ncn_admin, | ||
&ncn_base, | ||
) | ||
.await?; | ||
|
||
Ok(NcnRoot { | ||
ncn_pubkey, | ||
ncn_admin, | ||
}) | ||
} | ||
|
||
pub async fn initialize_ncn( | ||
&mut self, | ||
config: &Pubkey, | ||
ncn: &Pubkey, | ||
ncn_admin: &Keypair, | ||
ncn_base: &Keypair, | ||
) -> TestResult<()> { | ||
let blockhash = self.banks_client.get_latest_blockhash().await?; | ||
|
||
self.process_transaction(&Transaction::new_signed_with_payer( | ||
&[initialize_ncn( | ||
&jito_restaking_program::id(), | ||
config, | ||
ncn, | ||
&ncn_admin.pubkey(), | ||
&ncn_base.pubkey(), | ||
)], | ||
Some(&ncn_admin.pubkey()), | ||
&[&ncn_admin, &ncn_base], | ||
blockhash, | ||
)) | ||
.await | ||
} | ||
} |
Oops, something went wrong.