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
use codec::{Decode, Encode};
use frame_support::sp_runtime::traits::{CheckedAdd, CheckedDiv, CheckedMul, Zero};
use frame_support::sp_runtime::{FixedU128, RuntimeDebug};
use scale_info::TypeInfo;
use sp_std::iter::Sum;
use sp_std::ops::{Add, Index, IndexMut};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
pub type AssetId = u32;
pub type Balance = u128;
pub type Price = FixedU128;
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(RuntimeDebug, Encode, Decode, Copy, Clone, PartialEq, Eq, Default, TypeInfo)]
pub struct PriceEntry {
pub price: Price,
pub trade_amount: Balance,
pub liquidity_amount: Balance,
}
impl Add for PriceEntry {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
price: self.price.add(other.price),
trade_amount: self.trade_amount.add(other.trade_amount),
liquidity_amount: self.liquidity_amount.add(other.liquidity_amount),
}
}
}
impl Zero for PriceEntry {
fn zero() -> Self {
Self {
price: Price::zero(),
trade_amount: Balance::zero(),
liquidity_amount: Balance::zero(),
}
}
fn is_zero(&self) -> bool {
self == &PriceEntry::zero()
}
}
impl Add for &PriceEntry {
type Output = PriceEntry;
fn add(self, other: Self) -> Self::Output {
PriceEntry {
price: self.price.add(other.price),
trade_amount: self.trade_amount.add(other.trade_amount),
liquidity_amount: self.liquidity_amount.add(other.liquidity_amount),
}
}
}
impl<'a> Sum<&'a Self> for PriceEntry {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(PriceEntry::default(), |a, b| &a + b)
}
}
impl PriceEntry {
pub fn calculate_new_price_entry(&self, previous_price_entry: &Self) -> Option<Self> {
let total_liquidity = previous_price_entry
.liquidity_amount
.checked_add(self.liquidity_amount)?;
let product_of_old_values = previous_price_entry
.price
.checked_mul(&Price::from_inner(previous_price_entry.liquidity_amount))?;
let product_of_new_values = self.price.checked_mul(&Price::from_inner(self.liquidity_amount))?;
Some(Self {
price: product_of_old_values
.checked_add(&product_of_new_values)?
.checked_div(&Price::from_inner(total_liquidity))?,
trade_amount: previous_price_entry.trade_amount.checked_add(self.trade_amount)?,
liquidity_amount: total_liquidity,
})
}
}
pub const BUCKET_SIZE: u32 = 10;
pub type Bucket = [PriceInfo; BUCKET_SIZE as usize];
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(RuntimeDebug, Encode, Decode, Copy, Clone, PartialEq, Eq, Default, TypeInfo)]
pub struct PriceInfo {
pub avg_price: Price,
pub volume: Balance,
}
impl Add for &PriceInfo {
type Output = PriceInfo;
fn add(self, other: Self) -> Self::Output {
PriceInfo {
avg_price: self.avg_price.add(other.avg_price),
volume: self.volume.add(other.volume),
}
}
}
impl<'a> Sum<&'a Self> for PriceInfo {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(PriceInfo::default(), |a, b| &a + b)
}
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(RuntimeDebug, Encode, Decode, Copy, Clone, PartialEq, Eq, TypeInfo)]
pub struct BucketQueue {
bucket: Bucket,
last: u32,
}
impl BucketQueue {
pub const BUCKET_SIZE: u32 = BUCKET_SIZE;
}
impl Default for BucketQueue {
fn default() -> Self {
Self {
bucket: Bucket::default(),
last: Self::BUCKET_SIZE - 1,
}
}
}
pub trait BucketQueueT {
fn update_last(&mut self, price_info: PriceInfo);
fn get_last(&self) -> PriceInfo;
fn calculate_average(&self) -> PriceInfo;
}
impl BucketQueueT for BucketQueue {
fn update_last(&mut self, price_info: PriceInfo) {
self.last = (self.last + 1) % Self::BUCKET_SIZE;
self.bucket[self.last as usize] = price_info;
}
fn get_last(&self) -> PriceInfo {
self.bucket[self.last as usize]
}
fn calculate_average(&self) -> PriceInfo {
let sum = self.bucket.iter().sum::<PriceInfo>();
PriceInfo {
avg_price: sum
.avg_price
.checked_div(&Price::from(Self::BUCKET_SIZE as u128))
.expect("avg_price is valid value; BUCKET_SIZE is non-zero integer; qed"),
volume: sum
.volume
.checked_div(Self::BUCKET_SIZE as u128)
.expect("avg_price is valid value; BUCKET_SIZE is non-zero integer; qed"),
}
}
}
impl Index<usize> for BucketQueue {
type Output = PriceInfo;
fn index(&self, index: usize) -> &Self::Output {
&self.bucket[index]
}
}
impl IndexMut<usize> for BucketQueue {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.bucket[index]
}
}