-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify adding tokens and add compatibility with
burn_fee
(#6102)
- Loading branch information
Showing
5 changed files
with
146 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use icrc_ledger_types::icrc::generic_metadata_value::MetadataValue; | ||
|
||
pub struct MetadataHelper { | ||
name: String, | ||
symbol: String, | ||
decimals: u8, | ||
fee: u128, | ||
logo: Option<String>, | ||
is_icrc1_compatible: bool, | ||
} | ||
|
||
impl MetadataHelper { | ||
pub fn try_parse(metadata: Vec<(String, MetadataValue)>) -> Result<MetadataHelper, String> { | ||
let mut name = None; | ||
let mut symbol = None; | ||
let mut decimals = None; | ||
let mut fee = None; | ||
let mut burn_fee = None; | ||
let mut logo = None; | ||
let mut is_icrc1_compatible = true; | ||
|
||
for (key, value) in metadata { | ||
match (key.as_str(), value) { | ||
("icrc1:name", MetadataValue::Text(s)) => name = Some(s), | ||
("icrc1:symbol", MetadataValue::Text(s)) => symbol = Some(s), | ||
("icrc1:decimals", MetadataValue::Nat(n)) => decimals = u8::try_from(n.0).ok(), | ||
("icrc1:fee", MetadataValue::Nat(n)) => fee = u128::try_from(n.0).ok(), | ||
("icrc1:burn_fee", MetadataValue::Nat(n)) => burn_fee = u128::try_from(n.0).ok(), | ||
("icrc1:logo", MetadataValue::Text(s)) => logo = Some(s), | ||
("icrc1:transfer_fee_rate" | "icrc1:burn_fee_rate", _) => is_icrc1_compatible = false, | ||
_ => {} | ||
} | ||
} | ||
|
||
match (name, symbol, decimals, fee) { | ||
(Some(n), Some(s), Some(d), Some(f)) => Ok(MetadataHelper { | ||
name: n, | ||
symbol: s, | ||
decimals: d, | ||
fee: f + burn_fee.unwrap_or_default(), | ||
logo, | ||
is_icrc1_compatible, | ||
}), | ||
(None, ..) => Err("Name not found".to_string()), | ||
(_, None, ..) => Err("Symbol not found".to_string()), | ||
(.., None, _) => Err("Decimals not found".to_string()), | ||
(.., None) => Err("Fee not found".to_string()), | ||
} | ||
} | ||
|
||
pub fn name(&self) -> &str { | ||
&self.name | ||
} | ||
|
||
pub fn symbol(&self) -> &str { | ||
&self.symbol | ||
} | ||
|
||
pub fn decimals(&self) -> u8 { | ||
self.decimals | ||
} | ||
|
||
pub fn fee(&self) -> u128 { | ||
self.fee | ||
} | ||
|
||
pub fn logo(&self) -> Option<&String> { | ||
self.logo.as_ref() | ||
} | ||
|
||
pub fn is_icrc1_compatible(&self) -> bool { | ||
self.is_icrc1_compatible | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters