Skip to content

Commit 82260be

Browse files
authored
Improved precision converter (#74)
* Improved precision converter * Update trait doc
1 parent 2f56528 commit 82260be

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

pallets/parachain-app/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ pub mod pallet {
281281

282282
let precision = SidechainPrecision::<T>::get(network_id, &asset_id)
283283
.ok_or(Error::<T>::UnknownPrecision)?;
284-
let amount = T::BalancePrecisionConverter::from_sidechain(&asset_id, precision, amount)
285-
.ok_or(Error::<T>::WrongAmount)?;
284+
let (amount, _) =
285+
T::BalancePrecisionConverter::from_sidechain(&asset_id, precision, amount)
286+
.ok_or(Error::<T>::WrongAmount)?;
286287
ensure!(amount > Zero::zero(), Error::<T>::WrongAmount);
287288

288289
T::BridgeAssetLocker::unlock_asset(
@@ -366,7 +367,7 @@ pub mod pallet {
366367

367368
let sidechain_precision = T::AssetRegistry::get_raw_info(asset_id.clone()).precision;
368369

369-
let minimal_xcm_amount = T::BalancePrecisionConverter::to_sidechain(
370+
let (_, minimal_xcm_amount) = T::BalancePrecisionConverter::to_sidechain(
370371
&asset_id,
371372
sidechain_precision,
372373
minimal_xcm_amount,
@@ -403,7 +404,7 @@ pub mod pallet {
403404
ensure_root(origin)?;
404405

405406
let asset_id = T::AssetRegistry::register_asset(network_id.into(), name, symbol)?;
406-
let minimal_xcm_amount =
407+
let (_, minimal_xcm_amount) =
407408
T::BalancePrecisionConverter::to_sidechain(&asset_id, decimals, minimal_xcm_amount)
408409
.ok_or(Error::<T>::WrongAmount)?;
409410

@@ -500,7 +501,7 @@ pub mod pallet {
500501
fail!(Error::<T>::UnknownPrecision);
501502
};
502503

503-
let minimal_xcm_amount = T::BalancePrecisionConverter::to_sidechain(
504+
let (_, minimal_xcm_amount) = T::BalancePrecisionConverter::to_sidechain(
504505
&asset_id,
505506
sidechain_precision,
506507
minimal_xcm_amount,
@@ -588,7 +589,7 @@ pub mod pallet {
588589
let precision = SidechainPrecision::<T>::get(network_id, &asset_id)
589590
.ok_or(Error::<T>::UnknownPrecision)?;
590591

591-
let sidechain_amount =
592+
let (amount, sidechain_amount) =
592593
T::BalancePrecisionConverter::to_sidechain(&asset_id, precision, amount.clone())
593594
.ok_or(Error::<T>::WrongAmount)?;
594595

pallets/parachain-app/src/mock.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,23 @@ impl BalancePrecisionConverter<AssetId, Balance, Balance> for BalancePrecisionCo
298298
asset_id: &AssetId,
299299
_sidechain_precision: u8,
300300
amount: Balance,
301-
) -> Option<Balance> {
301+
) -> Option<(Balance, Balance)> {
302302
if matches!(asset_id, AssetId::Custom(_)) {
303-
Some(amount)
303+
Some((amount, amount))
304304
} else {
305-
Some(amount * 10)
305+
Some((amount, amount * 10))
306306
}
307307
}
308308

309309
fn from_sidechain(
310310
asset_id: &AssetId,
311311
_sidechain_precision: u8,
312312
amount: Balance,
313-
) -> Option<Balance> {
313+
) -> Option<(Balance, Balance)> {
314314
if matches!(asset_id, AssetId::Custom(_)) {
315-
Some(amount)
315+
Some((amount, amount))
316316
} else {
317-
Some(amount / 10)
317+
Some((amount / 10, amount))
318318
}
319319
}
320320
}

pallets/parachain-app/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn it_works_mint() {
7272
recipient.clone(),
7373
amount
7474
));
75-
let sidechain_amount =
75+
let (_, sidechain_amount) =
7676
BalancePrecisionConverterImpl::from_sidechain(&AssetId::Custom(1), 0, amount).unwrap();
7777
assert_eq!(
7878
Currencies::total_balance(asset_id, &recipient),

pallets/types/src/traits.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -358,35 +358,52 @@ impl AuxiliaryDigestHandler for () {
358358
fn add_item(_item: AuxiliaryDigestItem) {}
359359
}
360360

361+
/// Converter trait for Balance precision in different networks.
361362
pub trait BalancePrecisionConverter<AssetId, Balance, SidechainBalance> {
363+
/// Convert thischain balance to sidechain balance.
364+
///
365+
/// **Returns**
366+
/// * `Balance` - rounded thischain balance
367+
/// * `SidechainBalance` - converted sidechain balance
368+
///
369+
/// Or
370+
/// * `None` - if thischain balance can't be converted to sidechain balance
362371
fn to_sidechain(
363372
asset_id: &AssetId,
364373
sidechain_precision: u8,
365374
amount: Balance,
366-
) -> Option<SidechainBalance>;
367-
375+
) -> Option<(Balance, SidechainBalance)>;
376+
377+
/// Convert sidechain balance to thischain balance.
378+
///
379+
/// **Returns**
380+
/// * `Balance` - rounded thischain balance
381+
/// * `SidechainBalance` - converted sidechain balance
382+
///
383+
/// Or
384+
/// * `None` - if sidechain balance can't be converted to thischain balance
368385
fn from_sidechain(
369386
asset_id: &AssetId,
370387
sidechain_precision: u8,
371388
amount: SidechainBalance,
372-
) -> Option<Balance>;
389+
) -> Option<(Balance, SidechainBalance)>;
373390
}
374391

375-
impl<AssetId, Balance> BalancePrecisionConverter<AssetId, Balance, Balance> for () {
392+
impl<AssetId, Balance: Clone> BalancePrecisionConverter<AssetId, Balance, Balance> for () {
376393
fn to_sidechain(
377394
_asset_id: &AssetId,
378395
_sidechain_precision: u8,
379396
amount: Balance,
380-
) -> Option<Balance> {
381-
Some(amount)
397+
) -> Option<(Balance, Balance)> {
398+
Some((amount.clone(), amount))
382399
}
383400

384401
fn from_sidechain(
385402
_asset_id: &AssetId,
386403
_sidechain_precision: u8,
387404
amount: Balance,
388-
) -> Option<Balance> {
389-
Some(amount)
405+
) -> Option<(Balance, Balance)> {
406+
Some((amount.clone(), amount))
390407
}
391408
}
392409

0 commit comments

Comments
 (0)