-
Notifications
You must be signed in to change notification settings - Fork 39
/
oracle.go
120 lines (100 loc) · 3.03 KB
/
oracle.go
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
package types
import (
"strings"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
)
const QuoteUSD = "USD"
const TwapWindow = int64(5 * 60) // 5 minute TWAP window
const BandPriceMultiplier uint64 = 1000000000 // 1e9
// MaxHistoricalPriceRecordAge is the maximum age of oracle price records to track.
const MaxHistoricalPriceRecordAge = 60 * 5
func GetOracleType(oracleTypeStr string) (OracleType, error) {
oracleTypeStr = strings.ToLower(oracleTypeStr)
var oracleType OracleType
switch oracleTypeStr {
case "band":
oracleType = OracleType_Band
case "bandibc":
oracleType = OracleType_BandIBC
case "pricefeed":
oracleType = OracleType_PriceFeed
case "coinbase":
oracleType = OracleType_Coinbase
case "provider":
oracleType = OracleType_Provider
case "pyth":
oracleType = OracleType_Pyth
default:
return OracleType_Band, errors.Wrapf(ErrUnsupportedOracleType, "%s", oracleTypeStr)
}
return oracleType, nil
}
func (o *OracleType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(OracleType_value, data, "OracleType")
if err != nil {
return err
}
*o = OracleType(value)
return nil
}
func (c *CoinbasePriceState) GetDecPrice() sdk.Dec {
// nolint:all
// price = price/10^6
return sdk.NewDec(int64(c.Value)).QuoTruncate(sdk.NewDec(10).Power(6))
}
func NewPriceState(price sdk.Dec, timestamp int64) *PriceState {
return &PriceState{
Price: price,
CumulativePrice: sdk.ZeroDec(),
Timestamp: timestamp,
}
}
func NewProviderPriceState(symbol string, price sdk.Dec, timestamp int64) *ProviderPriceState {
return &ProviderPriceState{
Symbol: symbol,
State: NewPriceState(price, timestamp),
}
}
func (p *PriceState) UpdatePrice(price sdk.Dec, timestamp int64) {
cumulativePriceDelta := sdk.NewDec(timestamp - p.Timestamp).Mul(p.Price)
p.CumulativePrice = p.CumulativePrice.Add(cumulativePriceDelta)
p.Timestamp = timestamp
p.Price = price
}
type SymbolPriceTimestamps []*SymbolPriceTimestamp
func (s SymbolPriceTimestamps) SetTimestamp(oracleType OracleType, symbol string, ts int64) SymbolPriceTimestamps {
for _, entry := range s {
if entry.SymbolId == symbol {
entry.Timestamp = ts
return s
}
}
s = append(s, &SymbolPriceTimestamp{
Oracle: oracleType,
SymbolId: symbol,
Timestamp: ts,
})
return s
}
func (s SymbolPriceTimestamps) GetTimestamp(oracleType OracleType, symbol string) (ts int64, ok bool) {
for i := range s {
if s[i].Oracle == oracleType && s[i].SymbolId == symbol {
return s[i].Timestamp, true
}
}
return -1, false
}
// CheckPriceFeedThreshold returns true if the newPrice has changed beyond 100x or less than 1% of the last price
func CheckPriceFeedThreshold(lastPrice, newPrice sdk.Dec) bool {
return newPrice.GT(lastPrice.Mul(sdk.NewDec(100))) || newPrice.LT(lastPrice.Quo(sdk.NewDec(100)))
}
func IsLegacySchemeOracleScript(scriptID int64, params BandIBCParams) bool {
for _, id := range params.LegacyOracleIds {
if id == scriptID {
return true
}
}
return false
}