Skip to content

Commit 3e5f5af

Browse files
Merge pull request #2243 from multiversx/token-id-display
Display for TokenId and EgldOrEsdtTokenIdentifier
2 parents 4de338a + 4f08245 commit 3e5f5af

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

framework/base/src/types/managed/wrapped/token/egld_or_esdt_token_identifier.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,13 @@ where
378378
)
379379
}
380380
}
381+
382+
impl<M: ManagedTypeApi> core::fmt::Display for EgldOrEsdtTokenIdentifier<M> {
383+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
384+
self.map_ref_or_else(
385+
f,
386+
|f| core::fmt::Display::fmt("EGLD", f),
387+
|f, token_identifier| core::fmt::Display::fmt(token_identifier, f),
388+
)
389+
}
390+
}

framework/base/src/types/managed/wrapped/token/token_id.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::string::String;
1+
use alloc::string::{String, ToString};
22
use multiversx_chain_core::EGLD_000000_TOKEN_IDENTIFIER;
33

44
use crate::{
@@ -17,7 +17,7 @@ use crate::{
1717

1818
/// Specialized type for handling token identifiers (e.g. ABCDEF-123456).
1919
#[repr(transparent)]
20-
#[derive(Debug, Clone)]
20+
#[derive(Clone)]
2121
pub struct TokenId<M: ManagedTypeApi> {
2222
pub(crate) buffer: ManagedBuffer<M>,
2323
}
@@ -283,3 +283,17 @@ impl<M: ManagedTypeApi> SCLowerHex for TokenId<M> {
283283
f.append_managed_buffer_lower_hex(&wrap_cast);
284284
}
285285
}
286+
287+
impl<M: ManagedTypeApi> core::fmt::Display for TokenId<M> {
288+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
289+
let bytes = self.buffer.to_boxed_bytes();
290+
let s = alloc::string::String::from_utf8_lossy(bytes.as_slice());
291+
s.fmt(f)
292+
}
293+
}
294+
295+
impl<M: ManagedTypeApi> core::fmt::Debug for TokenId<M> {
296+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
297+
f.debug_tuple("TokenId").field(&self.to_string()).finish()
298+
}
299+
}

framework/scenario/tests/token_id_test.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use multiversx_sc::{
22
chain_core::EGLD_000000_TOKEN_IDENTIFIER,
33
types::{
4-
BoxedBytes, EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPayment, EsdtTokenPayment,
5-
ManagedBuffer, TokenId,
4+
BoxedBytes, EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPayment, EsdtTokenIdentifier,
5+
EsdtTokenPayment, ManagedBuffer, TokenId,
66
},
77
};
88
use multiversx_sc_scenario::{
@@ -222,3 +222,27 @@ fn test_managed_token_id_macro() {
222222
TokenId::<StaticApi>::from("ALC-6258d2")
223223
);
224224
}
225+
226+
#[test]
227+
fn test_token_id_to_string() {
228+
assert_eq!(
229+
TokenId::<StaticApi>::from("ALC-6258d2").to_string(),
230+
"ALC-6258d2"
231+
);
232+
assert_eq!(
233+
TokenId::<StaticApi>::from("EGLD-00000").to_string(),
234+
"EGLD-00000"
235+
);
236+
assert_eq!(
237+
EgldOrEsdtTokenIdentifier::<StaticApi>::egld().to_string(),
238+
"EGLD"
239+
);
240+
assert_eq!(
241+
EgldOrEsdtTokenIdentifier::<StaticApi>::esdt(TokenId::from("EGLDORESDT-00001")).to_string(),
242+
"EGLDORESDT-00001"
243+
);
244+
assert_eq!(
245+
EsdtTokenIdentifier::<StaticApi>::from_esdt_bytes("ESDT-00001").to_string(),
246+
"ESDT-00001"
247+
);
248+
}

0 commit comments

Comments
 (0)