Skip to content

Commit

Permalink
wip on order-book
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre committed Sep 29, 2023
1 parent 4df0ca2 commit 89cd13e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pallets/order-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod pallet {
},
Twox64Concat,
};
use frame_support::traits::tokens::{Precision, Preservation};
use frame_system::pallet_prelude::{OriginFor, *};
use orml_traits::asset_registry::{self, Inspect as _};
use scale_info::TypeInfo;
Expand All @@ -76,6 +77,8 @@ pub mod pallet {
>;
pub type BalanceOf<T> = <T as Config>::Balance;

type ReasonOf<T: Config> = <T::TradeableAsset as InspectHold<<T as frame_system::Config>::AccountId>>::Reason;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

Expand Down Expand Up @@ -405,6 +408,7 @@ pub mod pallet {
order_id: T::OrderIdNonce,
buy_amount: T::Balance,
price: T::SellRatio,
reason: ReasonOf<T>,
) -> DispatchResult {
let account_id = ensure_signed(origin)?;
Self::inner_update_order(
Expand All @@ -430,6 +434,7 @@ pub mod pallet {
min_amount,
)
},
reason,
)
}

Expand Down Expand Up @@ -457,12 +462,12 @@ pub mod pallet {
/// Fill an existing order, fulfilling the entire order.
#[pallet::call_index(3)]
#[pallet::weight(T::Weights::fill_order_full())]
pub fn fill_order_full(origin: OriginFor<T>, order_id: T::OrderIdNonce) -> DispatchResult {
pub fn fill_order_full(origin: OriginFor<T>, order_id: T::OrderIdNonce, reason: ReasonOf<T>) -> DispatchResult {
let account_id = ensure_signed(origin)?;
let order = <Orders<T>>::get(order_id)?;
let buy_amount = order.buy_amount;

Self::fulfill_order_with_amount(order, buy_amount, account_id)
Self::fulfill_order_with_amount(order, buy_amount, account_id, reason)
}

/// Adds a valid trading pair.
Expand Down Expand Up @@ -550,11 +555,12 @@ pub mod pallet {
origin: OriginFor<T>,
order_id: T::OrderIdNonce,
buy_amount: T::Balance,
reason: ReasonOf<T>,
) -> DispatchResult {
let account_id = ensure_signed(origin)?;
let order = <Orders<T>>::get(order_id)?;

Self::fulfill_order_with_amount(order, buy_amount, account_id)
Self::fulfill_order_with_amount(order, buy_amount, account_id, reason)
}
}

Expand All @@ -563,14 +569,15 @@ pub mod pallet {
order: OrderOf<T>,
buy_amount: T::Balance,
account_id: T::AccountId,
reason: ReasonOf<T>,
) -> DispatchResult {
ensure!(
buy_amount >= order.min_fulfillment_amount,
Error::<T>::InsufficientOrderSize,
);

ensure!(
T::TradeableAsset::can_hold(order.asset_in_id, &account_id, buy_amount),
T::TradeableAsset::can_hold(order.asset_in_id, &reason, &account_id, buy_amount),
Error::<T>::InsufficientAssetFunds,
);

Expand All @@ -597,9 +604,10 @@ pub mod pallet {
} else {
T::TradeableAsset::release(
order.asset_out_id,
&reason,
&order.placing_account,
sell_amount,
false,
Precision::Exact,
)?;

Self::remove_order(order.order_id)?;
Expand All @@ -610,14 +618,14 @@ pub mod pallet {
&account_id,
&order.placing_account,
buy_amount,
false,
Preservation::Expendable,
)?;
T::TradeableAsset::transfer(
order.asset_out_id,
&order.placing_account,
&account_id,
sell_amount,
false,
Preservation::Expendable,
)?;

T::FulfilledOrderHook::notify_status_change(
Expand Down Expand Up @@ -656,12 +664,13 @@ pub mod pallet {

/// Unreserve funds for an order that is finished either
/// through fulfillment or cancellation.
pub fn unreserve_order(order: &OrderOf<T>) -> Result<BalanceOf<T>, DispatchError> {
pub fn unreserve_order(order: &OrderOf<T>, reason: ReasonOf<T>) -> Result<BalanceOf<T>, DispatchError> {
T::TradeableAsset::release(
order.asset_out_id,
&reason,
&order.placing_account,
order.max_sell_amount,
false,
Precision::Exact,
)
}

Expand Down Expand Up @@ -742,6 +751,7 @@ pub mod pallet {
sell_rate_limit: T::SellRatio,
min_fulfillment_amount: T::Balance,
validate: impl FnOnce(&OrderOf<T>) -> DispatchResult,
reason: ReasonOf<T>,
) -> DispatchResult {
let max_sell_amount = <Orders<T>>::try_mutate_exists(
order_id,
Expand All @@ -764,6 +774,7 @@ pub mod pallet {
max_sell_amount.ensure_sub(order.max_sell_amount)?;
T::TradeableAsset::hold(
order.asset_out_id,
&reason,
&account,
sell_reserve_diff,
)?;
Expand All @@ -772,9 +783,10 @@ pub mod pallet {
order.max_sell_amount.ensure_sub(max_sell_amount)?;
T::TradeableAsset::release(
order.asset_out_id,
&reason,
&account,
sell_reserve_diff,
false,
Precision::Exact,
)?;
}
};
Expand Down Expand Up @@ -938,6 +950,7 @@ pub mod pallet {
buy_amount: T::Balance,
sell_rate_limit: T::SellRatio,
min_fulfillment_amount: T::Balance,
reason: ReasonOf<T>,
) -> DispatchResult {
Self::inner_update_order(
account,
Expand All @@ -959,6 +972,7 @@ pub mod pallet {
T::Balance::zero(),
)
},
reason,
)
}

Expand Down

0 comments on commit 89cd13e

Please sign in to comment.