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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use codec::{Decode, Encode};
use scale_info::TypeInfo;
#[derive(Encode, Decode, Clone, Copy, Debug, Eq, PartialEq, TypeInfo)]
pub enum PoolType<AssetId> {
XYK,
LBP,
Stableswap(AssetId),
Omnipool,
}
#[derive(Debug, PartialEq, Eq)]
pub enum ExecutorError<E> {
NotSupported,
Error(E),
}
pub trait TradeExecution<Origin, AccountId, AssetId, Balance> {
type Error;
fn calculate_sell(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
) -> Result<Balance, ExecutorError<Self::Error>>;
fn calculate_buy(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
) -> Result<Balance, ExecutorError<Self::Error>>;
fn execute_sell(
who: Origin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
min_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>>;
fn execute_buy(
who: Origin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
max_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>>;
}
#[allow(clippy::redundant_clone)] #[impl_trait_for_tuples::impl_for_tuples(1, 5)]
impl<E: PartialEq, Origin: Clone, AccountId, AssetId: Copy, Balance: Copy>
TradeExecution<Origin, AccountId, AssetId, Balance> for Tuple
{
for_tuples!( where #(Tuple: TradeExecution<Origin,AccountId, AssetId, Balance, Error=E>)*);
type Error = E;
fn calculate_sell(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
) -> Result<Balance, ExecutorError<Self::Error>> {
for_tuples!(
#(
let value = match Tuple::calculate_sell(pool_type, asset_in,asset_out,amount_in) {
Ok(result) => return Ok(result),
Err(v) if v == ExecutorError::NotSupported => v,
Err(v) => return Err(v),
};
)*
);
Err(value)
}
fn calculate_buy(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
) -> Result<Balance, ExecutorError<Self::Error>> {
for_tuples!(
#(
let value = match Tuple::calculate_buy(pool_type, asset_in,asset_out,amount_out) {
Ok(result) => return Ok(result),
Err(v) if v == ExecutorError::NotSupported => v,
Err(v) => return Err(v),
};
)*
);
Err(value)
}
fn execute_sell(
who: Origin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
min_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>> {
for_tuples!(
#(
let value = match Tuple::execute_sell(who.clone(),pool_type, asset_in, asset_out, amount_in, min_limit) {
Ok(result) => return Ok(result),
Err(v) if v == ExecutorError::NotSupported => v,
Err(v) => return Err(v),
};
)*
);
Err(value)
}
fn execute_buy(
who: Origin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
max_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>> {
for_tuples!(
#(
let value = match Tuple::execute_buy(who.clone(), pool_type,asset_in, asset_out, amount_out, max_limit) {
Ok(result) => return Ok(result),
Err(v) if v == ExecutorError::NotSupported => v,
Err(v) => return Err(v),
};
)*
);
Err(value)
}
}