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

Commit eff0c79

Browse files
committed
Add amount ui tests
1 parent 4262271 commit eff0c79

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ This repository contains a **proof-of-concept** of a reimplementation of the SPL
2020

2121
| Instruction | Completed | CU (`p-token`) | CU (`spl-token`) |
2222
|----------------------------|-----------|----------------|------------------|
23-
| `InitializeMint` || 97 | 2967 |
24-
| `InitializeAccount` || 175 | 4527 |
25-
| `InitializeMultisig` || 207 | 2973 |
23+
| `InitializeMint` || 98 | 2967 |
24+
| `InitializeAccount` || 161 | 4527 |
25+
| `InitializeMultisig` || 188 | 2973 |
2626
| `Transfer` || 140 | 4645 |
2727
| `Approve` || 133 | 2904 |
2828
| `Revoke` || 106 | 2677 |
@@ -43,8 +43,8 @@ This repository contains a **proof-of-concept** of a reimplementation of the SPL
4343
| `InitializeMint2` || 231 | 2827 |
4444
| `GetAccountDataSize` || | |
4545
| `InitializeImmutableOwner` || | |
46-
| `AmountToUiAmount` || | |
47-
| `UiAmountToAmount` || | |
46+
| `AmountToUiAmount` || 477 | 1913 |
47+
| `UiAmountToAmount` || 773 | 2680 |
4848

4949
> Tests were run using Solana `v2.1.0`.
5050

program/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ assert_matches = "1.5.0"
2828
solana-program-test = "~1.18"
2929
solana-sdk = "~1.18"
3030
spl-token = { version="^4", features=["no-entrypoint"] }
31-
test-case = "3.3.1"
31+
test-case = "3.3.1"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![cfg(feature = "test-sbf")]
2+
3+
mod setup;
4+
5+
use setup::mint;
6+
use solana_program_test::{tokio, ProgramTest};
7+
use solana_sdk::{pubkey::Pubkey, signature::Signer, transaction::Transaction};
8+
9+
#[test_case::test_case(spl_token::ID ; "spl-token")]
10+
#[test_case::test_case(Pubkey::new_from_array(token_program::ID) ; "p-token")]
11+
#[tokio::test]
12+
async fn amount_to_ui_amount(token_program: Pubkey) {
13+
let program_id = Pubkey::new_from_array(token_program::ID);
14+
let mut context = ProgramTest::new("token_program", program_id, None)
15+
.start_with_context()
16+
.await;
17+
18+
// Given a mint account.
19+
20+
let mint_authority = Pubkey::new_unique();
21+
let freeze_authority = Pubkey::new_unique();
22+
23+
let mint = mint::initialize(
24+
&mut context,
25+
mint_authority,
26+
Some(freeze_authority),
27+
&token_program,
28+
)
29+
.await
30+
.unwrap();
31+
32+
let mut amount_to_ui_amount_ix =
33+
spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 1000).unwrap();
34+
// Switches the program id to the token program.
35+
amount_to_ui_amount_ix.program_id = token_program;
36+
37+
let tx = Transaction::new_signed_with_payer(
38+
&[amount_to_ui_amount_ix],
39+
Some(&context.payer.pubkey()),
40+
&[&context.payer],
41+
context.last_blockhash,
42+
);
43+
context.banks_client.process_transaction(tx).await.unwrap();
44+
45+
// Then the transaction should succeed.
46+
47+
let account = context.banks_client.get_account(mint).await.unwrap();
48+
49+
assert!(account.is_some());
50+
}

program/tests/setup/mint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub async fn initialize(
2424
&account.pubkey(),
2525
&mint_authority,
2626
freeze_authority.as_ref(),
27-
0,
27+
4,
2828
)
2929
.unwrap();
3030
// Switches the program id in case we are using a "custom" one.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![cfg(feature = "test-sbf")]
2+
3+
mod setup;
4+
5+
use setup::mint;
6+
use solana_program_test::{tokio, ProgramTest};
7+
use solana_sdk::{pubkey::Pubkey, signature::Signer, transaction::Transaction};
8+
9+
#[test_case::test_case(spl_token::ID ; "spl-token")]
10+
#[test_case::test_case(Pubkey::new_from_array(token_program::ID) ; "p-token")]
11+
#[tokio::test]
12+
async fn ui_amount_to_amount(token_program: Pubkey) {
13+
let program_id = Pubkey::new_from_array(token_program::ID);
14+
let mut context = ProgramTest::new("token_program", program_id, None)
15+
.start_with_context()
16+
.await;
17+
18+
// Given a mint account.
19+
20+
let mint_authority = Pubkey::new_unique();
21+
let freeze_authority = Pubkey::new_unique();
22+
23+
let mint = mint::initialize(
24+
&mut context,
25+
mint_authority,
26+
Some(freeze_authority),
27+
&token_program,
28+
)
29+
.await
30+
.unwrap();
31+
32+
let mut ui_amount_to_amount_ix =
33+
spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, "1000.00").unwrap();
34+
// Switches the program id to the token program.
35+
ui_amount_to_amount_ix.program_id = token_program;
36+
37+
let tx = Transaction::new_signed_with_payer(
38+
&[ui_amount_to_amount_ix],
39+
Some(&context.payer.pubkey()),
40+
&[&context.payer],
41+
context.last_blockhash,
42+
);
43+
context.banks_client.process_transaction(tx).await.unwrap();
44+
45+
// Then the transaction should succeed.
46+
47+
let account = context.banks_client.get_account(mint).await.unwrap();
48+
49+
assert!(account.is_some());
50+
}

0 commit comments

Comments
 (0)