diff --git a/toolkit/primitives/domain/src/lib.rs b/toolkit/primitives/domain/src/lib.rs index 2b5c7a197..140e76c53 100644 --- a/toolkit/primitives/domain/src/lib.rs +++ b/toolkit/primitives/domain/src/lib.rs @@ -74,6 +74,12 @@ impl StakeDelegation { #[cfg_attr(feature = "serde", derive(Serialize))] pub struct NativeTokenAmount(pub u128); +impl Display for NativeTokenAmount { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + u128::fmt(&self.0, f) + } +} + /// A main chain block number. In range [0, 2^31-1]. #[derive( Default, diff --git a/toolkit/primitives/native-token-management/src/lib.rs b/toolkit/primitives/native-token-management/src/lib.rs index 94b0165d1..de6188edc 100644 --- a/toolkit/primitives/native-token-management/src/lib.rs +++ b/toolkit/primitives/native-token-management/src/lib.rs @@ -72,14 +72,14 @@ pub struct TokenTransferData { #[derive(Encode, Debug, PartialEq)] #[cfg_attr(feature = "std", derive(Decode, thiserror::Error))] pub enum InherentError { - #[cfg_attr(feature = "std", error("Inherent missing for token transfer of {}", 0.0))] + #[cfg_attr(feature = "std", error("Inherent missing for token transfer of {0} tokens"))] TokenTransferNotHandled(NativeTokenAmount), #[cfg_attr( feature = "std", - error("Incorrect token transfer amount: expected {}, got {}", 0.0, 1.0) + error("Incorrect token transfer amount: expected {0}, got {1} tokens") )] IncorrectTokenNumberTransfered(NativeTokenAmount, NativeTokenAmount), - #[cfg_attr(feature = "std", error("Unexpected transfer of {} tokens", 0.0))] + #[cfg_attr(feature = "std", error("Unexpected transfer of {0} tokens"))] UnexpectedTokenTransferInherent(NativeTokenAmount), } diff --git a/toolkit/primitives/native-token-management/src/tests/mod.rs b/toolkit/primitives/native-token-management/src/tests/mod.rs index dcfb134e3..b0fe7e77b 100644 --- a/toolkit/primitives/native-token-management/src/tests/mod.rs +++ b/toolkit/primitives/native-token-management/src/tests/mod.rs @@ -5,16 +5,33 @@ mod inherent_provider { use super::runtime_api_mock::*; use crate::inherent_provider::mock::*; use crate::inherent_provider::*; - use crate::MainChainScripts; - use crate::INHERENT_IDENTIFIER; + use crate::{InherentError, MainChainScripts, INHERENT_IDENTIFIER}; use sidechain_domain::*; use sidechain_mc_hash::MC_HASH_DIGEST_ID; - use sp_inherents::InherentData; - use sp_inherents::InherentDataProvider; - use sp_runtime::testing::Digest; - use sp_runtime::testing::DigestItem; + use sp_inherents::{InherentData, InherentDataProvider}; + use sp_runtime::testing::{Digest, DigestItem}; use std::sync::Arc; + #[test] + fn error_message_formatting() { + assert_eq!( + InherentError::TokenTransferNotHandled(NativeTokenAmount(3u128)).to_string(), + "Inherent missing for token transfer of 3 tokens" + ); + assert_eq!( + InherentError::IncorrectTokenNumberTransfered( + NativeTokenAmount(13u128), + NativeTokenAmount(7u128) + ) + .to_string(), + "Incorrect token transfer amount: expected 13, got 7 tokens" + ); + assert_eq!( + InherentError::UnexpectedTokenTransferInherent(NativeTokenAmount(13u128)).to_string(), + "Unexpected transfer of 13 tokens" + ); + } + #[tokio::test] async fn correctly_fetches_total_transfer_between_two_hashes() { let parent_number = 1; // not genesis