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

1️⃣ One Token Model - New Pallet Proxy Bonding #399

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions integration-tests/src/tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use pallet_funding::{
use sp_arithmetic::{FixedPointNumber, Percent};

use macros::generate_accounts;
use pallet_funding::traits::ProvideAssetPrice;
use polimec_common::{USD_DECIMALS, USD_UNIT};
use polimec_common::{ProvideAssetPrice, USD_DECIMALS, USD_UNIT};
use polimec_runtime::{AccountId, PLMC};
use sp_runtime::{traits::ConstU32, Perquintill};

Expand Down
5 changes: 2 additions & 3 deletions integration-tests/src/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ use frame_support::{
};
use itertools::Itertools;
use macros::generate_accounts;
use pallet_funding::{traits::ProvideAssetPrice, *};
use polimec_common::{USD_DECIMALS, USD_UNIT};
use pallet_funding::*;
use polimec_common::{ProvideAssetPrice, USD_DECIMALS, USD_UNIT};
use sp_arithmetic::{traits::Zero, Percent, Perquintill};
use sp_runtime::{FixedPointNumber, FixedU128};
use xcm_emulator::log;

type UserToCTBalance = Vec<(AccountId, FixedU128, ProjectId)>;

generate_accounts!(
Expand Down
7 changes: 2 additions & 5 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
//! Benchmarking setup for Funding pallet
use super::*;
use crate::{
instantiator::*,
traits::{ProvideAssetPrice, SetPrices},
};
use crate::{instantiator::*, traits::SetPrices};
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
Expand All @@ -35,7 +32,7 @@ use frame_support::{
};
use itertools::Itertools;
use parity_scale_codec::{Decode, Encode};
use polimec_common::{credentials::InvestorType, ReleaseSchedule, USD_DECIMALS, USD_UNIT};
use polimec_common::{credentials::InvestorType, ProvideAssetPrice, ReleaseSchedule, USD_DECIMALS, USD_UNIT};
use polimec_common_test_utils::{generate_did_from_account, get_mock_jwt_with_cid};
use sp_arithmetic::Percent;
use sp_core::H256;
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/functions/2_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(clippy::wildcard_imports)]
use super::*;

use polimec_common::ProvideAssetPrice;
impl<T: Config> Pallet<T> {
/// Called by user extrinsic
/// Starts the evaluation round of a project. It needs to be called by the project issuer.
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[allow(clippy::wildcard_imports)]
use super::*;
use polimec_common::ProvideAssetPrice;

// Helper functions
// ATTENTION: if this is called directly, it will not be transactional
Expand Down
3 changes: 1 addition & 2 deletions pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use super::*;
use core::cmp::Ordering;
use itertools::GroupBy;
use polimec_common::USD_DECIMALS;

use polimec_common::{ProvideAssetPrice, USD_DECIMALS};
impl<
T: Config,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
Expand Down
3 changes: 1 addition & 2 deletions pallets/funding/src/instantiator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use crate::{
defaults::{bounded_name, bounded_symbol, default_evaluations, default_project_metadata, ipfs_hash},
CT_DECIMALS, CT_UNIT,
},
traits::ProvideAssetPrice,
*,
};
use core::cell::RefCell;
use itertools::Itertools;
use polimec_common::{USD_DECIMALS, USD_UNIT};
use polimec_common::{ProvideAssetPrice, USD_DECIMALS, USD_UNIT};
use sp_arithmetic::Percent;

#[test]
Expand Down
5 changes: 3 additions & 2 deletions pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub use pallet::*;
use pallet_xcm::ensure_response;
use polimec_common::{
credentials::{Cid, Did, EnsureOriginWithCredentials, InvestorType, UntrustedToken},
migration_types::{Migration, MigrationStatusd},
migration_types::{Migration, MigrationStatus},
};
use polkadot_parachain_primitives::primitives::Id as ParaId;
use sp_arithmetic::traits::{One, Saturating};
Expand Down Expand Up @@ -151,7 +151,7 @@ pub const PLMC_DECIMALS: u8 = 10;
pub mod pallet {
#[allow(clippy::wildcard_imports)]
use super::*;
use crate::traits::{BondingRequirementCalculation, ProvideAssetPrice, VestingDurationCalculation};
use crate::traits::{BondingRequirementCalculation, VestingDurationCalculation};
use core::ops::RangeInclusive;
use frame_support::{
pallet_prelude::*,
Expand All @@ -160,6 +160,7 @@ pub mod pallet {
};
use frame_system::pallet_prelude::*;
use on_slash_vesting::OnSlash;
use polimec_common::ProvideAssetPrice;
use sp_arithmetic::Percent;
use sp_runtime::{
traits::{Convert, ConvertBack, Get},
Expand Down
8 changes: 3 additions & 5 deletions pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

use super::*;
use crate as pallet_funding;
use crate::{
runtime_api::{ExtrinsicHelpers, Leaderboards, ProjectInformation, ProjectParticipationIds, UserInformation},
traits::ProvideAssetPrice,
use crate::runtime_api::{
ExtrinsicHelpers, Leaderboards, ProjectInformation, ProjectParticipationIds, UserInformation,
};
use core::ops::RangeInclusive;
use frame_support::{
Expand All @@ -34,7 +33,7 @@ use frame_support::{
};
use frame_system as system;
use frame_system::{EnsureRoot, RawOrigin as SystemRawOrigin};
use polimec_common::{credentials::EnsureInvestor, DummyXcmSender, USD_UNIT};
use polimec_common::{credentials::EnsureInvestor, DummyXcmSender, ProvideAssetPrice, USD_UNIT};
use polkadot_parachain_primitives::primitives::Sibling;
use sp_arithmetic::Percent;
use sp_core::H256;
Expand All @@ -53,7 +52,6 @@ pub const PLMC: Balance = 10u128.pow(PLMC_DECIMALS as u32);
pub const MILLI_PLMC: Balance = PLMC / 10u128.pow(3);
pub const MICRO_PLMC: Balance = PLMC / 10u128.pow(6);
pub const EXISTENTIAL_DEPOSIT: Balance = 10 * MILLI_PLMC;

pub type Block = frame_system::mocking::MockBlock<TestRuntime>;
pub type AccountId = u32;
pub type BlockNumber = u64;
Expand Down
5 changes: 2 additions & 3 deletions pallets/funding/src/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#[allow(clippy::wildcard_imports)]
use crate::{traits::*, *};
use crate::*;
use alloc::collections::BTreeMap;
use frame_support::traits::fungibles::{metadata::Inspect as MetadataInspect, Inspect, InspectEnumerable};
use itertools::Itertools;
use parity_scale_codec::{Decode, Encode};
use polimec_common::USD_DECIMALS;
use polimec_common::{ProvideAssetPrice, USD_DECIMALS};
use scale_info::TypeInfo;
use sp_runtime::traits::Zero;

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
pub struct ProjectParticipationIds<T: Config> {
account: AccountIdOf<T>,
Expand Down
7 changes: 2 additions & 5 deletions pallets/funding/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::*;
use crate::{
instantiator::*,
mock::*,
traits::{ProvideAssetPrice, VestingDurationCalculation},
CurrencyMetadata, Error, ProjectMetadata, TicketSize,
instantiator::*, mock::*, traits::VestingDurationCalculation, CurrencyMetadata, Error, ProjectMetadata, TicketSize,
};
use defaults::*;
use frame_support::{
Expand All @@ -15,7 +12,7 @@ use frame_support::{
};
use itertools::Itertools;
use parachains_common::DAYS;
use polimec_common::{ReleaseSchedule, USD_DECIMALS, USD_UNIT};
use polimec_common::{ProvideAssetPrice, ReleaseSchedule, USD_DECIMALS, USD_UNIT};
use polimec_common_test_utils::{generate_did_from_account, get_mock_jwt_with_cid};
use sp_arithmetic::{traits::Zero, Percent, Perquintill};
use sp_runtime::TokenError;
Expand Down
48 changes: 0 additions & 48 deletions pallets/funding/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
use crate::{Balance, Config, ProjectId};
use frame_support::weights::Weight;
use frame_system::pallet_prelude::BlockNumberFor;
use sp_arithmetic::{
traits::{CheckedDiv, CheckedMul},
FixedPointNumber,
};
use sp_runtime::DispatchError;

pub trait BondingRequirementCalculation {
Expand All @@ -31,50 +27,6 @@ pub trait VestingDurationCalculation {
fn calculate_vesting_duration<T: Config>(&self) -> BlockNumberFor<T>;
}

pub trait ProvideAssetPrice {
type AssetId;
type Price: FixedPointNumber;
fn get_price(asset_id: Self::AssetId) -> Option<Self::Price>;

/// Prices define the relationship between USD/Asset. When to and from that asset, we need to be aware that they might
/// have different decimals. This function calculates the relationship having in mind the decimals. For example:
/// if the price is 2.5, our underlying USD unit has 6 decimals, and the asset has 8 decimals, the price will be
/// calculated like so: `(2.5USD * 10^6) / (1 * 10^8) = 0.025`. And so if we want to convert 20 of the asset to USD,
/// we would do `0.025(USD/Asset)FixedPointNumber * 20_000_000_00(Asset)u128 = 50_000_000` which is 50 USD with 6 decimals
fn calculate_decimals_aware_price(
original_price: Self::Price,
usd_decimals: u8,
asset_decimals: u8,
) -> Option<Self::Price> {
let usd_unit = 10u128.checked_pow(usd_decimals.into())?;
let usd_price_with_decimals = original_price.checked_mul_int(usd_unit)?;
let asset_unit = 10u128.checked_pow(asset_decimals.into())?;

Self::Price::checked_from_rational(usd_price_with_decimals, asset_unit)
}

fn convert_back_to_normal_price(
decimals_aware_price: Self::Price,
usd_decimals: u8,
asset_decimals: u8,
) -> Option<Self::Price> {
let abs_diff: u32 = asset_decimals.abs_diff(usd_decimals).into();
let abs_diff_unit = 10u128.checked_pow(abs_diff)?;
// We are pretty sure this is going to be representable because the number size is not the size of the asset decimals, but the difference between the asset and usd decimals
let abs_diff_fixed = Self::Price::checked_from_rational(abs_diff_unit, 1)?;
if usd_decimals > asset_decimals {
decimals_aware_price.checked_div(&abs_diff_fixed)
} else {
decimals_aware_price.checked_mul(&abs_diff_fixed)
}
}

fn get_decimals_aware_price(asset_id: Self::AssetId, usd_decimals: u8, asset_decimals: u8) -> Option<Self::Price> {
let original_price = Self::get_price(asset_id)?;
Self::calculate_decimals_aware_price(original_price, usd_decimals, asset_decimals)
}
}

pub trait DoRemainingOperation<T: Config> {
fn has_remaining_operations(&self) -> bool;

Expand Down
63 changes: 63 additions & 0 deletions pallets/proxy-bonding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[package]
name = "proxy-bonding"
authors.workspace = true
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
readme.workspace = true
repository.workspace = true
version.workspace = true

[lints]
workspace = true

[dependencies]
frame-system.workspace = true
frame-support.workspace = true
frame-benchmarking = { workspace = true, optional = true }
sp-runtime.workspace = true
polimec-common.workspace = true
parity-scale-codec.workspace = true
scale-info.workspace = true
serde = { version = "1.0.204", features = ["derive"] }

[dev-dependencies]
sp-io.workspace = true
pallet-linear-release.workspace = true
pallet-balances.workspace = true
pallet-assets.workspace = true


[features]
default = ["std"]

std = [
"frame-system/std",
"frame-support/std",
"sp-runtime/std",
"polimec-common/std",
"parity-scale-codec/std",
"scale-info/std",
"frame-benchmarking?/std",

"sp-io/std",
"pallet-linear-release/std",
"pallet-balances/std",
"pallet-assets/std"
]

try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"polimec-common/try-runtime"
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"polimec-common/runtime-benchmarks"
]
Loading