1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use frame_support::pallet_prelude::{RuntimeDebug, TypeInfo};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::fmt::Debug;

/// Determines an order price
#[derive(Clone, Copy, Debug, Encode, Decode, Eq, PartialEq, MaxEncodedLen, TypeInfo)]
pub enum OrderRatio<Ratio> {
	Market,
	Custom(Ratio),
}

/// A simple representation of a currency swap.
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct Swap<Amount, Currency> {
	/// The incoming currency, i.e. the desired one.
	pub currency_in: Currency,

	/// The outgoing currency, i.e. the one which should be replaced.
	pub currency_out: Currency,

	/// The amount of outcoming currency that will be swapped.
	pub amount_out: Amount,
}

/// 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;
	type BalanceIn;
	type Ratio;
	type OrderId;

	/// Swap tokens selling `amount_out` of `currency_out` and buying
	/// `currency_in` given an order ratio.
	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>;

	/// Update an existing active order.
	fn update_order(
		order_id: Self::OrderId,
		amount_out: Self::BalanceOut,
		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;

	/// Cancel an already active order.
	fn cancel_order(order: Self::OrderId) -> DispatchResult;

	/// Retrieve the details of the order if it exists.
	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.
	fn convert_by_market(
		currency_in: Self::CurrencyId,
		currency_out: Self::CurrencyId,
		amount_out: Self::BalanceOut,
	) -> Result<Self::BalanceIn, DispatchError>;

	/// 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>;
}

/// A representation of a currency swap in process.
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct SwapInfo<AmountIn, AmountOut, Currency, Ratio> {
	/// Swap not yet processed with the pending outcomming amount
	pub remaining: Swap<AmountOut, Currency>,

	/// Amount of incoming currency already swapped
	pub swapped_in: AmountIn,

	/// Amount of incoming currency already swapped denominated in outgoing
	/// currency
	pub swapped_out: AmountOut,

	/// Ratio used to swap `swapped_out` into `swapped_in`
	pub ratio: Ratio,
}