@@ -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+ }
0 commit comments