Skip to content

Commit

Permalink
remove ratio from FI events
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Mar 11, 2024
1 parent 4ccdf67 commit c86da3c
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 53 deletions.
7 changes: 0 additions & 7 deletions libs/traits/src/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ pub trait Swaps<AccountId> {
type Amount;
type CurrencyId;
type SwapId;
type Ratio;

/// Apply a swap over a current possible swap state.
/// - If there was no previous swap, it adds it.
Expand Down Expand Up @@ -175,12 +174,6 @@ pub trait Swaps<AccountId> {
currency_id: Self::CurrencyId,
) -> DispatchResult;

/// Returns the conversion ratio to convert currency out into currency in,
fn market_ratio(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
) -> Result<Self::Ratio, DispatchError>;

/// Returns the pending amount for a pending swap. The direction of the swap
/// is determined by the `from_currency` parameter. The amount returned is
/// denominated in the same currency as the given `from_currency`.
Expand Down
14 changes: 5 additions & 9 deletions pallets/foreign-investments/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cfg_traits::{
};
use cfg_types::investments::CollectedAmount;
use frame_support::pallet_prelude::*;
use sp_runtime::traits::{EnsureAdd, EnsureAddAssign, EnsureSub, One, Zero};
use sp_runtime::traits::{EnsureAdd, EnsureAddAssign, EnsureSub, Zero};
use sp_std::marker::PhantomData;

use crate::{
Expand Down Expand Up @@ -221,19 +221,15 @@ impl<T: Config> Pallet<T> {
status: &SwapStatus<T::SwapBalance>,
) -> DispatchResult {
if !status.swapped.is_zero() {
Pallet::<T>::deposit_event(Event::SwapFullfilled {
Pallet::<T>::deposit_event(Event::SwapCancelled {
who: who.clone(),
swap_id,
remaining: Swap {
amount_out: status.pending,
..swap.clone()
},
swapped_in: status.swapped,
ratio: if swap.has_same_currencies() {
T::SwapRatio::one()
} else {
T::Swaps::market_ratio(swap.currency_in, swap.currency_out)?
},
cancelled_in: status.swapped,
pending_in: T::Swaps::pending_amount(who, swap_id, swap.currency_in)?,
});
}

Expand Down Expand Up @@ -272,7 +268,7 @@ impl<T: Config> StatusNotificationHook for FulfilledSwapHook<T> {
swap_id: (investment_id, action),
remaining: swap_info.remaining.clone(),
swapped_in: swap_info.swapped_in,
ratio: swap_info.ratio,
swapped_out: swap_info.swapped_out,
});

match action {
Expand Down
13 changes: 11 additions & 2 deletions pallets/foreign-investments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub mod pallet {
CurrencyId = Self::CurrencyId,
Amount = Self::SwapBalance,
SwapId = SwapId<Self>,
Ratio = Self::SwapRatio,
>;

/// The hook type which acts upon a finalized investment decrement.
Expand Down Expand Up @@ -273,17 +272,27 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
// The swap is created and now is wating to be fulfilled
SwapCreated {
who: T::AccountId,
swap_id: SwapId<T>,
swap: SwapOf<T>,
},
// The swap was fulfilled by another participant.
SwapFullfilled {
who: T::AccountId,
swap_id: SwapId<T>,
remaining: SwapOf<T>,
swapped_in: T::SwapBalance,
ratio: T::SwapRatio,
swapped_out: T::SwapBalance,
},
// The swap was fulfilled by cancelling an opposite swap for the same foreign investment.
SwapCancelled {
who: T::AccountId,
swap_id: SwapId<T>,
remaining: SwapOf<T>,
cancelled_in: T::SwapBalance,
pending_in: T::SwapBalance,
},
}
}
1 change: 0 additions & 1 deletion pallets/foreign-investments/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ impl pallet_swaps::Config for Runtime {
type FulfilledSwap = FulfilledSwapHook<Runtime>;
type OrderBook = MockTokenSwaps;
type OrderId = OrderId;
type Ratio = Ratio;
type SwapId = SwapId<Runtime>;
}

Expand Down
45 changes: 30 additions & 15 deletions pallets/foreign-investments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,6 @@ mod investment {
);

assert_eq!(ForeignInvestment::investment(&USER, INVESTMENT_ID), Ok(0));

System::assert_has_event(
Event::SwapFullfilled {
who: USER,
swap_id: (INVESTMENT_ID, Action::Investment),
remaining: Swap {
amount_out: 0,
currency_out: POOL_CURR,
currency_in: FOREIGN_CURR,
},
swapped_in: AMOUNT,
ratio: Ratio::from_rational(1, STABLE_RATIO),
}
.into(),
);
});
}

Expand Down Expand Up @@ -391,6 +376,21 @@ mod investment {
Ok(AMOUNT * 3 / 4)
);
assert_eq!(MockInvestment::investment(&USER, INVESTMENT_ID), Ok(0));

System::assert_has_event(
Event::SwapCancelled {
who: USER,
swap_id: (INVESTMENT_ID, Action::Investment),
remaining: Swap {
amount_out: 0,
currency_out: POOL_CURR,
currency_in: FOREIGN_CURR,
},
cancelled_in: AMOUNT / 4,
pending_in: 3 * AMOUNT / 4,
}
.into(),
);
});
}

Expand Down Expand Up @@ -449,6 +449,21 @@ mod investment {
MockInvestment::investment(&USER, INVESTMENT_ID),
Ok(foreign_to_pool(AMOUNT / 4))
);

System::assert_has_event(
Event::SwapFullfilled {
who: USER,
swap_id: (INVESTMENT_ID, Action::Investment),
remaining: Swap {
amount_out: 3 * AMOUNT / 4,
currency_out: FOREIGN_CURR,
currency_in: POOL_CURR,
},
swapped_in: foreign_to_pool(AMOUNT / 4),
swapped_out: AMOUNT / 4,
}
.into(),
);
});
}

Expand Down
21 changes: 6 additions & 15 deletions pallets/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ pub mod pallet {
StatusNotificationHook,
};
use frame_support::pallet_prelude::*;
use sp_runtime::traits::{AtLeast32BitUnsigned, EnsureAdd, EnsureSub, One, Zero};
use sp_runtime::traits::{AtLeast32BitUnsigned, EnsureAdd, EnsureSub, Zero};
use sp_std::cmp::Ordering;

use super::*;

pub type RatioOf<T> =
<<T as Config>::OrderBook as TokenSwaps<<T as frame_system::Config>::AccountId>>::Ratio;

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand All @@ -46,9 +49,6 @@ pub mod pallet {
/// Represents an amount that can be swapped
type Balance: Parameter + Member + AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen;

/// Represents an amount that can be swapped
type Ratio: Copy + One + Zero;

/// An identification for a swap
type SwapId: Parameter + Member + Copy + Ord + MaxEncodedLen;

Expand All @@ -65,13 +65,12 @@ pub mod pallet {
BalanceIn = Self::Balance,
BalanceOut = Self::Balance,
OrderId = Self::OrderId,
Ratio = Self::Ratio,
>;

/// The hook which acts upon a (partially) fulfilled the swap
type FulfilledSwap: StatusNotificationHook<
Id = (Self::AccountId, Self::SwapId),
Status = SwapInfo<Self::Balance, Self::Balance, Self::CurrencyId, Self::Ratio>,
Status = SwapInfo<Self::Balance, Self::Balance, Self::CurrencyId, RatioOf<Self>>,
Error = DispatchError,
>;
}
Expand Down Expand Up @@ -287,7 +286,6 @@ pub mod pallet {
impl<T: Config> Swaps<T::AccountId> for Pallet<T> {
type Amount = T::Balance;
type CurrencyId = T::CurrencyId;
type Ratio = T::Ratio;
type SwapId = T::SwapId;

fn apply_swap(
Expand Down Expand Up @@ -327,13 +325,6 @@ pub mod pallet {
}
}

fn market_ratio(
currency_in: T::CurrencyId,
currency_out: T::CurrencyId,
) -> Result<T::Ratio, DispatchError> {
T::OrderBook::market_ratio(currency_in, currency_out)
}

fn pending_amount(
who: &T::AccountId,
swap_id: Self::SwapId,
Expand All @@ -350,7 +341,7 @@ pub mod pallet {
impl<T: Config> StatusNotificationHook for Pallet<T> {
type Error = DispatchError;
type Id = T::OrderId;
type Status = SwapInfo<T::Balance, T::Balance, T::CurrencyId, T::Ratio>;
type Status = SwapInfo<T::Balance, T::Balance, T::CurrencyId, RatioOf<T>>;

fn notify_status_change(order_id: T::OrderId, swap_info: Self::Status) -> DispatchResult {
if let Ok((who, swap_id)) = Self::swap_id(order_id) {
Expand Down
1 change: 0 additions & 1 deletion pallets/swaps/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl pallet_swaps::Config for Runtime {
type FulfilledSwap = FulfilledSwapHook;
type OrderBook = MockTokenSwaps;
type OrderId = OrderId;
type Ratio = Ratio;
type SwapId = SwapId;
}

Expand Down
1 change: 0 additions & 1 deletion runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,6 @@ impl pallet_swaps::Config for Runtime {
type FulfilledSwap = pallet_foreign_investments::FulfilledSwapHook<Runtime>;
type OrderBook = OrderBook;
type OrderId = OrderId;
type Ratio = Ratio;
type SwapId = pallet_foreign_investments::SwapId<Runtime>;
}

Expand Down
1 change: 0 additions & 1 deletion runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,6 @@ impl pallet_swaps::Config for Runtime {
type FulfilledSwap = pallet_foreign_investments::FulfilledSwapHook<Runtime>;
type OrderBook = OrderBook;
type OrderId = OrderId;
type Ratio = Ratio;
type SwapId = pallet_foreign_investments::SwapId<Runtime>;
}

Expand Down
1 change: 0 additions & 1 deletion runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,6 @@ impl pallet_swaps::Config for Runtime {
type FulfilledSwap = pallet_foreign_investments::FulfilledSwapHook<Runtime>;
type OrderBook = OrderBook;
type OrderId = OrderId;
type Ratio = Ratio;
type SwapId = pallet_foreign_investments::SwapId<Runtime>;
}

Expand Down

0 comments on commit c86da3c

Please sign in to comment.