/
utils.go
67 lines (58 loc) · 1.71 KB
/
utils.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
package types
import (
"sort"
"time"
"github.com/cometbft/cometbft/crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
// MustParseRFC3339 parses string time to time in RFC3339 format.
// This is used only for internal testing purpose.
func MustParseRFC3339(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}
// DeriveAddress derives an address with the given address length type, module name, and
// address derivation name. It is used to derive reserve account addresses for selling, paying, and vesting.
func DeriveAddress(addressType AddressType, moduleName, name string) sdk.AccAddress {
switch addressType {
case AddressType32Bytes:
return sdk.AccAddress(address.Module(moduleName, []byte(name)))
case AddressType20Bytes:
return sdk.AccAddress(crypto.AddressHash([]byte(moduleName + name)))
default:
return sdk.AccAddress{}
}
}
// SortBids sorts bid array by bid price in descending order.
func SortBids(bids []Bid) []Bid {
sort.Slice(bids, func(i, j int) bool {
if bids[i].Price.GT(bids[j].Price) {
return true
}
return bids[i].Id < bids[j].Id
})
return bids
}
func BidsByPrice(bids []Bid) (prices []sdk.Dec, bidsByPrice map[string][]Bid) {
bids = SortBids(bids)
bidsByPrice = map[string][]Bid{} // price => []Bid
for _, bid := range bids {
priceStr := bid.Price.String()
bidsByPrice[priceStr] = append(bidsByPrice[priceStr], bid)
}
// Sort prices in descending order.
prices = make([]sdk.Dec, len(bidsByPrice))
i := 0
for priceStr := range bidsByPrice {
prices[i] = sdk.MustNewDecFromStr(priceStr)
i++
}
sort.Slice(prices, func(i, j int) bool {
return prices[i].GT(prices[j])
})
return
}