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
// This file is part of pallet-ema-oracle.

// Copyright (C) 2022-2023  Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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;
/// A price is a tuple of two `u128`s representing the numerator and denominator of a rational number.
pub type Price = EmaPrice;

/// A type representing data produced by a trade or liquidity event. Timestamped to the block where
/// it was created.
#[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>,
{
    /// Convert the `OracleEntry` into an `AggregatedEntry` for consumption. Determines the age by
    /// subtracting `initialized` from the timestamp.
    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),
        }
    }

    /// Return the raw data of the entry as a tuple of tuples, excluding the timestamp.
    pub fn raw_data(&self) -> (Price, (Balance, Balance, Balance, Balance), (Balance, Balance)) {
        (self.price, self.volume.clone().into(), self.liquidity.into())
    }

    /// Return an inverted version of the entry where the meaning of assets a and b are inverted.
    /// So the price of a/b become the price b/a and the volume switches correspondingly.
    pub fn inverted(self) -> Self {
        // It makes sense for the reciprocal of zero to be zero here.
        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,
        }
    }

    /// Update the volume in `self` by adding in the volume of `incoming` and taking over the other
    /// values from `incoming`.
    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;
    }

    /// Fast forward the oracle value to `new_timestamp`. Updates the timestamp and resets the volume.
    pub fn fast_forward_to(&mut self, new_timestamp: BlockNumber) {
        self.timestamp = new_timestamp;
        self.volume = Volume::default();
    }

    /// Determine a new entry based on `self` and a previous entry. Adds the volumes together and
    /// takes the values of `self` for the rest.
    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,
        }
    }

    /// Determine the next oracle entry based on a previous (`self`) and an `incoming` entry as well as
    /// a `period`.
    ///
    /// Returns `None` if any of the calculations fail (including the `incoming` entry not being
    /// one iteration (block) more recent than `self`).
    ///
    /// The period is used to determine the smoothing factor alpha for an exponential moving average.
    pub fn calculate_new_by_integrating_incoming(&self, period: OraclePeriod, incoming: &Self) -> Option<Self> {
        // incoming should be one step ahead of the previous value
        if !incoming.timestamp.checked_sub(&self.timestamp)?.is_one() {
            return None;
        }
        if period == OraclePeriod::LastBlock {
            return Some(incoming.clone());
        }
        // determine smoothing factor
        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,
        })
    }

    /// Update `self` based on a previous (`self`) and an `incoming` oracle entry as well as  a `period`.
    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)
    }

    /// Determine the current intended oracle entry based on a previous (`self`) and an `update_with` entry as well as
    /// a `period`.
    ///
    /// Returns `None` if any of the calculations fail (including the `update_with` entry not being
    /// more recent than `self`).
    ///
    /// The period is used to determine the smoothing factor alpha for an exponential moving average.
    ///
    /// Uses the difference between the `timestamp`s to determine the time (i.e. iterations) to cover.
    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());
        }
        // determine smoothing factor
        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,
        })
    }

    /// Update `self` based on a previous (`self`) and an `update_with` entry as well as a `period`.
    /// See [`calculate_current_from_outdated`].
    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)
    }
}

/// Convert a given `period` into the smoothing factor used in the weighted average.
/// See [`check_period_smoothing_factors`] for how the values are generated.
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,
        }
    }
}