-
Notifications
You must be signed in to change notification settings - Fork 200
/
vote.go
181 lines (147 loc) · 4.96 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package types
import (
"fmt"
"strings"
"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/set"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
ExchangeRateTuplesSeparator = "|"
ExchangeRateTupleStringPrefix = '('
ExchangeRateTupleStringSuffix = ')'
ExchangeRateTuplePairRateSeparator = ","
)
// NewAggregateExchangeRatePrevote returns AggregateExchangeRatePrevote object
func NewAggregateExchangeRatePrevote(hash AggregateVoteHash, voter sdk.ValAddress, submitBlock uint64) AggregateExchangeRatePrevote {
return AggregateExchangeRatePrevote{
Hash: hash.String(),
Voter: voter.String(),
SubmitBlock: submitBlock,
}
}
// String implement stringify
func (v AggregateExchangeRatePrevote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// NewAggregateExchangeRateVote creates a AggregateExchangeRateVote instance
func NewAggregateExchangeRateVote(exchangeRateTuples ExchangeRateTuples, voter sdk.ValAddress) AggregateExchangeRateVote {
return AggregateExchangeRateVote{
ExchangeRateTuples: exchangeRateTuples,
Voter: voter.String(),
}
}
// String implement stringify
func (v AggregateExchangeRateVote) String() string {
out, _ := yaml.Marshal(v)
return string(out)
}
// NewExchangeRateTuple creates a ExchangeRateTuple instance
func NewExchangeRateTuple(pair asset.Pair, exchangeRate sdk.Dec) ExchangeRateTuple {
return ExchangeRateTuple{
pair,
exchangeRate,
}
}
// String implement stringify
func (m ExchangeRateTuple) String() string {
out, _ := yaml.Marshal(m)
return string(out)
}
// ToString converts the ExchangeRateTuple to the vote string.
func (m ExchangeRateTuple) ToString() (string, error) {
err := m.Pair.Validate()
if err != nil {
return "", err
}
return fmt.Sprintf(
"%c%s%s%s%c",
ExchangeRateTupleStringPrefix,
m.Pair,
ExchangeRateTuplePairRateSeparator,
m.ExchangeRate.String(),
ExchangeRateTupleStringSuffix,
), nil
}
// NewExchangeRateTupleFromString populates ExchangeRateTuple from a string, fails if the string is of invalid format.
func NewExchangeRateTupleFromString(s string) (ExchangeRateTuple, error) {
// strip parentheses
if len(s) <= 2 {
return ExchangeRateTuple{}, fmt.Errorf("invalid string length: %v", len(s))
}
if s[0] != ExchangeRateTupleStringPrefix || s[len(s)-1] != ExchangeRateTupleStringSuffix {
return ExchangeRateTuple{}, fmt.Errorf("invalid ExchangeRateTuple delimiters, start is expected with '(', end with ')', got: %s", s)
}
stripParentheses := s[1 : len(s)-1]
split := strings.Split(stripParentheses, ExchangeRateTuplePairRateSeparator)
if len(split) != 2 {
return ExchangeRateTuple{}, fmt.Errorf("invalid ExchangeRateTuple format")
}
pair, err := asset.TryNewPair(split[0])
if err != nil {
return ExchangeRateTuple{}, fmt.Errorf("invalid pair definition %s: %w", split[0], err)
}
dec, err := sdk.NewDecFromStr(split[1])
if err != nil {
return ExchangeRateTuple{}, fmt.Errorf("invalid decimal %s: %w", split[1], err)
}
return ExchangeRateTuple{
Pair: pair,
ExchangeRate: dec,
}, nil
}
// ExchangeRateTuples - array of ExchangeRateTuple
type ExchangeRateTuples []ExchangeRateTuple
func (tuples ExchangeRateTuples) ToMap() (exchangeRateMap map[asset.Pair]sdk.Dec) {
exchangeRateMap = make(map[asset.Pair]sdk.Dec)
for _, tuple := range tuples {
exchangeRateMap[tuple.Pair] = tuple.ExchangeRate
}
return exchangeRateMap
}
// String implements fmt.Stringer interface
func (tuples ExchangeRateTuples) String() string {
out, _ := yaml.Marshal(tuples)
return string(out)
}
func NewExchangeRateTuplesFromString(s string) (ExchangeRateTuples, error) {
stringTuples := strings.Split(s, ExchangeRateTuplesSeparator)
tuples := make(ExchangeRateTuples, len(stringTuples))
duplicates := make(set.Set[asset.Pair], len(stringTuples))
for i, stringTuple := range stringTuples {
exchangeRate, err := NewExchangeRateTupleFromString(stringTuple)
if err != nil {
return []ExchangeRateTuple{}, fmt.Errorf("invalid ExchangeRateTuple at index %d: %w", i, err)
}
// check duplicates
if _, ok := duplicates[exchangeRate.Pair]; ok {
return []ExchangeRateTuple{}, fmt.Errorf("found duplicate at index %d: %s", i, exchangeRate.Pair)
} else {
duplicates[exchangeRate.Pair] = struct{}{}
}
// insert exchange rate into the tuple
tuples[i] = exchangeRate
}
return tuples, nil
}
func (tuples ExchangeRateTuples) ToString() (string, error) {
tuplesStringSlice := make([]string, len(tuples))
for i, r := range tuples {
rStr, err := r.ToString()
if err != nil {
return "", fmt.Errorf("invalid ExchangeRateTuple at index %d: %w", i, err)
}
tuplesStringSlice[i] = rStr
}
return strings.Join(tuplesStringSlice, "|"), nil
}
// ParseExchangeRateTuples ExchangeRateTuple parser
func ParseExchangeRateTuples(tuplesStr string) (ExchangeRateTuples, error) {
tuples, err := NewExchangeRateTuplesFromString(tuplesStr)
if err != nil {
return nil, err
}
return tuples, nil
}