Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit c9edc4d

Browse files
authored
stake-pool: Enforce that pool mint uses 9 decimal places (HAL-03) (#5085)
1 parent 7f2f115 commit c9edc4d

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

stake-pool/program/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ pub enum StakePoolError {
144144
/// Instruction exceeds desired slippage limit
145145
#[error("Instruction exceeds desired slippage limit")]
146146
ExceededSlippage,
147+
148+
// 40.
149+
/// Provided mint does not have 9 decimals to match SOL
150+
#[error("IncorrectMintDecimals")]
151+
IncorrectMintDecimals,
147152
}
148153
impl From<StakePoolError> for ProgramError {
149154
fn from(e: StakePoolError) -> Self {

stake-pool/program/src/processor.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use {
3939
spl_token_2022::{
4040
check_spl_token_program_account,
4141
extension::{BaseStateWithExtensions, StateWithExtensions},
42+
native_mint,
4243
state::Mint,
4344
},
4445
std::num::NonZeroU32,
@@ -828,6 +829,10 @@ impl Processor {
828829
return Err(StakePoolError::NonZeroPoolTokenSupply.into());
829830
}
830831

832+
if pool_mint.base.decimals != native_mint::DECIMALS {
833+
return Err(StakePoolError::IncorrectMintDecimals.into());
834+
}
835+
831836
if !pool_mint
832837
.base
833838
.mint_authority
@@ -4006,6 +4011,7 @@ impl PrintProgramError for StakePoolError {
40064011
StakePoolError::UnsupportedMintExtension => msg!("Error: mint has an unsupported extension"),
40074012
StakePoolError::UnsupportedFeeAccountExtension => msg!("Error: fee account has an unsupported extension"),
40084013
StakePoolError::ExceededSlippage => msg!("Error: instruction exceeds desired slippage limit"),
4014+
StakePoolError::IncorrectMintDecimals => msg!("Error: Provided mint does not have 9 decimals to match SOL"),
40094015
}
40104016
}
40114017
}

stake-pool/program/tests/helpers/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use {
3636
},
3737
spl_token_2022::{
3838
extension::{ExtensionType, StateWithExtensionsOwned},
39+
native_mint,
3940
state::{Account, Mint},
4041
},
4142
std::{convert::TryInto, num::NonZeroU32},
@@ -2013,7 +2014,7 @@ impl Default for StakePoolAccounts {
20132014
token_program_id: spl_token::id(),
20142015
pool_mint,
20152016
pool_fee_account,
2016-
pool_decimals: 0,
2017+
pool_decimals: native_mint::DECIMALS,
20172018
manager,
20182019
staker,
20192020
withdraw_authority,

stake-pool/program/tests/initialize.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ async fn fail_with_freeze_authority() {
448448
&wrong_mint.pubkey(),
449449
&stake_pool_accounts.withdraw_authority,
450450
Some(&stake_pool_accounts.withdraw_authority),
451-
0,
451+
stake_pool_accounts.pool_decimals,
452452
)
453453
.unwrap(),
454454
],
@@ -1603,3 +1603,30 @@ async fn success_with_extra_reserve_lamports() {
16031603
.await;
16041604
assert_eq!(init_pool_tokens, init_lamports);
16051605
}
1606+
1607+
#[tokio::test]
1608+
async fn fail_with_incorrect_mint_decimals() {
1609+
let (mut banks_client, payer, recent_blockhash) = program_test().start().await;
1610+
let stake_pool_accounts = StakePoolAccounts {
1611+
pool_decimals: 8,
1612+
..Default::default()
1613+
};
1614+
let error = stake_pool_accounts
1615+
.initialize_stake_pool(
1616+
&mut banks_client,
1617+
&payer,
1618+
&recent_blockhash,
1619+
MINIMUM_RESERVE_LAMPORTS,
1620+
)
1621+
.await
1622+
.unwrap_err()
1623+
.unwrap();
1624+
1625+
assert_eq!(
1626+
error,
1627+
TransactionError::InstructionError(
1628+
2,
1629+
InstructionError::Custom(error::StakePoolError::IncorrectMintDecimals as u32),
1630+
)
1631+
);
1632+
}

0 commit comments

Comments
 (0)