Skip to content

Commit

Permalink
🧠 Extract logic for getting the price of an accepted funding asset in…
Browse files Browse the repository at this point in the history
…to a function (#419)
  • Loading branch information
JuaniRios authored Oct 14, 2024
1 parent 0a07290 commit 92b3b1e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 60 deletions.
6 changes: 6 additions & 0 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,10 @@ impl<T: Config> Pallet<T> {

Ok(())
}

pub fn get_decimals_aware_funding_asset_price(funding_asset: &AcceptedFundingAsset) -> Option<PriceOf<T>> {
let funding_asset_id = funding_asset.id();
let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id);
<PriceProviderOf<T>>::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals)
}
}
8 changes: 2 additions & 6 deletions pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,8 @@ impl<
usd_ticket_size: Balance,
funding_asset: AcceptedFundingAsset,
) {
let funding_asset_id = funding_asset.id();
let funding_asset_decimals = self.execute(|| T::FundingCurrency::decimals(funding_asset_id));
let funding_asset_usd_price = self.execute(|| {
<PriceProviderOf<T>>::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals)
.unwrap()
});
let funding_asset_usd_price =
self.execute(|| Pallet::<T>::get_decimals_aware_funding_asset_price(&funding_asset).unwrap());
let funding_asset_bond = funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(usd_ticket_size);
*balance += funding_asset_bond;
}
Expand Down
22 changes: 5 additions & 17 deletions pallets/funding/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::traits::BondingRequirementCalculation;
#[allow(clippy::wildcard_imports)]
use crate::*;
use alloc::collections::BTreeMap;
use frame_support::traits::fungibles::{metadata::Inspect as MetadataInspect, Inspect, InspectEnumerable};
use frame_support::traits::fungibles::{Inspect, InspectEnumerable};
use itertools::Itertools;
use parity_scale_codec::{Decode, Encode};
use polimec_common::{credentials::InvestorType, ProvideAssetPrice, USD_DECIMALS};
Expand Down Expand Up @@ -144,11 +144,8 @@ impl<T: Config> Pallet<T> {
asset_amount: Balance,
) -> Balance {
let project_details = ProjectsDetails::<T>::get(project_id).expect("Project not found");
let funding_asset_id = asset.id();
let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id);
let funding_asset_usd_price =
<PriceProviderOf<T>>::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals)
.expect("Price not found");
Pallet::<T>::get_decimals_aware_funding_asset_price(&asset).expect("Price not found");
let usd_ticket_size = funding_asset_usd_price.saturating_mul_int(asset_amount);

let mut ct_amount = Zero::zero();
Expand Down Expand Up @@ -217,16 +214,12 @@ impl<T: Config> Pallet<T> {
pub fn calculate_otm_fee(funding_asset: AcceptedFundingAsset, funding_asset_amount: Balance) -> Option<Balance> {
let plmc_price = <PriceProviderOf<T>>::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS)
.expect("Price not found");
let funding_asset_id = funding_asset.id();
let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id);
let funding_asset_usd_price =
<PriceProviderOf<T>>::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals)
.expect("Price not found");
let funding_asset_usd_price = Pallet::<T>::get_decimals_aware_funding_asset_price(&funding_asset).unwrap();
let usd_amount = funding_asset_usd_price.saturating_mul_int(funding_asset_amount);
let otm_multiplier: MultiplierOf<T> = ParticipationMode::OTM.multiplier().try_into().ok()?;
let required_usd_bond = otm_multiplier.calculate_usd_bonding_requirement::<T>(usd_amount)?;
let plmc_bond = plmc_price.reciprocal()?.saturating_mul_int(required_usd_bond);
pallet_proxy_bonding::Pallet::<T>::calculate_fee(plmc_bond, funding_asset_id).ok()
pallet_proxy_bonding::Pallet::<T>::calculate_fee(plmc_bond, funding_asset.id()).ok()
}

pub fn get_funding_asset_min_max_amounts(
Expand All @@ -237,12 +230,7 @@ impl<T: Config> Pallet<T> {
) -> Option<(Balance, Balance)> {
let project_details = ProjectsDetails::<T>::get(project_id)?;
let project_metadata = ProjectsMetadata::<T>::get(project_id)?;
let funding_asset_id = funding_asset.id();
let funding_asset_price = <PriceProviderOf<T>>::get_decimals_aware_price(
funding_asset_id,
USD_DECIMALS,
T::FundingCurrency::decimals(funding_asset_id),
)?;
let funding_asset_price = Pallet::<T>::get_decimals_aware_funding_asset_price(&funding_asset)?;

let (min_usd_ticket, maybe_max_usd_ticket, already_spent_usd, total_cts_usd_amount) =
match project_details.status {
Expand Down
21 changes: 3 additions & 18 deletions pallets/funding/src/tests/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,13 @@ mod round_flow {
.unwrap()
});
let usdt_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::USDT.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::USDT.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::USDT).unwrap()
});
let usdc_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::USDC.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::USDC.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::USDC).unwrap()
});
let dot_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::DOT.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::DOT.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::DOT).unwrap()
});

let mut funding_assets_cycle =
Expand Down
22 changes: 3 additions & 19 deletions pallets/funding/src/tests/4_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod round_flow {
#[cfg(test)]
mod success {
use super::*;
use frame_support::traits::fungibles::metadata::Inspect;
use sp_runtime::bounded_vec;
use std::collections::HashSet;

Expand Down Expand Up @@ -189,28 +188,13 @@ mod round_flow {
.unwrap()
});
let usdt_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::USDT.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::USDT.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::USDT).unwrap()
});
let usdc_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::USDC.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::USDC.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::USDC).unwrap()
});
let dot_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
AcceptedFundingAsset::DOT.id(),
USD_DECIMALS,
ForeignAssets::decimals(AcceptedFundingAsset::DOT.id()),
)
.unwrap()
PolimecFunding::get_decimals_aware_funding_asset_price(&AcceptedFundingAsset::DOT).unwrap()
});

let mut funding_assets_cycle =
Expand Down

0 comments on commit 92b3b1e

Please sign in to comment.