Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: implement Display for NativeTokenManagement and fix error formatting #334

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading