-
Notifications
You must be signed in to change notification settings - Fork 169
/
vote.go
108 lines (90 loc) · 2.61 KB
/
vote.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
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"gopkg.in/yaml.v3"
"github.com/umee-network/umee/v4/x/leverage/types"
)
func NewAggregateExchangeRatePrevote(
hash AggregateVoteHash,
voter sdk.ValAddress,
submitBlock uint64,
) AggregateExchangeRatePrevote {
return AggregateExchangeRatePrevote{
Hash: hash.String(), // we could store bytes here!
Voter: voter.String(), // we could store bytes here!
SubmitBlock: submitBlock,
}
}
// String implement stringify
func (v AggregateExchangeRatePrevote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
func NewAggregateExchangeRateVote(
exchangeRateTuples ExchangeRateTuples,
voter sdk.ValAddress,
) AggregateExchangeRateVote {
return AggregateExchangeRateVote{
ExchangeRateTuples: exchangeRateTuples,
Voter: voter.String(), // we could use bytes here!
}
}
// String implement stringify
func (v AggregateExchangeRateVote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// NewExchangeRateTuple creates a ExchangeRateTuple instance
func NewExchangeRateTuple(denom string, exchangeRate sdk.Dec) ExchangeRateTuple {
return ExchangeRateTuple{
denom,
exchangeRate,
}
}
// String implement stringify
func (v ExchangeRateTuple) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// ExchangeRateTuples - array of ExchangeRateTuple
type ExchangeRateTuples []ExchangeRateTuple
// String implements fmt.Stringer interface
func (tuples ExchangeRateTuples) String() string {
out, _ := yaml.Marshal(tuples)
return string(out)
}
// ParseExchangeRateTuples ExchangeRateTuple parser
func ParseExchangeRateTuples(tuplesStr string) (ExchangeRateTuples, error) {
if len(tuplesStr) == 0 {
return nil, nil
}
tupleStrs := strings.Split(tuplesStr, ",")
tuples := make(ExchangeRateTuples, len(tupleStrs))
duplicateCheckMap := make(map[string]struct{})
for i, tupleStr := range tupleStrs {
denomAmountStr := strings.Split(tupleStr, ":")
if len(denomAmountStr) != 2 {
return nil, fmt.Errorf("invalid exchange rate %s", tupleStr)
}
decCoin, err := sdk.NewDecFromStr(denomAmountStr[1])
if err != nil {
return nil, err
}
if !decCoin.IsPositive() {
return nil, types.ErrInvalidOraclePrice
}
denom := strings.ToUpper(denomAmountStr[0])
tuples[i] = ExchangeRateTuple{
Denom: denom,
ExchangeRate: decCoin,
}
if _, ok := duplicateCheckMap[denom]; ok {
return nil, sdkerrors.ErrInvalidCoins.Wrapf("duplicated denom %s", denom)
}
duplicateCheckMap[denom] = struct{}{}
}
return tuples, nil
}