|
1 | 1 | mod setup; |
2 | 2 |
|
3 | 3 | use { |
| 4 | + crate::setup::mollusk::{create_mint_account, mollusk}, |
| 5 | + core::str::from_utf8, |
| 6 | + mollusk_svm::result::Check, |
4 | 7 | setup::{mint, TOKEN_PROGRAM_ID}, |
5 | 8 | solana_program_test::{tokio, ProgramTest}, |
6 | 9 | solana_pubkey::Pubkey, |
@@ -45,3 +48,38 @@ async fn ui_amount_to_amount() { |
45 | 48 |
|
46 | 49 | assert!(account.is_some()); |
47 | 50 | } |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn ui_amount_to_amount_with_maximum_decimals() { |
| 54 | + // Given a mint account with `u8::MAX` as decimals. |
| 55 | + |
| 56 | + let mint = Pubkey::new_unique(); |
| 57 | + let mint_authority = Pubkey::new_unique(); |
| 58 | + let freeze_authority = Pubkey::new_unique(); |
| 59 | + |
| 60 | + let mint_account = create_mint_account( |
| 61 | + mint_authority, |
| 62 | + Some(freeze_authority), |
| 63 | + u8::MAX, |
| 64 | + &TOKEN_PROGRAM_ID, |
| 65 | + ); |
| 66 | + |
| 67 | + // String representing the ui value `0.000....002` |
| 68 | + let mut ui_amount = [b'0'; u8::MAX as usize + 1]; |
| 69 | + ui_amount[1] = b'.'; |
| 70 | + ui_amount[ui_amount.len() - 1] = b'2'; |
| 71 | + |
| 72 | + let input = from_utf8(&ui_amount).unwrap(); |
| 73 | + |
| 74 | + // When we convert the ui amount using the mint, the transaction should |
| 75 | + // succeed and return 20 as the amount. |
| 76 | + |
| 77 | + let instruction = |
| 78 | + spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, input).unwrap(); |
| 79 | + |
| 80 | + mollusk().process_and_validate_instruction( |
| 81 | + &instruction, |
| 82 | + &[(mint, mint_account)], |
| 83 | + &[Check::success(), Check::return_data(&20u64.to_le_bytes())], |
| 84 | + ); |
| 85 | +} |
0 commit comments