From 688ef3b68a931e2a9d4da5d8c59e0dad2e5d98d2 Mon Sep 17 00:00:00 2001 From: Jon C Date: Fri, 22 Nov 2024 12:02:48 +0100 Subject: [PATCH] Address feedback --- .../tests/scaled_ui_amount.rs | 36 +++++++++++++++++++ .../extension/scaled_ui_amount/instruction.rs | 6 +++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/token/program-2022-test/tests/scaled_ui_amount.rs b/token/program-2022-test/tests/scaled_ui_amount.rs index 78fd4433735..062e6fafad8 100644 --- a/token/program-2022-test/tests/scaled_ui_amount.rs +++ b/token/program-2022-test/tests/scaled_ui_amount.rs @@ -84,6 +84,27 @@ async fn fail_initialize_with_interest_bearing() { ); } +#[tokio::test] +async fn fail_initialize_with_bad_multiplier() { + let mut context = TestContext::new().await; + let err = context + .init_token_with_mint(vec![ExtensionInitializationParams::ScaledUiAmountConfig { + authority: None, + multiplier: 0.0, + }]) + .await + .unwrap_err(); + assert_eq!( + err, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenError::InvalidScale as u32) + ) + ))) + ); +} + #[tokio::test] async fn update_multiplier() { let authority = Keypair::new(); @@ -115,6 +136,21 @@ async fn update_multiplier() { assert_eq!(f64::from(extension.new_multiplier), new_multiplier); assert_eq!(i64::from(extension.new_multiplier_effective_timestamp), 0); + // fail, bad number + let err = token + .update_multiplier(&authority.pubkey(), f64::INFINITY, 0, &[&authority]) + .await + .unwrap_err(); + assert_eq!( + err, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 0, + InstructionError::Custom(TokenError::InvalidScale as u32) + ) + ))) + ); + // correct in the future let newest_multiplier = 100.0; token diff --git a/token/program-2022/src/extension/scaled_ui_amount/instruction.rs b/token/program-2022/src/extension/scaled_ui_amount/instruction.rs index 9dc092714f9..cb939f6a675 100644 --- a/token/program-2022/src/extension/scaled_ui_amount/instruction.rs +++ b/token/program-2022/src/extension/scaled_ui_amount/instruction.rs @@ -28,7 +28,8 @@ pub enum ScaledUiAmountMintInstruction { /// Fails if the mint has already been initialized, so must be called before /// `InitializeMint`. /// - /// Fails with any number less than 0. + /// Fails if the multiplier is less than or equal to 0 or if it's + /// [subnormal](https://en.wikipedia.org/wiki/Subnormal_number). /// /// The mint must have exactly enough space allocated for the base mint (82 /// bytes), plus 83 bytes of padding, 1 byte reserved for the account type, @@ -44,6 +45,9 @@ pub enum ScaledUiAmountMintInstruction { /// Update the multiplier. Only supported for mints that include the /// `ScaledUiAmount` extension. /// + /// Fails if the multiplier is less than or equal to 0 or if it's + /// [subnormal](https://en.wikipedia.org/wiki/Subnormal_number). + /// /// The authority provides a new multiplier and a unix timestamp on which /// it should take effect. If the timestamp is before the current time, /// immediately sets the multiplier.