Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into repo-organization
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Feb 20, 2024
2 parents da10c02 + 19e2b6d commit ed68aa1
Show file tree
Hide file tree
Showing 38 changed files with 2,865 additions and 160 deletions.
35 changes: 34 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ members = [
"pallets/restricted-xtokens",
"pallets/rewards",
"pallets/swaps",
"pallets/token-mux",
"pallets/transfer-allowlist",
"runtime/altair",
"runtime/centrifuge",
Expand Down Expand Up @@ -288,6 +289,7 @@ pallet-restricted-tokens = { path = "pallets/restricted-tokens", default-feature
pallet-restricted-xtokens = { path = "pallets/restricted-xtokens", default-features = false }
pallet-rewards = { path = "pallets/rewards", default-features = false }
pallet-swaps = { path = "pallets/swaps", default-features = false }
pallet-token-mux = { path = "pallets/token-mux", default-features = false }
pallet-transfer-allowlist = { path = "pallets/transfer-allowlist", default-features = false }

# Centrifuge libs
Expand Down
39 changes: 34 additions & 5 deletions libs/mocks/src/token_swaps.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::{OrderRatio, Swap, TokenSwaps};
use cfg_traits::{OrderInfo, OrderRatio, TokenSwaps};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

Expand Down Expand Up @@ -59,7 +59,8 @@ pub mod pallet {
}

pub fn mock_get_order_details(
f: impl Fn(T::OrderId) -> Option<Swap<T::BalanceOut, T::CurrencyId>> + 'static,
f: impl Fn(T::OrderId) -> Option<OrderInfo<T::BalanceOut, T::CurrencyId, T::Ratio>>
+ 'static,
) {
register_call!(f);
}
Expand All @@ -74,11 +75,24 @@ pub mod pallet {
) {
register_call!(move |(a, b, c)| f(a, b, c));
}

pub fn mock_fill_order(
f: impl Fn(T::AccountId, T::OrderId, T::BalanceOut) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c))
}

#[cfg(feature = "runtime-benchmarks")]
pub fn mock_add_trading_pair(
f: impl Fn(T::CurrencyId, T::CurrencyId, T::BalanceOut) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c))
}
}

impl<T: Config> TokenSwaps<T::AccountId> for Pallet<T> {
type BalanceIn = T::BalanceOut;
type BalanceOut = T::BalanceIn;
type BalanceIn = T::BalanceIn;
type BalanceOut = T::BalanceOut;
type CurrencyId = T::CurrencyId;
type OrderId = T::OrderId;
type Ratio = T::Ratio;
Expand Down Expand Up @@ -109,7 +123,9 @@ pub mod pallet {
execute_call!((a, b))
}

fn get_order_details(a: Self::OrderId) -> Option<Swap<Self::BalanceOut, Self::CurrencyId>> {
fn get_order_details(
a: Self::OrderId,
) -> Option<OrderInfo<Self::BalanceOut, Self::CurrencyId, Self::Ratio>> {
execute_call!(a)
}

Expand All @@ -120,5 +136,18 @@ pub mod pallet {
) -> Result<Self::BalanceIn, DispatchError> {
execute_call!((a, b, c))
}

fn fill_order(a: T::AccountId, b: Self::OrderId, c: Self::BalanceOut) -> DispatchResult {
execute_call!((a, b, c))
}

#[cfg(feature = "runtime-benchmarks")]
fn add_trading_pair(
a: Self::CurrencyId,
b: Self::CurrencyId,
c: Self::BalanceOut,
) -> DispatchResult {
execute_call!((a, b, c))
}
}
}
39 changes: 37 additions & 2 deletions libs/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ impl<Amount, Currency: PartialEq> Swap<Amount, Currency> {
}
}

/// The information of a swap order
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct OrderInfo<Balance, Currency, Ratio> {
/// The underlying currency swap
pub swap: Swap<Balance, Currency>,
/// The ratio at which the swap should happen
pub ratio: OrderRatio<Ratio>,
}

pub trait TokenSwaps<Account> {
type CurrencyId;
type BalanceOut;
Expand All @@ -466,6 +475,17 @@ pub trait TokenSwaps<Account> {
ratio: OrderRatio<Self::Ratio>,
) -> DispatchResult;

/// Fill an existing order up to the provided amount.
/// * If `amount` equals the `order.amount_out`, the order is completely
/// fulfilled.
/// * Else, the order is partially fulfilled for `amount /
/// order.amount_out`%.
fn fill_order(
account: Account,
order_id: Self::OrderId,
amount: Self::BalanceOut,
) -> DispatchResult;

/// A sanity check that can be used for validating that a trading pair
/// is supported. Will also be checked when placing an order but might be
/// cheaper.
Expand All @@ -475,15 +495,25 @@ pub trait TokenSwaps<Account> {
fn cancel_order(order: Self::OrderId) -> DispatchResult;

/// Retrieve the details of the order if it exists.
fn get_order_details(order: Self::OrderId) -> Option<Swap<Self::BalanceOut, Self::CurrencyId>>;
fn get_order_details(
order: Self::OrderId,
) -> Option<OrderInfo<Self::BalanceOut, Self::CurrencyId, Self::Ratio>>;

/// Makes a conversion between 2 currencies using the market ratio between
/// them
/// them.
fn convert_by_market(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
amount_out: Self::BalanceOut,
) -> Result<Self::BalanceIn, DispatchError>;

#[cfg(feature = "runtime-benchmarks")]
/// Adds a valid trading pair.
fn add_trading_pair(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
min_order: Self::BalanceOut,
) -> DispatchResult;
}

/// A representation of a currency swap in process.
Expand Down Expand Up @@ -689,3 +719,8 @@ impl<Source, Key, Value> ValueProvider<Source, Key> for NoProvider<Value> {
Err(DispatchError::Other("No value"))
}
}

/// Checks whether an asset is the local representation of another one
pub trait HasLocalAssetRepresentation<AssetRegistry> {
fn is_local_representation_of(&self, variant_currency: &Self) -> Result<bool, DispatchError>;
}
2 changes: 2 additions & 0 deletions libs/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sp-runtime = { workspace = true }
sp-std = { workspace = true }

orml-asset-registry = { workspace = true }
orml-traits = { workspace = true }
xcm = { workspace = true }

# local dependencies
Expand All @@ -47,6 +48,7 @@ std = [
"parity-scale-codec/std",
"frame-support/std",
"orml-asset-registry/std",
"orml-traits/std",
"scale-info/std",
"serde/std",
"sp-arithmetic/std",
Expand Down
1 change: 1 addition & 0 deletions libs/types/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub const STAKE_POT_PALLET_ID: PalletId = PalletId(*b"PotStake");
pub const BLOCK_REWARDS_PALLET_ID: PalletId = PalletId(*b"cfg/blrw");
pub const LIQUIDITY_REWARDS_PALLET_ID: PalletId = PalletId(*b"cfg/lqrw");
pub const POOL_FEES_PALLET_ID: PalletId = PalletId(*b"cfg/plfs");
pub const TOKEN_MUX_PALLET_ID: PalletId = PalletId(*b"cfg/tmux");

// Other ids
pub const CHAIN_BRIDGE_HASH_ID: [u8; 13] = *b"cent_nft_hash";
Expand Down
31 changes: 28 additions & 3 deletions libs/types/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,14 @@ mod tests {
use super::*;

mod saturated_proration {
use cfg_primitives::SECONDS_PER_YEAR;
use sp_arithmetic::traits::{One, Zero};
use cfg_primitives::{CFG, DAYS, SECONDS_PER_YEAR};
use sp_arithmetic::{
traits::{One, Zero},
FixedPointNumber,
};

use super::*;
use crate::fixed_point::Rate;
use crate::fixed_point::{Quantity, Rate};

type Balance = u128;

Expand Down Expand Up @@ -370,5 +373,27 @@ mod tests {
saturated_rate_proration::<Rate>(Rate::from_integer(2), u64::MAX) < right_bound
);
}

#[test]
fn precision_quantity_vs_rate() {
let period = (DAYS / 4) as Seconds;
let nav_multiplier = 1_000_000;
let nav = nav_multiplier * CFG;

let q_proration = saturated_rate_proration::<Quantity>(
Quantity::checked_from_rational(1, 100).unwrap(),
period,
);
let r_proration = saturated_rate_proration::<Rate>(
Rate::checked_from_rational(1, 100).unwrap(),
period,
);

let q_amount = q_proration.saturating_mul_int(nav);
let r_amount = r_proration.saturating_mul_int(nav);
let r_amount_rounded_up = (r_amount / (nav_multiplier) + 1) * (nav_multiplier);

assert_eq!(q_amount, r_amount_rounded_up);
}
}
}
Loading

0 comments on commit ed68aa1

Please sign in to comment.