use frame_support::pallet_prelude::{RuntimeDebug, TypeInfo};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::fmt::Debug;
#[derive(Clone, Copy, Debug, Encode, Decode, Eq, PartialEq, MaxEncodedLen, TypeInfo)]
pub enum OrderRatio<Ratio> {
Market,
Custom(Ratio),
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Swap<Amount, Currency> {
pub currency_in: Currency,
pub currency_out: Currency,
pub amount_out: Amount,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct OrderInfo<Balance, Currency, Ratio> {
pub swap: Swap<Balance, Currency>,
pub ratio: OrderRatio<Ratio>,
}
pub trait TokenSwaps<Account> {
type CurrencyId;
type BalanceOut;
type BalanceIn;
type Ratio;
type OrderId;
fn place_order(
account: Account,
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
amount_out: Self::BalanceOut,
ratio: OrderRatio<Self::Ratio>,
) -> Result<Self::OrderId, DispatchError>;
fn update_order(
order_id: Self::OrderId,
amount_out: Self::BalanceOut,
ratio: OrderRatio<Self::Ratio>,
) -> DispatchResult;
fn fill_order(
account: Account,
order_id: Self::OrderId,
amount: Self::BalanceOut,
) -> DispatchResult;
fn cancel_order(order: Self::OrderId) -> DispatchResult;
fn get_order_details(
order: Self::OrderId,
) -> Option<OrderInfo<Self::BalanceOut, Self::CurrencyId, Self::Ratio>>;
fn convert_by_market(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
amount_out: Self::BalanceOut,
) -> Result<Self::BalanceIn, DispatchError>;
fn market_ratio(
currency_in: Self::CurrencyId,
currency_out: Self::CurrencyId,
) -> Result<Self::Ratio, DispatchError>;
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct SwapInfo<AmountIn, AmountOut, Currency, Ratio> {
pub remaining: Swap<AmountOut, Currency>,
pub swapped_in: AmountIn,
pub swapped_out: AmountOut,
pub ratio: Ratio,
}