Browse Source

Add amount ui tests

febo 10 tháng trước cách đây
mục cha
commit
3db0680aa5

+ 1 - 1
p-token/Cargo.toml

@@ -28,4 +28,4 @@ assert_matches = "1.5.0"
 solana-program-test = "~1.18"
 solana-sdk = "~1.18"
 spl-token = { version="^4", features=["no-entrypoint"] }
-test-case = "3.3.1"
+test-case = "3.3.1"

+ 50 - 0
p-token/tests/amount_to_ui_amount.rs

@@ -0,0 +1,50 @@
+#![cfg(feature = "test-sbf")]
+
+mod setup;
+
+use setup::mint;
+use solana_program_test::{tokio, ProgramTest};
+use solana_sdk::{pubkey::Pubkey, signature::Signer, transaction::Transaction};
+
+#[test_case::test_case(spl_token::ID ; "spl-token")]
+#[test_case::test_case(Pubkey::new_from_array(token_program::ID) ; "p-token")]
+#[tokio::test]
+async fn amount_to_ui_amount(token_program: Pubkey) {
+    let program_id = Pubkey::new_from_array(token_program::ID);
+    let mut context = ProgramTest::new("token_program", program_id, None)
+        .start_with_context()
+        .await;
+
+    // Given a mint account.
+
+    let mint_authority = Pubkey::new_unique();
+    let freeze_authority = Pubkey::new_unique();
+
+    let mint = mint::initialize(
+        &mut context,
+        mint_authority,
+        Some(freeze_authority),
+        &token_program,
+    )
+    .await
+    .unwrap();
+
+    let mut amount_to_ui_amount_ix =
+        spl_token::instruction::amount_to_ui_amount(&spl_token::ID, &mint, 1000).unwrap();
+    // Switches the program id to the token program.
+    amount_to_ui_amount_ix.program_id = token_program;
+
+    let tx = Transaction::new_signed_with_payer(
+        &[amount_to_ui_amount_ix],
+        Some(&context.payer.pubkey()),
+        &[&context.payer],
+        context.last_blockhash,
+    );
+    context.banks_client.process_transaction(tx).await.unwrap();
+
+    // Then the transaction should succeed.
+
+    let account = context.banks_client.get_account(mint).await.unwrap();
+
+    assert!(account.is_some());
+}

+ 1 - 1
p-token/tests/setup/mint.rs

@@ -24,7 +24,7 @@ pub async fn initialize(
         &account.pubkey(),
         &mint_authority,
         freeze_authority.as_ref(),
-        0,
+        4,
     )
     .unwrap();
     // Switches the program id in case we are using a "custom" one.

+ 50 - 0
p-token/tests/ui_amount_to_amount.rs

@@ -0,0 +1,50 @@
+#![cfg(feature = "test-sbf")]
+
+mod setup;
+
+use setup::mint;
+use solana_program_test::{tokio, ProgramTest};
+use solana_sdk::{pubkey::Pubkey, signature::Signer, transaction::Transaction};
+
+#[test_case::test_case(spl_token::ID ; "spl-token")]
+#[test_case::test_case(Pubkey::new_from_array(token_program::ID) ; "p-token")]
+#[tokio::test]
+async fn ui_amount_to_amount(token_program: Pubkey) {
+    let program_id = Pubkey::new_from_array(token_program::ID);
+    let mut context = ProgramTest::new("token_program", program_id, None)
+        .start_with_context()
+        .await;
+
+    // Given a mint account.
+
+    let mint_authority = Pubkey::new_unique();
+    let freeze_authority = Pubkey::new_unique();
+
+    let mint = mint::initialize(
+        &mut context,
+        mint_authority,
+        Some(freeze_authority),
+        &token_program,
+    )
+    .await
+    .unwrap();
+
+    let mut ui_amount_to_amount_ix =
+        spl_token::instruction::ui_amount_to_amount(&spl_token::ID, &mint, "1000.00").unwrap();
+    // Switches the program id to the token program.
+    ui_amount_to_amount_ix.program_id = token_program;
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ui_amount_to_amount_ix],
+        Some(&context.payer.pubkey()),
+        &[&context.payer],
+        context.last_blockhash,
+    );
+    context.banks_client.process_transaction(tx).await.unwrap();
+
+    // Then the transaction should succeed.
+
+    let account = context.banks_client.get_account(mint).await.unwrap();
+
+    assert!(account.is_some());
+}