Skip to content
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
24 changes: 23 additions & 1 deletion crates/chain_primitives/src/token_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ pub fn format_token_id(chain: Chain, token_id: String) -> Option<String> {
None
}
}
Chain::Stellar => (token_id.len() == 56 && token_id.starts_with('G')).then_some(token_id),
Chain::Stellar => {
if let Some((issuer, symbol)) = token_id.split_once("::") {
(issuer.len() == 56 && issuer.starts_with('G') && !symbol.is_empty()).then_some(token_id)
} else {
None
}
}
Chain::Bitcoin
| Chain::BitcoinCash
| Chain::Litecoin
Expand Down Expand Up @@ -132,4 +138,20 @@ mod tests {
Some("rsoLo2S1kiGeCcn6hCUXVrCpGMWLrRrLZz".to_string())
);
}

#[test]
fn test_format_token_id_stellar() {
let chain = Chain::Stellar;

assert_eq!(
format_token_id(chain, "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN::USDC".to_string()),
Some("GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN::USDC".to_string())
);
assert_eq!(
format_token_id(chain, "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN".to_string()),
None
);
assert_eq!(format_token_id(chain, "invalid".to_string()), None);
assert_eq!(format_token_id(chain, "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN::".to_string()), None);
}
}
26 changes: 1 addition & 25 deletions crates/gem_stellar/src/provider/balances_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn map_all_balances(chain: Chain, account: Account) -> Vec<AssetBalance> {
"credit_alphanum4" | "credit_alphanum12" => {
// Token balances
if let (Some(asset_issuer), Some(asset_code)) = (&balance.asset_issuer, &balance.asset_code) {
let token_id = format!("{}-{}", asset_code, asset_issuer);
let token_id = AssetId::sub_token_id(&[asset_issuer.clone(), asset_code.clone()]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved performance and conciseness, consider using format! directly. This avoids cloning strings and allocating an intermediate array. While this hardcodes the :: separator, it's more efficient for this two-part token ID construction.

Suggested change
let token_id = AssetId::sub_token_id(&[asset_issuer.clone(), asset_code.clone()]);
let token_id = format!("{}::{}", asset_issuer, asset_code);

let asset_id = AssetId::from_token(chain, &token_id);
if let Ok(value) = BigNumberFormatter::value_from_amount_biguint(&balance.balance, STELLAR_DECIMALS) {
let balance_obj = Balance::coin_balance(value);
Expand All @@ -78,30 +78,6 @@ pub fn map_all_balances(chain: Chain, account: Account) -> Vec<AssetBalance> {
balances
}

pub fn map_token_balances_by_ids(chain: Chain, account: &Account, token_ids: &[String]) -> Vec<AssetBalance> {
let mut result = Vec::new();
for token_id in token_ids {
if let Some(balance) = account.balances.iter().find(|b| {
if let (Some(asset_issuer), Some(asset_code)) = (&b.asset_issuer, &b.asset_code) {
let balance_token_id = format!("{}-{}", asset_code, asset_issuer);
balance_token_id == *token_id && b.asset_type != "native"
} else {
false
}
}) {
if let Ok(amount) = BigNumberFormatter::value_from_amount_biguint(&balance.balance, STELLAR_DECIMALS) {
let asset_id = AssetId::from_token(chain, token_id);
let balance_obj = Balance::coin_balance(amount);
result.push(AssetBalance::new_with_active(asset_id, balance_obj, true));
}
} else {
let asset_id = AssetId::from_token(chain, token_id);
let balance_obj = Balance::coin_balance(BigUint::from(0u32));
result.push(AssetBalance::new_with_active(asset_id, balance_obj, false));
}
}
result
}

#[cfg(test)]
mod tests {
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/asset_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub const USDT_BERA_ASSET_ID: &str = "berachain_0x779Ded0c9e1022225f8e0630b35a9b
pub const USDT_GNOSIS_ASSET_ID: &str = "gnosis_0x4ECaBa5870353805a9F068101A40E0f32ed605C6";

// Stellar
pub const USDC_STELLAR_ASSET_ID: &str = "stellar_GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN";
pub const USDC_STELLAR_ASSET_ID: &str = "stellar_GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN::USDC";

// XLayer
pub const USDC_XLAYER_ASSET_ID: &str = "xlayer_0x74b7f16337b8972027F6196A17a631aC6dE26d22";
Expand Down
Loading