-
Notifications
You must be signed in to change notification settings - Fork 39
/
pyth.go
75 lines (63 loc) · 1.54 KB
/
pyth.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)
func NewPythPriceState(
priceID common.Hash,
emaPrice, emaConf, conf sdk.Dec,
publishTime int64,
price sdk.Dec,
blockTime int64,
) *PythPriceState {
return &PythPriceState{
PriceId: priceID.Hex(),
EmaPrice: emaPrice,
EmaConf: emaConf,
Conf: conf,
PublishTime: uint64(publishTime),
PriceState: *NewPriceState(price, blockTime),
}
}
func (p *PythPriceState) Update(
emaPrice, emaConf, conf sdk.Dec,
publishTime uint64,
price sdk.Dec,
blockTime int64,
) {
p.EmaPrice = emaPrice
p.EmaConf = emaConf
p.Conf = conf
p.PublishTime = publishTime
p.PriceState.UpdatePrice(price, blockTime)
}
func (p *PriceAttestation) GetPriceID() string {
return p.PriceId
}
func (p *PriceAttestation) GetPriceIDHash() common.Hash {
return common.HexToHash(p.PriceId)
}
func (p *PriceAttestation) Validate() error {
if len(p.GetPriceIDHash().Bytes()) != 32 {
return ErrInvalidPythPriceID
}
if !(p.Price > 0) {
return ErrBadPrice
}
if p.Expo > MaxPythExponent || p.Expo < MinPythExponent {
return ErrInvalidPythExponent
}
if !(p.PublishTime > 0) {
return ErrInvalidPythPublishTime
}
// validating EMA price/conf is omitted since it's not mission critical and we don't want to block normal price updates
return nil
}
func GetExponentiatedDec(value, expo int64) sdk.Dec {
// price * 10^-expo
if expo <= 0 {
return sdk.NewDecWithPrec(value, -expo)
}
// price * 10^expo
return sdk.NewDec(value).Power(uint64(expo))
}