/
vote.go
106 lines (86 loc) · 2.71 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
package types
import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewAggregateExchangeRatePrevote creates an AggregateExchangeRatePrevote instance.
func NewAggregateExchangeRatePrevote(hash AggregateVoteHash, voter sdk.ValAddress, submitBlock uint64) AggregateExchangeRatePrevote {
return AggregateExchangeRatePrevote{
Hash: hash.String(),
Voter: voter.String(),
SubmitBlock: submitBlock,
}
}
// String implement fmt.Stringer.
func (v AggregateExchangeRatePrevote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// NewAggregateExchangeRateVote creates an AggregateExchangeRateVote instance.
func NewAggregateExchangeRateVote(exchangeRateTuples ExchangeRateTuples, voter sdk.ValAddress) AggregateExchangeRateVote {
return AggregateExchangeRateVote{
ExchangeRateTuples: exchangeRateTuples,
Voter: voter.String(),
}
}
// String implement fmt.Stringer.
func (v AggregateExchangeRateVote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// NewExchangeRateTuple creates an ExchangeRateTuple instance.
func NewExchangeRateTuple(denom string, exchangeRate sdk.Dec) ExchangeRateTuple {
return ExchangeRateTuple{
denom,
exchangeRate,
}
}
// String implement fmt.Stringer.
func (v ExchangeRateTuple) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// ExchangeRateTuples defines array of ExchangeRateTuple.
type ExchangeRateTuples []ExchangeRateTuple
// String implements fmt.Stringer.
func (tuples ExchangeRateTuples) String() string {
out, _ := yaml.Marshal(tuples)
return string(out)
}
// ParseExchangeRateTuples parses ExchangeRateTuple from a string.
func ParseExchangeRateTuples(tuplesStr string) (ExchangeRateTuples, error) {
tuplesStr = strings.TrimSpace(tuplesStr)
if len(tuplesStr) == 0 {
return nil, nil
}
tupleStrs := strings.Split(tuplesStr, ",")
tuples := make(ExchangeRateTuples, len(tupleStrs))
duplicateCheckMap := make(map[string]bool)
for i, tupleStr := range tupleStrs {
splits := strings.Split(strings.TrimSpace(tupleStr), ":")
if len(splits) != 2 {
return nil, fmt.Errorf("invalid exchange rate notation: %s", tupleStr)
}
denom := strings.TrimSpace(splits[0])
err := sdk.ValidateDenom(denom)
if err != nil {
return nil, err
}
amountStr := strings.TrimSpace(splits[1])
amount, err := sdk.NewDecFromStr(splits[1])
if err != nil {
return nil, fmt.Errorf("failed to parse decimal amount '%s': %w", amountStr, err)
}
tuples[i] = ExchangeRateTuple{
Denom: denom,
ExchangeRate: amount,
}
if _, ok := duplicateCheckMap[denom]; ok {
return nil, fmt.Errorf("duplicated denom %s", denom)
}
duplicateCheckMap[denom] = true
}
return tuples, nil
}