admin tests

This commit is contained in:
Hardhat Chad
2024-02-16 19:51:54 +00:00
parent ecbe2bb40a
commit d158742958
6 changed files with 148 additions and 75 deletions

View File

@@ -311,3 +311,33 @@ pub fn reset(signer: Pubkey) -> Instruction {
data: OreInstruction::Reset.to_vec(),
}
}
pub fn update_admin(signer: Pubkey, new_admin: Pubkey) -> Instruction {
Instruction {
program_id: crate::id(),
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(TREASURY_ADDRESS, false),
],
data: [
OreInstruction::UpdateAdmin.to_vec(),
UpdateAdminArgs { new_admin }.to_bytes().to_vec(),
]
.concat(),
}
}
pub fn update_difficulty(signer: Pubkey, new_difficulty: Hash) -> Instruction {
Instruction {
program_id: crate::id(),
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(TREASURY_ADDRESS, false),
],
data: [
OreInstruction::UpdateDifficulty.to_vec(),
UpdateDifficultyArgs { new_difficulty }.to_bytes().to_vec(),
]
.concat(),
}
}

View File

@@ -1,17 +1,9 @@
use ore::{
instruction::{InitializeArgs, OreInstruction},
state::{Bus, Treasury},
utils::AccountDeserialize,
BUS, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, MINT, TREASURY,
};
use solana_program::{
hash::Hash,
instruction::{AccountMeta, Instruction},
program_option::COption,
program_pack::Pack,
pubkey::Pubkey,
system_program, sysvar,
BUS_ADDRESSES, BUS_COUNT, INITIAL_DIFFICULTY, INITIAL_REWARD_RATE, MINT_ADDRESS, TREASURY,
};
use solana_program::{hash::Hash, program_option::COption, program_pack::Pack, pubkey::Pubkey};
use solana_program_test::{processor, BanksClient, ProgramTest};
use solana_sdk::{
signature::{Keypair, Signer},
@@ -22,73 +14,22 @@ use spl_token::state::{AccountState, Mint};
#[tokio::test]
async fn test_initialize() {
// Setup
let (mut banks, payer, hash) = setup_program_test_env().await;
let (mut banks, payer, blockhash) = setup_program_test_env().await;
// Pdas
let bus_pdas = vec![
Pubkey::find_program_address(&[BUS, &[0]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[1]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[2]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[3]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[4]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[5]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[6]], &ore::id()),
Pubkey::find_program_address(&[BUS, &[7]], &ore::id()),
];
let mint_pda = Pubkey::find_program_address(&[MINT], &ore::id());
let treasury_pda = Pubkey::find_program_address(&[TREASURY], &ore::id());
let treasury_tokens_address =
spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &mint_pda.0);
// Build ix
let ix = Instruction {
program_id: ore::ID,
accounts: vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(bus_pdas[0].0, false),
AccountMeta::new(bus_pdas[1].0, false),
AccountMeta::new(bus_pdas[2].0, false),
AccountMeta::new(bus_pdas[3].0, false),
AccountMeta::new(bus_pdas[4].0, false),
AccountMeta::new(bus_pdas[5].0, false),
AccountMeta::new(bus_pdas[6].0, false),
AccountMeta::new(bus_pdas[7].0, false),
AccountMeta::new(mint_pda.0, false),
AccountMeta::new(treasury_pda.0, false),
AccountMeta::new(treasury_tokens_address, false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(spl_token::id(), false),
AccountMeta::new_readonly(spl_associated_token_account::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
],
data: [
OreInstruction::Initialize.to_vec(),
InitializeArgs {
bus_0_bump: bus_pdas[0].1,
bus_1_bump: bus_pdas[1].1,
bus_2_bump: bus_pdas[2].1,
bus_3_bump: bus_pdas[3].1,
bus_4_bump: bus_pdas[4].1,
bus_5_bump: bus_pdas[5].1,
bus_6_bump: bus_pdas[6].1,
bus_7_bump: bus_pdas[7].1,
mint_bump: mint_pda.1,
treasury_bump: treasury_pda.1,
}
.to_bytes()
.to_vec(),
]
.concat(),
};
spl_associated_token_account::get_associated_token_address(&treasury_pda.0, &MINT_ADDRESS);
// Submit tx
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash);
let ix = ore::instruction::initialize(payer.pubkey());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
// Test bus state
for i in 0..BUS_COUNT {
let bus_account = banks.get_account(bus_pdas[i].0).await.unwrap().unwrap();
let bus_account = banks.get_account(BUS_ADDRESSES[i]).await.unwrap().unwrap();
assert_eq!(bus_account.owner, ore::id());
let bus = Bus::try_from_bytes(&bus_account.data).unwrap();
assert_eq!(bus.id as u8, i as u8);
@@ -107,7 +48,7 @@ async fn test_initialize() {
assert_eq!(treasury.total_claimed_rewards as u8, 0);
// Test mint state
let mint_account = banks.get_account(mint_pda.0).await.unwrap().unwrap();
let mint_account = banks.get_account(MINT_ADDRESS).await.unwrap().unwrap();
assert_eq!(mint_account.owner, spl_token::id());
let mint = Mint::unpack(&mint_account.data).unwrap();
assert_eq!(mint.mint_authority, COption::Some(treasury_pda.0));
@@ -124,7 +65,7 @@ async fn test_initialize() {
.unwrap();
assert_eq!(treasury_tokens_account.owner, spl_token::id());
let treasury_tokens = spl_token::state::Account::unpack(&treasury_tokens_account.data).unwrap();
assert_eq!(treasury_tokens.mint, mint_pda.0);
assert_eq!(treasury_tokens.mint, MINT_ADDRESS);
assert_eq!(treasury_tokens.owner, treasury_pda.0);
assert_eq!(treasury_tokens.amount, 0);
assert_eq!(treasury_tokens.delegate, COption::None);

View File

@@ -29,12 +29,12 @@ use spl_token::state::{AccountState, Mint};
#[tokio::test]
async fn test_mine() {
// Setup
let (mut banks, payer, hash) = setup_program_test_env().await;
let (mut banks, payer, blockhash) = setup_program_test_env().await;
// Submit register tx
let proof_pda = Pubkey::find_program_address(&[PROOF, payer.pubkey().as_ref()], &ore::id());
let ix = ore::instruction::register(payer.pubkey());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash);
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
@@ -57,7 +57,7 @@ async fn test_mine() {
// Submit mine tx
let ix = ore::instruction::mine(payer.pubkey(), BUS_ADDRESSES[0], next_hash.into(), nonce);
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash);
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
@@ -90,8 +90,12 @@ async fn test_mine() {
&spl_token::id(),
);
let ix = ore::instruction::claim(payer.pubkey(), beneficiary_address, amount);
let tx =
Transaction::new_signed_with_payer(&[token_ix, ix], Some(&payer.pubkey()), &[&payer], hash);
let tx = Transaction::new_signed_with_payer(
&[token_ix, ix],
Some(&payer.pubkey()),
&[&payer],
blockhash,
);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());

View File

@@ -20,7 +20,7 @@ use spl_token::state::{AccountState, Mint};
#[tokio::test]
async fn test_reset() {
// Setup
let (mut banks, payer, hash) = setup_program_test_env().await;
let (mut banks, payer, blockhash) = setup_program_test_env().await;
// Pdas
let bus_pdas = vec![
@@ -40,7 +40,7 @@ async fn test_reset() {
// Submit tx
let ix = ore::instruction::reset(payer.pubkey());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], hash);
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());

View File

@@ -0,0 +1,49 @@
use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
use solana_program::{hash::Hash, pubkey::Pubkey};
use solana_program_test::{processor, BanksClient, ProgramTest};
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
#[tokio::test]
async fn test_update_admin() {
// Setup
let (mut banks, payer, blockhash) = setup_program_test_env().await;
// Submit tx
let ix = ore::instruction::initialize(payer.pubkey());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
// Get treasury account
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
// Submit update admin ix
let new_admin = Pubkey::new_unique();
let ix = ore::instruction::update_admin(payer.pubkey(), new_admin);
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
// Assert treasury state
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
assert_eq!(treasury_.bump, treasury.bump);
assert_eq!(treasury_.admin, new_admin);
assert_eq!(treasury_.difficulty, treasury.difficulty);
assert_eq!(treasury_.epoch_start_at, treasury.epoch_start_at);
assert_eq!(treasury_.reward_rate, treasury.reward_rate);
assert_eq!(
treasury_.total_claimed_rewards,
treasury.total_claimed_rewards
);
}
async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) {
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
program_test.prefer_bpf(true);
program_test.start().await
}

View File

@@ -0,0 +1,49 @@
use ore::{state::Treasury, utils::AccountDeserialize, TREASURY_ADDRESS};
use solana_program::{hash::Hash, keccak::Hash as KeccakHash};
use solana_program_test::{processor, BanksClient, ProgramTest};
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
#[tokio::test]
async fn test_update_difficulty() {
// Setup
let (mut banks, payer, blockhash) = setup_program_test_env().await;
// Submit tx
let ix = ore::instruction::initialize(payer.pubkey());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
// Get treasury account
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
let treasury = Treasury::try_from_bytes(&treasury_account.data).unwrap();
// Submit update admin ix
let new_difficulty = KeccakHash::new_unique();
let ix = ore::instruction::update_difficulty(payer.pubkey(), new_difficulty.into());
let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
let res = banks.process_transaction(tx).await;
assert!(res.is_ok());
// Assert treasury state
let treasury_account = banks.get_account(TREASURY_ADDRESS).await.unwrap().unwrap();
let treasury_ = Treasury::try_from_bytes(&treasury_account.data).unwrap();
assert_eq!(treasury_.bump, treasury.bump);
assert_eq!(treasury_.admin, treasury.admin);
assert_eq!(treasury_.difficulty, new_difficulty.into());
assert_eq!(treasury_.epoch_start_at, treasury.epoch_start_at);
assert_eq!(treasury_.reward_rate, treasury.reward_rate);
assert_eq!(
treasury_.total_claimed_rewards,
treasury.total_claimed_rewards
);
}
async fn setup_program_test_env() -> (BanksClient, Keypair, Hash) {
let mut program_test = ProgramTest::new("ore", ore::ID, processor!(ore::process_instruction));
program_test.prefer_bpf(true);
program_test.start().await
}