Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stake-pool: Enforce that pool mint uses 9 decimal places (HAL-03) #5085

Merged
merged 1 commit into from
Sep 1, 2023
Merged
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
5 changes: 5 additions & 0 deletions stake-pool/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ pub enum StakePoolError {
/// Instruction exceeds desired slippage limit
#[error("Instruction exceeds desired slippage limit")]
ExceededSlippage,

// 40.
/// Provided mint does not have 9 decimals to match SOL
#[error("IncorrectMintDecimals")]
IncorrectMintDecimals,
}
impl From<StakePoolError> for ProgramError {
fn from(e: StakePoolError) -> Self {
Expand Down
6 changes: 6 additions & 0 deletions stake-pool/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use {
spl_token_2022::{
check_spl_token_program_account,
extension::{BaseStateWithExtensions, StateWithExtensions},
native_mint,
state::Mint,
},
std::num::NonZeroU32,
Expand Down Expand Up @@ -828,6 +829,10 @@ impl Processor {
return Err(StakePoolError::NonZeroPoolTokenSupply.into());
}

if pool_mint.base.decimals != native_mint::DECIMALS {
return Err(StakePoolError::IncorrectMintDecimals.into());
}

if !pool_mint
.base
.mint_authority
Expand Down Expand Up @@ -3995,6 +4000,7 @@ impl PrintProgramError for StakePoolError {
StakePoolError::UnsupportedMintExtension => msg!("Error: mint has an unsupported extension"),
StakePoolError::UnsupportedFeeAccountExtension => msg!("Error: fee account has an unsupported extension"),
StakePoolError::ExceededSlippage => msg!("Error: instruction exceeds desired slippage limit"),
StakePoolError::IncorrectMintDecimals => msg!("Error: Provided mint does not have 9 decimals to match SOL"),
}
}
}
3 changes: 2 additions & 1 deletion stake-pool/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use {
},
spl_token_2022::{
extension::{ExtensionType, StateWithExtensionsOwned},
native_mint,
state::{Account, Mint},
},
std::{convert::TryInto, num::NonZeroU32},
Expand Down Expand Up @@ -2013,7 +2014,7 @@ impl Default for StakePoolAccounts {
token_program_id: spl_token::id(),
pool_mint,
pool_fee_account,
pool_decimals: 0,
pool_decimals: native_mint::DECIMALS,
manager,
staker,
withdraw_authority,
Expand Down
29 changes: 28 additions & 1 deletion stake-pool/program/tests/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ async fn fail_with_freeze_authority() {
&wrong_mint.pubkey(),
&stake_pool_accounts.withdraw_authority,
Some(&stake_pool_accounts.withdraw_authority),
0,
stake_pool_accounts.pool_decimals,
)
.unwrap(),
],
Expand Down Expand Up @@ -1603,3 +1603,30 @@ async fn success_with_extra_reserve_lamports() {
.await;
assert_eq!(init_pool_tokens, init_lamports);
}

#[tokio::test]
async fn fail_with_incorrect_mint_decimals() {
let (mut banks_client, payer, recent_blockhash) = program_test().start().await;
let stake_pool_accounts = StakePoolAccounts {
pool_decimals: 8,
..Default::default()
};
let error = stake_pool_accounts
.initialize_stake_pool(
&mut banks_client,
&payer,
&recent_blockhash,
MINIMUM_RESERVE_LAMPORTS,
)
.await
.unwrap_err()
.unwrap();

assert_eq!(
error,
TransactionError::InstructionError(
2,
InstructionError::Custom(error::StakePoolError::IncorrectMintDecimals as u32),
)
);
}
Loading