generated from substrate-developer-hub/substrate-node-template
-
Notifications
You must be signed in to change notification settings - Fork 60
/
trade_execution.rs
198 lines (171 loc) · 6.26 KB
/
trade_execution.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::*;
use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution};
use hydradx_traits::AMM;
use orml_traits::MultiCurrency;
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::DispatchError::Corruption;
use sp_runtime::{ArithmeticError, DispatchError, FixedPointNumber, FixedU128};
impl<T: Config> TradeExecution<T::RuntimeOrigin, T::AccountId, AssetId, Balance> for Pallet<T> {
type Error = DispatchError;
fn calculate_sell(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
) -> Result<Balance, ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
let assets = AssetPair { asset_in, asset_out };
let pool_id = Self::get_pair_id(assets);
let pool_data =
<PoolData<T>>::try_get(&pool_id).map_err(|_| ExecutorError::Error(Error::<T>::PoolNotFound.into()))?;
let now = T::BlockNumberProvider::current_block_number();
let (weight_in, weight_out) = Self::get_sorted_weight(assets.asset_in, now, &pool_data)
.map_err(|err| ExecutorError::Error(err.into()))?;
let asset_in_reserve = T::MultiCurrency::free_balance(assets.asset_in, &pool_id);
let asset_out_reserve = T::MultiCurrency::free_balance(assets.asset_out, &pool_id);
let amount_out = hydra_dx_math::lbp::calculate_out_given_in(
asset_in_reserve,
asset_out_reserve,
weight_in,
weight_out,
amount_in,
)
.map_err(|_| ExecutorError::Error(Error::<T>::Overflow.into()))?;
let fee_asset = pool_data.assets.0;
if fee_asset == assets.asset_in {
Ok(amount_out) //amount with fee applied as the user is responsible to send fee to the fee collector
} else {
let fee = Self::calculate_fees(&pool_data, amount_out).map_err(ExecutorError::Error)?;
let amount_out_without_fee = amount_out
.checked_sub(fee)
.ok_or_else(|| ExecutorError::Error(Error::<T>::Overflow.into()))?;
Ok(amount_out_without_fee) //amount without fee as the pool is responsible to send fee to the fee collector
}
}
fn calculate_buy(
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
) -> Result<Balance, ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
let assets = AssetPair { asset_in, asset_out };
let pool_id = Self::get_pair_id(assets);
let pool_data =
<PoolData<T>>::try_get(&pool_id).map_err(|_| ExecutorError::Error(Error::<T>::PoolNotFound.into()))?;
let now = T::BlockNumberProvider::current_block_number();
let (weight_in, weight_out) = Self::get_sorted_weight(assets.asset_in, now, &pool_data)
.map_err(|err| ExecutorError::Error(err.into()))?;
let asset_in_reserve = T::MultiCurrency::free_balance(assets.asset_in, &pool_id);
let asset_out_reserve = T::MultiCurrency::free_balance(assets.asset_out, &pool_id);
let fee_asset = pool_data.assets.0;
if fee_asset == assets.asset_out {
let fee = Self::calculate_fees(&pool_data, amount_out).map_err(ExecutorError::Error)?;
let amount_out_plus_fee = amount_out
.checked_add(fee)
.ok_or_else(|| ExecutorError::Error(Error::<T>::Overflow.into()))?;
let calculated_in = hydra_dx_math::lbp::calculate_in_given_out(
asset_in_reserve,
asset_out_reserve,
weight_in,
weight_out,
amount_out_plus_fee,
)
.map_err(|_| ExecutorError::Error(Error::<T>::Overflow.into()))?;
Ok(calculated_in) //TODO: Double check with someone if this is correct
} else {
let calculated_in = hydra_dx_math::lbp::calculate_in_given_out(
asset_in_reserve,
asset_out_reserve,
weight_in,
weight_out,
amount_out,
)
.map_err(|_| ExecutorError::Error(Error::<T>::Overflow.into()))?;
Ok(calculated_in) //amount with fee applied as the user is responsible to send fee to the fee collector
}
}
fn execute_sell(
who: T::RuntimeOrigin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_in: Balance,
min_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
Self::sell(who, asset_in, asset_out, amount_in, min_limit).map_err(ExecutorError::Error)
}
fn execute_buy(
who: T::RuntimeOrigin,
pool_type: PoolType<AssetId>,
asset_in: AssetId,
asset_out: AssetId,
amount_out: Balance,
max_limit: Balance,
) -> Result<(), ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
Self::buy(who, asset_out, asset_in, amount_out, max_limit).map_err(ExecutorError::Error)
}
fn get_liquidity_depth(
pool_type: PoolType<AssetId>,
asset_a: AssetId,
asset_b: AssetId,
) -> Result<Balance, ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
let asset_pair = AssetPair::new(asset_a, asset_b);
let pair_account = Self::get_pair_id(asset_pair);
let liquidty = T::MultiCurrency::free_balance(asset_a, &pair_account);
Ok(liquidty)
}
fn calculate_spot_price_with_fee(
pool_type: PoolType<AssetId>,
asset_a: AssetId,
asset_b: AssetId,
) -> Result<FixedU128, ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}
let assets = AssetPair {
asset_in: asset_a,
asset_out: asset_b,
};
let pool_id = Self::get_pair_id(assets);
let pool_data =
<PoolData<T>>::try_get(&pool_id).map_err(|_| ExecutorError::Error(Error::<T>::PoolNotFound.into()))?;
let now = T::BlockNumberProvider::current_block_number();
let (weight_in, weight_out) =
Self::get_sorted_weight(asset_a, now, &pool_data).map_err(|err| ExecutorError::Error(err.into()))?;
let fee_asset = pool_data.assets.0;
let fee = if Self::is_repay_fee_applied(&pool_data) {
Self::repay_fee()
} else {
pool_data.fee
};
let asset_in_reserve = T::MultiCurrency::free_balance(asset_a, &pool_id);
let asset_out_reserve = T::MultiCurrency::free_balance(asset_b, &pool_id);
let spot_price_with_fee = hydra_dx_math::lbp::calculate_spot_price_with_fee(
asset_in_reserve,
asset_out_reserve,
weight_in,
weight_out,
fee_asset,
asset_b,
Some(fee),
)
.map_err(|_| ExecutorError::Error(ArithmeticError::Overflow.into()))?
.reciprocal()
.ok_or(ExecutorError::Error(Corruption))?;
Ok(spot_price_with_fee)
}
}