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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::sp_runtime::RuntimeDebug;
use hydra_dx_math::ema::{calculate_new_by_integrating_incoming, update_outdated_to_current, EmaPrice};
use hydra_dx_math::types::Fraction;
use hydradx_traits::{AggregatedEntry, Liquidity, Volume};
use scale_info::TypeInfo;
use sp_arithmetic::traits::{AtLeast32BitUnsigned, SaturatedConversion, UniqueSaturatedInto};
pub use hydradx_traits::{OraclePeriod, Source};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
pub type AssetId = u32;
pub type Balance = u128;
pub type Price = EmaPrice;
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(RuntimeDebug, Encode, Decode, Clone, PartialEq, Eq, Default, TypeInfo, MaxEncodedLen)]
pub struct OracleEntry<BlockNumber> {
pub price: Price,
pub volume: Volume<Balance>,
pub liquidity: Liquidity<Balance>,
pub timestamp: BlockNumber,
}
impl<BlockNumber> OracleEntry<BlockNumber>
where
BlockNumber: AtLeast32BitUnsigned + Copy + UniqueSaturatedInto<u64>,
{
pub fn into_aggregated(self, initialized: BlockNumber) -> AggregatedEntry<Balance, BlockNumber, Price> {
AggregatedEntry {
price: self.price,
volume: self.volume,
liquidity: self.liquidity,
oracle_age: self.timestamp.saturating_sub(initialized),
}
}
pub fn raw_data(&self) -> (Price, (Balance, Balance, Balance, Balance), (Balance, Balance)) {
(self.price, self.volume.clone().into(), self.liquidity.into())
}
pub fn inverted(self) -> Self {
let price = if self.price.is_zero() {
self.price
} else {
let (a, b) = self.price.into();
(b, a).into()
};
let volume = self.volume.inverted();
let liquidity = self.liquidity.inverted();
Self {
price,
volume,
liquidity,
timestamp: self.timestamp,
}
}
pub fn accumulate_volume_and_update_from(&mut self, incoming: &Self) {
self.volume = incoming.volume.saturating_add(&self.volume);
self.price = incoming.price;
self.liquidity = incoming.liquidity;
self.timestamp = incoming.timestamp;
}
pub fn fast_forward_to(&mut self, new_timestamp: BlockNumber) {
self.timestamp = new_timestamp;
self.volume = Volume::default();
}
pub fn with_added_volume_from(&self, previous_entry: &Self) -> Self {
let volume = previous_entry.volume.saturating_add(&self.volume);
Self {
price: self.price,
volume,
liquidity: self.liquidity,
timestamp: self.timestamp,
}
}
pub fn calculate_new_by_integrating_incoming(&self, period: OraclePeriod, incoming: &Self) -> Option<Self> {
if !incoming.timestamp.checked_sub(&self.timestamp)?.is_one() {
return None;
}
if period == OraclePeriod::LastBlock {
return Some(incoming.clone());
}
let smoothing = into_smoothing(period);
let (price, volume, liquidity) =
calculate_new_by_integrating_incoming(self.raw_data(), incoming.raw_data(), smoothing);
Some(Self {
price,
volume: volume.into(),
liquidity: liquidity.into(),
timestamp: incoming.timestamp,
})
}
pub fn update_to_new_by_integrating_incoming(
&mut self,
period: OraclePeriod,
incoming: &Self,
) -> Option<&mut Self> {
*self = self.calculate_new_by_integrating_incoming(period, incoming)?;
Some(self)
}
pub fn calculate_current_from_outdated(&self, period: OraclePeriod, update_with: &Self) -> Option<Self> {
let iterations = update_with.timestamp.checked_sub(&self.timestamp)?;
if iterations.is_zero() {
return None;
}
if period == OraclePeriod::LastBlock {
return Some(update_with.clone());
}
let smoothing = into_smoothing(period);
let (price, volume, liquidity) = update_outdated_to_current(
iterations.saturated_into(),
self.raw_data(),
(update_with.price, update_with.liquidity.into()),
smoothing,
);
Some(Self {
price,
volume: volume.into(),
liquidity: liquidity.into(),
timestamp: update_with.timestamp,
})
}
pub fn update_outdated_to_current(&mut self, period: OraclePeriod, update_with: &Self) -> Option<&mut Self> {
*self = self.calculate_current_from_outdated(period, update_with)?;
Some(self)
}
}
pub fn into_smoothing(period: OraclePeriod) -> Fraction {
match period {
OraclePeriod::LastBlock => Fraction::from_bits(170141183460469231731687303715884105728),
OraclePeriod::Short => Fraction::from_bits(34028236692093846346337460743176821146),
OraclePeriod::TenMinutes => Fraction::from_bits(3369132345751865974884897103284833777),
OraclePeriod::Hour => Fraction::from_bits(566193622164623067326746434994622648),
OraclePeriod::Day => Fraction::from_bits(23629079016800115510268356880200556),
OraclePeriod::Week => Fraction::from_bits(3375783642235081630771268215908257),
}
}
impl<BlockNumber> From<(Price, Volume<Balance>, Liquidity<Balance>, BlockNumber)> for OracleEntry<BlockNumber> {
fn from((price, volume, liquidity, timestamp): (Price, Volume<Balance>, Liquidity<Balance>, BlockNumber)) -> Self {
Self {
price,
volume,
liquidity,
timestamp,
}
}
}