Skip to content

Commit

Permalink
fix: bonded pallet runtime call rounding is different from module (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
rflechtner authored and Ad96el committed Jan 16, 2025
1 parent a59e8e2 commit f5f3a08
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pallets/pallet-bonded-coins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ pub mod pallet {
/// # Errors
/// - `ArithmeticError`: If there is an error during the conversion to
/// fixed point.
fn calculate_normalized_passive_issuance(
pub fn calculate_normalized_passive_issuance(
bonded_currencies: &[FungiblesAssetIdOf<T>],
denomination: u8,
currency_idx: usize,
Expand Down
2 changes: 1 addition & 1 deletion pallets/pallet-bonded-coins/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub struct PoolManagingTeam<AccountId> {
}

/// Enum, to specify the rounding direction.
#[derive(PartialEq, Clone, Copy, Eq)]
#[derive(PartialEq, Clone, Copy, Eq, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub enum Round {
/// Round up.
Up,
Expand Down
2 changes: 2 additions & 0 deletions runtime-api/bonded-coins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

use pallet_bonded_coins::Round;
use parity_scale_codec::{alloc::string::String, Codec, Decode, Encode};
use scale_info::TypeInfo;
use sp_std::vec::Vec;
Expand Down Expand Up @@ -71,6 +72,7 @@ sp_api::decl_runtime_apis! {
currency_idx: u8,
low: Balance,
high: Balance,
rounding: Round
) -> Result<Balance, Error>;

/// Query all pool IDs where the given account is the manager.
Expand Down
27 changes: 12 additions & 15 deletions runtimes/peregrine/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ use crate::{
kilt::{DipProofError, DipProofRequest, DotName, NativeAndForeignAssets, UniqueLinkingDeployment},
parachain::ConsensusHook,
xcm::UniversalLocation,
AssetSwitchPool1, Aura, Block, BondedFungibles, DotNames, Executive, InherentDataExt, ParachainStaking,
ParachainSystem, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment, UniqueLinking,
VERSION,
AssetSwitchPool1, Aura, Block, BondedCurrencies, BondedFungibles, DotNames, Executive, InherentDataExt,
ParachainStaking, ParachainSystem, Runtime, RuntimeCall, RuntimeGenesisConfig, SessionKeys, TransactionPayment,
UniqueLinking, VERSION,
};

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

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

let (low, high, _) = match operation {
let (low, high, rounding) = match operation {
Operation::Mint(amount) => (currency_supply, currency_supply.saturating_add(amount), Round::Up),
Operation::Burn(amount) => (currency_supply.saturating_sub(amount), currency_supply, Round::Down),
};

Self::quote_for_low_and_high_bounds(pool_id, currency_idx, low, high)
Self::quote_for_low_and_high_bounds(pool_id, currency_idx, low, high, rounding)
}

fn quote_for_low_and_high_bounds(
pool_id: AccountId,
currency_idx: u8,
low: Balance,
high: Balance,
rounding: Round,
) -> Result<Balance, BondedCurrencyError> {
let pool = Pools::<Runtime>::get(pool_id).ok_or(BondedCurrencyError::PoolNotFound)?;
let PoolDetailsOf::<Runtime> { curve, bonded_currencies, denomination, collateral, .. } = pool;
let currency_id = bonded_currencies.get(currency_idx.saturated_into::<usize>()).ok_or(BondedCurrencyError::CurrencyNotFound)?;

let collateral_denomination = NativeAndForeignAssets::decimals(collateral);

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


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

let normalized_collateral = curve.calculate_costs(normalized_low, normalized_high, passive_supply).map_err(|_| BondedCurrencyError::CalculationError)?;
fixed_to_balance(normalized_collateral, collateral_denomination, Round::Up).map_err(|_| BondedCurrencyError::BalanceConversion)
fixed_to_balance(normalized_collateral, collateral_denomination, rounding).map_err(|_| BondedCurrencyError::BalanceConversion)
}

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

0 comments on commit f5f3a08

Please sign in to comment.