-
Notifications
You must be signed in to change notification settings - Fork 168
/
price.go
61 lines (53 loc) · 1.17 KB
/
price.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
package types
import (
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Prices []Price
func NewPrice(exchangeRate sdk.Dec, denom string, blockNum uint64) *Price {
return &Price{
ExchangeRateTuple: ExchangeRateTuple{
ExchangeRate: exchangeRate,
Denom: denom,
},
BlockNum: blockNum,
}
}
func (p *Prices) Decs() []sdk.Dec {
decs := []sdk.Dec{}
for _, price := range *p {
decs = append(decs, price.ExchangeRateTuple.ExchangeRate)
}
return decs
}
func (p *Prices) FilterByBlock(blockNum uint64) *Prices {
prices := Prices{}
for _, price := range *p {
if price.BlockNum == blockNum {
prices = append(prices, price)
}
}
return &prices
}
func (p *Prices) FilterByDenom(denom string) *Prices {
prices := Prices{}
for _, price := range *p {
if price.ExchangeRateTuple.Denom == denom {
prices = append(prices, price)
}
}
return &prices
}
func (p *Prices) Sort() *Prices {
prices := *p
sort.Slice(
prices,
func(i, j int) bool {
if prices[i].BlockNum == prices[j].BlockNum {
return prices[i].ExchangeRateTuple.Denom < prices[j].ExchangeRateTuple.Denom
}
return prices[i].BlockNum < prices[j].BlockNum
},
)
return &prices
}