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 crate::{Config, Error, Pallet, PoolData};
use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution};
use hydradx_traits::AMM;
use orml_traits::MultiCurrency;
use primitives::asset::AssetPair;
use primitives::{AssetId, Balance};
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::DispatchError;

impl<T: Config> TradeExecution<T::Origin, 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::Origin,
		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::Origin,
		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)
	}
}