Skip to content

Commit

Permalink
bugfix: implement Display for NativeTokenManagement and fix error for…
Browse files Browse the repository at this point in the history
…matting
  • Loading branch information
LGLO committed Dec 16, 2024
1 parent 8cbb7e0 commit a97d8d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
6 changes: 6 additions & 0 deletions toolkit/primitives/domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions toolkit/primitives/native-token-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
29 changes: 23 additions & 6 deletions toolkit/primitives/native-token-management/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a97d8d8

Please sign in to comment.