Skip to content

Commit b93bb2c

Browse files
committed
Add more tests
1 parent 278f40e commit b93bb2c

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

p-token/tests/amount_to_ui_amount.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ fn amount_to_ui_amount_with_maximum_decimals() {
6666
&TOKEN_PROGRAM_ID,
6767
);
6868

69-
// When we convert a 20 amount using the mint the transaction should succeed and
70-
// return the correct UI amount.
69+
// When we convert a 20 amount using the mint, the transaction should
70+
// succeed and return the correct UI amount.
7171

7272
let instruction =
7373
spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 20).unwrap();
@@ -83,3 +83,48 @@ fn amount_to_ui_amount_with_maximum_decimals() {
8383
&[Check::success(), Check::return_data(&ui_amount)],
8484
);
8585
}
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+
}

p-token/tests/ui_amount_to_amount.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use {
55
core::str::from_utf8,
66
mollusk_svm::result::Check,
77
setup::{mint, TOKEN_PROGRAM_ID},
8+
solana_program_error::ProgramError,
89
solana_program_test::{tokio, ProgramTest},
910
solana_pubkey::Pubkey,
1011
solana_signer::Signer,
@@ -83,3 +84,35 @@ fn ui_amount_to_amount_with_maximum_decimals() {
8384
&[Check::success(), Check::return_data(&20u64.to_le_bytes())],
8485
);
8586
}
87+
88+
#[test]
89+
fn fail_ui_amount_to_amount_with_invalid_ui_amount() {
90+
// Given a mint account with `u8::MAX` as decimals.
91+
92+
let mint = Pubkey::new_unique();
93+
let mint_authority = Pubkey::new_unique();
94+
let freeze_authority = Pubkey::new_unique();
95+
96+
let mint_account = create_mint_account(
97+
mint_authority,
98+
Some(freeze_authority),
99+
u8::MAX,
100+
&TOKEN_PROGRAM_ID,
101+
);
102+
103+
// String representing the ui value `2.0`
104+
let ui_amount = [b'2', b'.', b'0'];
105+
let input = from_utf8(&ui_amount).unwrap();
106+
107+
// When we try to convert the ui amount using the mint, the transaction should
108+
// fail with an error since the resulting value does not fit in an `u64`.
109+
110+
let instruction =
111+
spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, input).unwrap();
112+
113+
mollusk().process_and_validate_instruction(
114+
&instruction,
115+
&[(mint, mint_account)],
116+
&[Check::err(ProgramError::InvalidArgument)],
117+
);
118+
}

0 commit comments

Comments
 (0)