Skip to content

Commit

Permalink
order-book: Move extrinsic function
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Sep 19, 2023
1 parent 3f57549 commit 97c6940
Showing 1 changed file with 88 additions and 88 deletions.
176 changes: 88 additions & 88 deletions pallets/order-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,84 @@ pub mod pallet {
Ok(())
}

/// Adds a valid trading pair.
#[pallet::call_index(4)]
#[pallet::weight(T::Weights::add_trading_pair())]
pub fn add_trading_pair(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
min_order: T::Balance,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// We do not check, we just overwrite as this is an admin action.
TradingPair::<T>::insert(asset_in, asset_out, min_order);

Self::deposit_event(Event::<T>::TradingPairAdded {
asset_in,
asset_out,
min_order,
});

Ok(())
}

/// Removes a valid trading pair
//
// NOTE: We do not need to remove existing order as
// fulfilling orders is not checking for a valid trading pair.
// Existing orders will just fade out by by being canceled
// or fulfilled.
#[pallet::call_index(5)]
#[pallet::weight(T::Weights::rm_trading_pair())]
pub fn rm_trading_pair(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// We do not check, we just remove as this is an admin action.
TradingPair::<T>::remove(asset_in, asset_out);

Self::deposit_event(Event::<T>::TradingPairRemoved {
asset_in,
asset_out,
});

Ok(())
}

/// Sets the minimum order amount for a given trading pair.
/// If the trading pair is not yet added this errors out.
//
// NOTE: We do not need to update any existing orders as fulfillment does
// not verify the validity of the order that is to be fulfilled.
#[pallet::call_index(6)]
#[pallet::weight(T::Weights::update_min_order())]
pub fn update_min_order(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
min_order: T::Balance,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// try_mutate is a pain with the direct error return. But we do want to only
// update if the pair exists.
let _old_min_order = TradingPair::<T>::get(&asset_in, &asset_out)?;
TradingPair::<T>::insert(&asset_in, &asset_out, min_order);

Self::deposit_event(Event::<T>::MinOrderUpdated {
asset_in,
asset_out,
min_order,
});

Ok(())
}

/// Fill an existing order, based on the provided ratio.
#[pallet::call_index(7)]
#[pallet::weight(T::Weights::fill_order_partial())]
Expand All @@ -539,6 +617,16 @@ pub mod pallet {
.ok_or(Error::<T>::RemainingBuyAmountError)?;
let partial_fulfillment = !remaining_buy_amount.is_zero();

ensure!(
partial_buy_amount >= order.min_fulfillment_amount,
Error::<T>::InsufficientOrderSize,
);

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

if partial_fulfillment {
Self::update_order(
order.placing_account.clone(),
Expand All @@ -558,16 +646,6 @@ pub mod pallet {
Self::remove_order(order.order_id)?;
}

ensure!(
partial_buy_amount >= order.min_fulfillment_amount,
Error::<T>::InsufficientOrderSize,
);

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

T::TradeableAsset::transfer(
order.asset_in_id,
&account_id,
Expand Down Expand Up @@ -606,84 +684,6 @@ pub mod pallet {

Ok(())
}

/// Adds a valid trading pair.
#[pallet::call_index(4)]
#[pallet::weight(T::Weights::add_trading_pair())]
pub fn add_trading_pair(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
min_order: T::Balance,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// We do not check, we just overwrite as this is an admin action.
TradingPair::<T>::insert(asset_in, asset_out, min_order);

Self::deposit_event(Event::<T>::TradingPairAdded {
asset_in,
asset_out,
min_order,
});

Ok(())
}

/// Removes a valid trading pair
//
// NOTE: We do not need to remove existing order as
// fulfilling orders is not checking for a valid trading pair.
// Existing orders will just fade out by by being canceled
// or fulfilled.
#[pallet::call_index(5)]
#[pallet::weight(T::Weights::rm_trading_pair())]
pub fn rm_trading_pair(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// We do not check, we just remove as this is an admin action.
TradingPair::<T>::remove(asset_in, asset_out);

Self::deposit_event(Event::<T>::TradingPairRemoved {
asset_in,
asset_out,
});

Ok(())
}

/// Sets the minimum order amount for a given trading pair.
/// If the trading pair is not yet added this errors out.
//
// NOTE: We do not need to update any existing orders as fulfillment does
// not verify the validity of the order that is to be fulfilled.
#[pallet::call_index(6)]
#[pallet::weight(T::Weights::update_min_order())]
pub fn update_min_order(
origin: OriginFor<T>,
asset_in: T::AssetCurrencyId,
asset_out: T::AssetCurrencyId,
min_order: T::Balance,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;

// try_mutate is a pain with the direct error return. But we do want to only
// update if the pair exists.
let _old_min_order = TradingPair::<T>::get(&asset_in, &asset_out)?;
TradingPair::<T>::insert(&asset_in, &asset_out, min_order);

Self::deposit_event(Event::<T>::MinOrderUpdated {
asset_in,
asset_out,
min_order,
});

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down

0 comments on commit 97c6940

Please sign in to comment.