Skip to content

Commit d980556

Browse files
authored
fix: bonded pallet runtime call rounding is different from module (#848)
## fixes https://github.com/KILTprotocol/ticket/issues/3755
1 parent 5fa0650 commit d980556

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

pallets/pallet-bonded-coins/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ pub mod pallet {
13551355
/// # Errors
13561356
/// - `ArithmeticError`: If there is an error during the conversion to
13571357
/// fixed point.
1358-
fn calculate_normalized_passive_issuance(
1358+
pub fn calculate_normalized_passive_issuance(
13591359
bonded_currencies: &[FungiblesAssetIdOf<T>],
13601360
denomination: u8,
13611361
currency_idx: usize,

pallets/pallet-bonded-coins/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub struct PoolManagingTeam<AccountId> {
180180
}
181181

182182
/// Enum, to specify the rounding direction.
183-
#[derive(PartialEq, Clone, Copy, Eq)]
183+
#[derive(PartialEq, Clone, Copy, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)]
184184
pub enum Round {
185185
/// Round up.
186186
Up,

runtime-api/bonded-coins/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#![cfg_attr(not(feature = "std"), no_std)]
2020

21+
use pallet_bonded_coins::Round;
2122
use parity_scale_codec::{alloc::string::String, Codec, Decode, Encode};
2223
use scale_info::TypeInfo;
2324
use sp_std::vec::Vec;
@@ -71,6 +72,7 @@ sp_api::decl_runtime_apis! {
7172
currency_idx: u8,
7273
low: Balance,
7374
high: Balance,
75+
rounding: Round
7476
) -> Result<Balance, Error>;
7577

7678
/// Query all pool IDs where the given account is the manager.

runtimes/peregrine/src/runtime_apis.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ use crate::{
6363
kilt::{DipProofError, DipProofRequest, DotName, NativeAndForeignAssets, UniqueLinkingDeployment},
6464
parachain::ConsensusHook,
6565
xcm::UniversalLocation,
66-
AssetSwitchPool1, Aura, Block, BondedFungibles, DotNames, Executive, InherentDataExt, ParachainStaking,
67-
ParachainSystem, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment, UniqueLinking,
68-
VERSION,
66+
AssetSwitchPool1, Aura, Block, BondedCurrencies, BondedFungibles, DotNames, Executive, InherentDataExt,
67+
ParachainStaking, ParachainSystem, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment,
68+
UniqueLinking, VERSION,
6969
};
7070

7171
// This is necessary since by default `RUNTIME_API_VERSIONS` generated by
@@ -506,37 +506,34 @@ impl_runtime_apis! {
506506

507507
let currency_supply = BondedFungibles::total_issuance(currency_id.to_owned());
508508

509-
let (low, high, _) = match operation {
509+
let (low, high, rounding) = match operation {
510510
Operation::Mint(amount) => (currency_supply, currency_supply.saturating_add(amount), Round::Up),
511511
Operation::Burn(amount) => (currency_supply.saturating_sub(amount), currency_supply, Round::Down),
512512
};
513513

514-
Self::quote_for_low_and_high_bounds(pool_id, currency_idx, low, high)
514+
Self::quote_for_low_and_high_bounds(pool_id, currency_idx, low, high, rounding)
515515
}
516516

517517
fn quote_for_low_and_high_bounds(
518518
pool_id: AccountId,
519519
currency_idx: u8,
520520
low: Balance,
521521
high: Balance,
522+
rounding: Round,
522523
) -> Result<Balance, BondedCurrencyError> {
523524
let pool = Pools::<Runtime>::get(pool_id).ok_or(BondedCurrencyError::PoolNotFound)?;
524525
let PoolDetailsOf::<Runtime> { curve, bonded_currencies, denomination, collateral, .. } = pool;
525-
let currency_id = bonded_currencies.get(currency_idx.saturated_into::<usize>()).ok_or(BondedCurrencyError::CurrencyNotFound)?;
526-
527526
let collateral_denomination = NativeAndForeignAssets::decimals(collateral);
528527

529-
let normalized_low = balance_to_fixed(low, denomination, Round::Down).map_err(|_| BondedCurrencyError::BalanceConversion)?;
530-
let normalized_high = balance_to_fixed(high, denomination, Round::Up).map_err(|_| BondedCurrencyError::BalanceConversion)?;
528+
let normalized_low = balance_to_fixed(low, denomination, rounding).map_err(|_| BondedCurrencyError::BalanceConversion)?;
529+
let normalized_high = balance_to_fixed(high, denomination, rounding).map_err(|_| BondedCurrencyError::BalanceConversion)?;
530+
531531

532-
let passive_supply = bonded_currencies
533-
.iter()
534-
.filter_map(|id| (id != currency_id).then_some(BondedFungibles::total_issuance(id.to_owned())))
535-
.map(|supply| balance_to_fixed(supply, denomination, Round::Up).map_err(|_| BondedCurrencyError::BalanceConversion))
536-
.collect::<Result<Vec<FixedPoint>, BondedCurrencyError>>()?;
532+
let (_, passive_supply) = BondedCurrencies::calculate_normalized_passive_issuance(&bonded_currencies, denomination, currency_idx.into(), rounding)
533+
.map_err(|_| BondedCurrencyError::BalanceConversion)?;
537534

538535
let normalized_collateral = curve.calculate_costs(normalized_low, normalized_high, passive_supply).map_err(|_| BondedCurrencyError::CalculationError)?;
539-
fixed_to_balance(normalized_collateral, collateral_denomination, Round::Up).map_err(|_| BondedCurrencyError::BalanceConversion)
536+
fixed_to_balance(normalized_collateral, collateral_denomination, rounding).map_err(|_| BondedCurrencyError::BalanceConversion)
540537
}
541538

542539
fn query_pools_by_manager(account: AccountId) -> Vec<PoolDetails<AccountId, Balance, BondedAssetId, AssetId>> {

0 commit comments

Comments
 (0)