|
1 | 1 | mod setup; |
2 | 2 |
|
3 | 3 | use { |
4 | | - setup::{mint, TOKEN_PROGRAM_ID}, |
| 4 | + mollusk_svm::result::Check, |
| 5 | + setup::{ |
| 6 | + mint, |
| 7 | + mollusk::{create_mint_account, mollusk}, |
| 8 | + TOKEN_PROGRAM_ID, |
| 9 | + }, |
5 | 10 | solana_program_test::{tokio, ProgramTest}, |
6 | 11 | solana_pubkey::Pubkey, |
7 | 12 | solana_signer::Signer, |
@@ -45,3 +50,81 @@ async fn amount_to_ui_amount() { |
45 | 50 |
|
46 | 51 | assert!(account.is_some()); |
47 | 52 | } |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn amount_to_ui_amount_with_maximum_decimals() { |
| 56 | + // Given a mint account with `u8::MAX` as decimals. |
| 57 | + |
| 58 | + let mint = Pubkey::new_unique(); |
| 59 | + let mint_authority = Pubkey::new_unique(); |
| 60 | + let freeze_authority = Pubkey::new_unique(); |
| 61 | + |
| 62 | + let mint_account = create_mint_account( |
| 63 | + mint_authority, |
| 64 | + Some(freeze_authority), |
| 65 | + u8::MAX, |
| 66 | + &TOKEN_PROGRAM_ID, |
| 67 | + ); |
| 68 | + |
| 69 | + // When we convert a 20 amount using the mint, the transaction should |
| 70 | + // succeed and return the correct UI amount. |
| 71 | + |
| 72 | + let instruction = |
| 73 | + spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 20).unwrap(); |
| 74 | + |
| 75 | + // The expected UI amount is "0.000....002" without the trailing zeros. |
| 76 | + let mut ui_amount = [b'0'; u8::MAX as usize + 1]; |
| 77 | + ui_amount[1] = b'.'; |
| 78 | + ui_amount[ui_amount.len() - 1] = b'2'; |
| 79 | + |
| 80 | + mollusk().process_and_validate_instruction( |
| 81 | + &instruction, |
| 82 | + &[(mint, mint_account)], |
| 83 | + &[Check::success(), Check::return_data(&ui_amount)], |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn amount_to_ui_amount_with_u64_max() { |
| 89 | + // Given a mint account with `u8::MAX` as decimals. |
| 90 | + |
| 91 | + let mint = Pubkey::new_unique(); |
| 92 | + let mint_authority = Pubkey::new_unique(); |
| 93 | + let freeze_authority = Pubkey::new_unique(); |
| 94 | + |
| 95 | + let mint_account = create_mint_account( |
| 96 | + mint_authority, |
| 97 | + Some(freeze_authority), |
| 98 | + u8::MAX, |
| 99 | + &TOKEN_PROGRAM_ID, |
| 100 | + ); |
| 101 | + |
| 102 | + // When we convert an u64::MAX amount using the mint, the transaction should |
| 103 | + // succeed and return the correct UI amount. |
| 104 | + |
| 105 | + let instruction = |
| 106 | + spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, u64::MAX).unwrap(); |
| 107 | + |
| 108 | + // The expected UI amount is a `u64::MAX` with 255 decimal places. |
| 109 | + // - 2 digits for `0.` |
| 110 | + // - 255 digits for the maximum decimals. |
| 111 | + let mut ui_amount = [b'0'; u8::MAX as usize + 2]; |
| 112 | + ui_amount[1] = b'.'; |
| 113 | + |
| 114 | + let mut offset = ui_amount.len(); |
| 115 | + let mut value = u64::MAX; |
| 116 | + |
| 117 | + while value > 0 { |
| 118 | + let remainder = value % 10; |
| 119 | + value /= 10; |
| 120 | + offset -= 1; |
| 121 | + |
| 122 | + ui_amount[offset] = b'0' + (remainder as u8); |
| 123 | + } |
| 124 | + |
| 125 | + mollusk().process_and_validate_instruction( |
| 126 | + &instruction, |
| 127 | + &[(mint, mint_account)], |
| 128 | + &[Check::success(), Check::return_data(&ui_amount)], |
| 129 | + ); |
| 130 | +} |
0 commit comments