Skip to content
Open
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
11 changes: 5 additions & 6 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ pub fn initialize(
minimum_pool_balance: u64,
) -> Vec<Instruction> {
let pool_address = find_pool_address(program_id, vote_account_address);
let pool_rent = rent.minimum_balance(std::mem::size_of::<SinglePool>());
let pool_rent = rent.minimum_balance(SinglePool::size_of());

let stake_address = find_pool_stake_address(program_id, &pool_address);
let onramp_address = find_pool_onramp_address(program_id, &pool_address);
let stake_space = std::mem::size_of::<stake::state::StakeStateV2>();
let stake_space = stake::state::StakeStateV2::size_of();
let stake_rent = rent.minimum_balance(stake_space);
let stake_rent_plus_minimum = stake_rent.saturating_add(minimum_pool_balance);

Expand Down Expand Up @@ -344,8 +344,7 @@ pub fn deposit_stake(
/// Creates all necessary instructions to withdraw stake into a given stake
/// account. If a new stake account is required, the user should first include
/// `system_instruction::create_account` with account size
/// `std::mem::size_of::<stake::state::StakeStateV2>()` and owner
/// `stake::program::id()`.
/// `stake::state::StakeStateV2::size_of()` and owner `stake::program::id()`.
pub fn withdraw(
program_id: &Pubkey,
pool_address: &Pubkey,
Expand Down Expand Up @@ -438,7 +437,7 @@ pub fn create_and_delegate_user_stake(
stake_amount: u64,
) -> Vec<Instruction> {
let pool_address = find_pool_address(program_id, vote_account_address);
let stake_space = std::mem::size_of::<stake::state::StakeStateV2>();
let stake_space = stake::state::StakeStateV2::size_of();
let lamports = rent
.minimum_balance(stake_space)
.saturating_add(stake_amount);
Expand Down Expand Up @@ -557,7 +556,7 @@ pub fn create_pool_onramp(
rent: &Rent,
) -> Vec<Instruction> {
let onramp_address = find_pool_onramp_address(program_id, pool_address);
let stake_space = std::mem::size_of::<stake::state::StakeStateV2>();
let stake_space = stake::state::StakeStateV2::size_of();
let stake_rent = rent.minimum_balance(stake_space);

vec![
Expand Down
8 changes: 4 additions & 4 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
},
borsh::BorshDeserialize,
solana_account_info::{next_account_info, AccountInfo},
solana_borsh::v1::{get_packed_len, try_from_slice_unchecked},
solana_borsh::v1::try_from_slice_unchecked,
solana_clock::{Clock, Epoch},
solana_cpi::invoke_signed,
solana_msg::msg,
Expand Down Expand Up @@ -626,7 +626,7 @@ impl Processor {
let mint_authority_signers = &[&mint_authority_seeds[..]];

// create the pool. user has already transferred in rent
let pool_space = get_packed_len::<SinglePool>();
let pool_space = SinglePool::size_of();
if !rent.is_exempt(pool_info.lamports(), pool_space) {
return Err(SinglePoolError::WrongRentAmount.into());
}
Expand Down Expand Up @@ -681,7 +681,7 @@ impl Processor {
// create the pool stake account. user has already transferred in rent plus at
// least the minimum
let minimum_pool_balance = minimum_pool_balance()?;
let stake_space = std::mem::size_of::<stake::state::StakeStateV2>();
let stake_space = StakeStateV2::size_of();
let stake_rent_plus_initial = rent
.minimum_balance(stake_space)
.saturating_add(minimum_pool_balance);
Expand Down Expand Up @@ -1411,7 +1411,7 @@ impl Processor {
let stake_authority_signers = &[&stake_authority_seeds[..]];

// create the pool on-ramp account. user has already transferred in rent
let stake_space = std::mem::size_of::<stake::state::StakeStateV2>();
let stake_space = StakeStateV2::size_of();
let stake_rent = rent.minimum_balance(stake_space);

if pool_onramp_info.lamports() < stake_rent {
Expand Down
15 changes: 15 additions & 0 deletions program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub struct SinglePool {
pub vote_account_address: Pubkey,
}
impl SinglePool {
/// Onchain serialized size of `SinglePool`
pub const fn size_of() -> usize {
33
}

/// Create a `SinglePool` struct from its account info
pub fn from_account_info(
account_info: &AccountInfo,
Expand Down Expand Up @@ -55,3 +60,13 @@ impl SinglePool {
Ok(pool)
}
}

#[cfg(test)]
mod tests {
use {super::*, solana_borsh::v1::get_packed_len};

#[test]
fn single_pool_size_of() {
assert_eq!(SinglePool::size_of(), get_packed_len::<SinglePool>());
}
}
Loading