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

Modify DecimalsLookup trait, add PINK, HDX, vDOT to decimals list #523

Merged
Merged
Changes from 1 commit
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
60 changes: 35 additions & 25 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ pub trait CurrencyInfo {
pub fn remove_trailing_non_alphanum_bytes(input: &[u8]) -> &[u8] {
for (idx, elem) in input.iter().enumerate().rev() {
if elem.is_ascii_alphanumeric() {
return &input[..=idx]
return &input[..=idx];
}
}
b""
Expand All @@ -440,10 +440,12 @@ impl CurrencyInfo for Asset {
fn name(&self) -> &str {
match self {
Asset::StellarNative => "Stellar",
Asset::AlphaNum4 { code, issuer: _ } =>
from_utf8(&remove_trailing_non_alphanum_bytes(code)).unwrap_or("unspecified"),
Asset::AlphaNum12 { code, issuer: _ } =>
from_utf8(&remove_trailing_non_alphanum_bytes(code)).unwrap_or("unspecified"),
Asset::AlphaNum4 { code, issuer: _ } => {
from_utf8(&remove_trailing_non_alphanum_bytes(code)).unwrap_or("unspecified")
},
Asset::AlphaNum12 { code, issuer: _ } => {
from_utf8(&remove_trailing_non_alphanum_bytes(code)).unwrap_or("unspecified")
},
}
}

Expand Down Expand Up @@ -498,9 +500,9 @@ pub enum CurrencyId {
pub trait DecimalsLookup {
type CurrencyId;

fn decimals(currency_id: CurrencyId) -> u32;
fn decimals(currency_id: Self::CurrencyId) -> u32;

fn one(currency_id: CurrencyId) -> Balance {
fn one(currency_id: Self::CurrencyId) -> Balance {
10u128.pow(Self::decimals(currency_id))
}
}
Expand Down Expand Up @@ -531,11 +533,14 @@ impl DecimalsLookup for PendulumDecimalsLookup {
5 => 12,
// GLMR
6 => 18,
// PINK
7 => 10,
_ => 12,
},
// We assume that all other assets have 12 decimals
CurrencyId::Native | CurrencyId::ZenlinkLPToken(_, _, _, _) | CurrencyId::Token(_) =>
12,
CurrencyId::Native | CurrencyId::ZenlinkLPToken(_, _, _, _) | CurrencyId::Token(_) => {
12
},
}) as u32
}
}
Expand All @@ -555,8 +560,9 @@ impl DecimalsLookup for AmplitudeDecimalsLookup {
_ => 12,
},
// We assume that all other assets have 12 decimals
CurrencyId::Native | CurrencyId::ZenlinkLPToken(_, _, _, _) | CurrencyId::Token(_) =>
12,
CurrencyId::Native | CurrencyId::ZenlinkLPToken(_, _, _, _) | CurrencyId::Token(_) => {
12
},
}) as u32
}
}
Expand Down Expand Up @@ -606,11 +612,11 @@ impl TryFrom<(&str, AssetIssuer)> for CurrencyId {
if slice.len() <= 4 {
let mut code: Bytes4 = [0; 4];
code[..slice.len()].copy_from_slice(slice.as_bytes());
return Ok(CurrencyId::AlphaNum4(code, issuer))
return Ok(CurrencyId::AlphaNum4(code, issuer));
} else if slice.len() <= 12 {
let mut code: Bytes12 = [0; 12];
code[..slice.len()].copy_from_slice(slice.as_bytes());
return Ok(CurrencyId::AlphaNum12(code, issuer))
return Ok(CurrencyId::AlphaNum12(code, issuer));
} else {
Err("More than 12 bytes not supported")
}
Expand Down Expand Up @@ -642,18 +648,21 @@ impl TryInto<stellar::Asset> for CurrencyId {
Self::XCM(_currency_id) => Err("XCM Foreign Asset not defined in the Stellar world."),
Self::Native => Err("PEN token not defined in the Stellar world."),
Self::StellarNative => Ok(stellar::Asset::native()),
Self::Stellar(Asset::AlphaNum4 { code, issuer }) =>
Self::Stellar(Asset::AlphaNum4 { code, issuer }) => {
Ok(stellar::Asset::AssetTypeCreditAlphanum4(AlphaNum4 {
asset_code: code,
issuer: PublicKey::PublicKeyTypeEd25519(issuer),
})),
Self::Stellar(Asset::AlphaNum12 { code, issuer }) =>
}))
},
Self::Stellar(Asset::AlphaNum12 { code, issuer }) => {
Ok(stellar::Asset::AssetTypeCreditAlphanum12(AlphaNum12 {
asset_code: code,
issuer: PublicKey::PublicKeyTypeEd25519(issuer),
})),
Self::ZenlinkLPToken(_, _, _, _) =>
Err("Zenlink LP Token not defined in the Stellar world."),
}))
},
Self::ZenlinkLPToken(_, _, _, _) => {
Err("Zenlink LP Token not defined in the Stellar world.")
},
Self::Token(_) => Err("Token not defined in the Stellar world."),
}
}
Expand Down Expand Up @@ -871,7 +880,7 @@ impl TransactionEnvelopeExt for TransactionEnvelope {
};

if tx_operations.len() == 0 {
return BalanceConversion::unlookup(transferred_amount)
return BalanceConversion::unlookup(transferred_amount);
}

transferred_amount = tx_operations.iter().fold(0i64, |acc, x| {
Expand All @@ -889,9 +898,9 @@ impl TransactionEnvelopeExt for TransactionEnvelope {
if payment.claimants.len() == 1 {
let Claimant::ClaimantTypeV0(claimant) = &payment.claimants.get_vec()[0];

if claimant.destination.eq(&recipient_account_pk) &&
payment.asset == asset && claimant.predicate ==
ClaimPredicate::ClaimPredicateUnconditional
if claimant.destination.eq(&recipient_account_pk)
&& payment.asset == asset && claimant.predicate
== ClaimPredicate::ClaimPredicateUnconditional
{
acc.saturating_add(payment.amount)
} else {
Expand Down Expand Up @@ -919,8 +928,9 @@ impl TransactionEnvelopeExt for TransactionEnvelope {

fn get_transaction(&self) -> Option<Transaction> {
match self {
TransactionEnvelope::EnvelopeTypeTxV0(transaction) =>
Some(transaction.tx.clone().into()),
TransactionEnvelope::EnvelopeTypeTxV0(transaction) => {
Some(transaction.tx.clone().into())
},
TransactionEnvelope::EnvelopeTypeTx(transaction) => Some(transaction.tx.clone()),
_ => None,
}
Expand Down
Loading