-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmod.rs
More file actions
94 lines (87 loc) · 2.76 KB
/
Copy pathmod.rs
File metadata and controls
94 lines (87 loc) · 2.76 KB
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
use crate::tests::mock::*;
use crate::*;
use frame_support::pallet_prelude::ConstU32;
use frame_support::BoundedVec;
use sp_runtime::FixedU128;
mod add_liquidity;
mod amplification;
mod calculate_spot_price;
mod creation;
mod hooks;
mod invariants;
pub(crate) mod mock;
mod peg;
mod peg_one;
mod price;
mod remove_liquidity;
mod trades;
mod update_max_peg_update;
mod update_peg_source;
mod update_pool;
type Balance = u128;
#[macro_export]
macro_rules! to_precision {
($e:expr, $f:expr) => {
$e * 10u128.pow($f as u32)
};
}
pub(crate) fn get_share_price(pool_id: AssetId, asset_idx: usize) -> FixedU128 {
let pool_account = pool_account(pool_id);
let pool = <Pools<Test>>::get(pool_id).unwrap();
let balances = pool.reserves_with_decimals::<Test>(&pool_account).unwrap();
let amp = Pallet::<Test>::get_amplification(&pool);
let issuance = Tokens::total_issuance(pool_id);
let (_, asset_pegs) = Pallet::<Test>::get_updated_pegs(pool_id, &pool).unwrap();
let share_price = hydra_dx_math::stableswap::calculate_share_price::<128u8>(
&balances,
amp,
issuance,
asset_idx,
None,
&asset_pegs,
)
.unwrap();
FixedU128::from_rational(share_price.0, share_price.1)
}
pub(crate) fn spot_price_first_asset(pool_id: AssetId, asset_id: AssetId) -> FixedU128 {
let pool_account = pool_account(pool_id);
let pool = <Pools<Test>>::get(pool_id).unwrap();
let balances = pool.reserves_with_decimals::<Test>(&pool_account).unwrap();
let amp = Pallet::<Test>::get_amplification(&pool);
let asset_idx = pool.find_asset(asset_id).unwrap();
let (_, asset_pegs) = Pallet::<Test>::get_updated_pegs(pool_id, &pool).unwrap();
let d = hydra_dx_math::stableswap::calculate_d::<D_ITERATIONS>(&balances, amp, &asset_pegs).unwrap();
hydra_dx_math::stableswap::calculate_spot_price_between_two_stable_assets(
&balances,
amp,
d,
0,
asset_idx,
None,
&asset_pegs,
)
.unwrap()
}
pub(crate) fn spot_price(pool_id: AssetId, asset_id_a: AssetId, asset_id_b: AssetId) -> FixedU128 {
let pool_account = pool_account(pool_id);
let pool = <Pools<Test>>::get(pool_id).unwrap();
let balances = pool.reserves_with_decimals::<Test>(&pool_account).unwrap();
let amp = Pallet::<Test>::get_amplification(&pool);
let asset_idx_a = pool.find_asset(asset_id_a).unwrap();
let asset_idx_b = pool.find_asset(asset_id_b).unwrap();
let (_, asset_pegs) = Pallet::<Test>::get_updated_pegs(pool_id, &pool).unwrap();
let d = hydra_dx_math::stableswap::calculate_d::<D_ITERATIONS>(&balances, amp, &asset_pegs).unwrap();
hydra_dx_math::stableswap::calculate_spot_price_between_two_stable_assets(
&balances,
amp,
d,
asset_idx_a,
asset_idx_b,
None,
&asset_pegs,
)
.unwrap()
}
pub(crate) fn to_bounded_asset_vec(vec: Vec<AssetId>) -> BoundedVec<AssetId, ConstU32<MAX_ASSETS_IN_POOL>> {
vec.try_into().unwrap()
}