-
Notifications
You must be signed in to change notification settings - Fork 226
/
coins.go
260 lines (212 loc) · 6.57 KB
/
coins.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package types
import (
"database/sql"
"database/sql/driver"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func ToString(value sql.NullString) string {
if value.Valid {
return value.String
}
return ""
}
func ToNullString(value string) sql.NullString {
value = strings.TrimSpace(value)
return sql.NullString{
Valid: value != "",
String: value,
}
}
func RemoveEmpty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
// _________________________________________________________
// DbCoin represents the information stored inside the database about a single coin
type DbCoin struct {
Denom string
Amount string
}
// NewDbCoin builds a DbCoin starting from an SDK Coin
func NewDbCoin(coin sdk.Coin) DbCoin {
return DbCoin{
Denom: coin.Denom,
Amount: coin.Amount.String(),
}
}
// Equal tells whether coin and d represent the same coin with the same amount
func (coin DbCoin) Equal(d DbCoin) bool {
return coin.Denom == d.Denom && coin.Amount == d.Amount
}
// Value implements driver.Valuer
func (coin *DbCoin) Value() (driver.Value, error) {
return fmt.Sprintf("(%s,%s)", coin.Denom, coin.Amount), nil
}
// Scan implements sql.Scanner
func (coin *DbCoin) Scan(src interface{}) error {
strValue := string(src.([]byte))
strValue = strings.ReplaceAll(strValue, `"`, "")
strValue = strings.ReplaceAll(strValue, "{", "")
strValue = strings.ReplaceAll(strValue, "}", "")
strValue = strings.ReplaceAll(strValue, "(", "")
strValue = strings.ReplaceAll(strValue, ")", "")
values := strings.Split(strValue, ",")
*coin = DbCoin{Denom: values[0], Amount: values[1]}
return nil
}
// ToCoin converts this DbCoin to sdk.Coin
func (coin DbCoin) ToCoin() sdk.Coin {
amount, _ := sdk.NewIntFromString(coin.Amount)
return sdk.NewCoin(coin.Denom, amount)
}
// _________________________________________________________
// DbCoins represents an array of coins
type DbCoins []*DbCoin
// NewDbCoins build a new DbCoins object starting from an array of coins
func NewDbCoins(coins sdk.Coins) DbCoins {
dbCoins := make([]*DbCoin, 0)
for _, coin := range coins {
dbCoins = append(dbCoins, &DbCoin{Amount: coin.Amount.String(), Denom: coin.Denom})
}
return dbCoins
}
// Equal tells whether c and d contain the same items in the same order
func (coins DbCoins) Equal(d *DbCoins) bool {
if d == nil {
return false
}
if len(coins) != len(*d) {
return false
}
for index, coin := range coins {
if !coin.Equal(*(*d)[index]) {
return false
}
}
return true
}
// Scan implements sql.Scanner
func (coins *DbCoins) Scan(src interface{}) error {
strValue := string(src.([]byte))
strValue = strings.ReplaceAll(strValue, `"`, "")
strValue = strings.ReplaceAll(strValue, "{", "")
strValue = strings.ReplaceAll(strValue, "}", "")
strValue = strings.ReplaceAll(strValue, "),(", ") (")
strValue = strings.ReplaceAll(strValue, "(", "")
strValue = strings.ReplaceAll(strValue, ")", "")
values := RemoveEmpty(strings.Split(strValue, " "))
coinsV := make(DbCoins, len(values))
for index, value := range values {
v := strings.Split(value, ",") // Split the values
coin := DbCoin{Denom: v[0], Amount: v[1]}
coinsV[index] = &coin
}
*coins = coinsV
return nil
}
// ToCoins converts this DbCoins to sdk.Coins
func (coins DbCoins) ToCoins() sdk.Coins {
var sdkCoins = make([]sdk.Coin, len(coins))
for index := range coins {
sdkCoins[index] = coins[index].ToCoin()
}
return sdkCoins
}
// --------------------------------------------------------------------------------------------------------------------
// DbDecCoin represents the information stored inside the database about a single coin
type DbDecCoin struct {
Denom string
Amount string
}
// NewDbDecCoin builds a DbDecCoin starting from an SDK Coin
func NewDbDecCoin(coin sdk.DecCoin) DbDecCoin {
return DbDecCoin{
Denom: coin.Denom,
Amount: coin.Amount.String(),
}
}
// Equal tells whether coin and d represent the same coin with the same amount
func (coin DbDecCoin) Equal(d DbDecCoin) bool {
return coin.Denom == d.Denom && coin.Amount == d.Amount
}
// Value implements driver.Valuer
func (coin *DbDecCoin) Value() (driver.Value, error) {
return fmt.Sprintf("(%s,%s)", coin.Denom, coin.Amount), nil
}
// Scan implements sql.Scanner
func (coin *DbDecCoin) Scan(src interface{}) error {
strValue := string(src.([]byte))
strValue = strings.ReplaceAll(strValue, `"`, "")
strValue = strings.ReplaceAll(strValue, "{", "")
strValue = strings.ReplaceAll(strValue, "}", "")
strValue = strings.ReplaceAll(strValue, "(", "")
strValue = strings.ReplaceAll(strValue, ")", "")
values := strings.Split(strValue, ",")
*coin = DbDecCoin{Denom: values[0], Amount: values[1]}
return nil
}
// ToDecCoin converts this DbDecCoin to sdk.DecCoin
func (coin DbDecCoin) ToDecCoin() sdk.DecCoin {
amount, _ := sdk.NewDecFromStr(coin.Amount)
return sdk.NewDecCoinFromDec(coin.Denom, amount)
}
// _________________________________________________________
// DbDecCoins represents an array of coins
type DbDecCoins []*DbDecCoin
// NewDbDecCoins build a new DbDecCoins object starting from an array of coins
func NewDbDecCoins(coins sdk.DecCoins) DbDecCoins {
DbDecCoins := make([]*DbDecCoin, 0)
for _, coin := range coins {
DbDecCoins = append(DbDecCoins, &DbDecCoin{Amount: coin.Amount.String(), Denom: coin.Denom})
}
return DbDecCoins
}
// Equal tells whether c and d contain the same items in the same order
func (coins DbDecCoins) Equal(d *DbDecCoins) bool {
if d == nil {
return false
}
if len(coins) != len(*d) {
return false
}
for index, coin := range coins {
if !coin.Equal(*(*d)[index]) {
return false
}
}
return true
}
// Scan implements sql.Scanner
func (coins *DbDecCoins) Scan(src interface{}) error {
strValue := string(src.([]byte))
strValue = strings.ReplaceAll(strValue, `"`, "")
strValue = strings.ReplaceAll(strValue, "{", "")
strValue = strings.ReplaceAll(strValue, "}", "")
strValue = strings.ReplaceAll(strValue, "),(", ") (")
strValue = strings.ReplaceAll(strValue, "(", "")
strValue = strings.ReplaceAll(strValue, ")", "")
values := RemoveEmpty(strings.Split(strValue, " "))
coinsV := make(DbDecCoins, len(values))
for index, value := range values {
v := strings.Split(value, ",") // Split the values
coin := DbDecCoin{Denom: v[0], Amount: v[1]}
coinsV[index] = &coin
}
*coins = coinsV
return nil
}
// ToDecCoins converts this DbDecCoins to sdk.DecCoins
func (coins DbDecCoins) ToDecCoins() sdk.DecCoins {
var sdkCoins = make([]sdk.DecCoin, len(coins))
for index := range coins {
sdkCoins[index] = coins[index].ToDecCoin()
}
return sdkCoins
}