/
token.go
240 lines (182 loc) · 6.63 KB
/
token.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
package keeper
import (
"fmt"
gogotypes "github.com/gogo/protobuf/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
tokentypes "github.com/bitsongofficial/chainmodules/x/fantoken/types"
)
// GetTokens returns all existing tokens
func (k Keeper) GetFanTokens(ctx sdk.Context, owner sdk.AccAddress) (tokens []tokentypes.FanTokenI) {
store := ctx.KVStore(k.storeKey)
var it sdk.Iterator
if owner == nil {
it = sdk.KVStorePrefixIterator(store, tokentypes.PrefixFanTokenForSymbol)
defer it.Close()
for ; it.Valid(); it.Next() {
var token tokentypes.FanToken
k.cdc.MustUnmarshalBinaryBare(it.Value(), &token)
tokens = append(tokens, &token)
}
return
}
it = sdk.KVStorePrefixIterator(store, tokentypes.KeyFanTokens(owner, ""))
defer it.Close()
for ; it.Valid(); it.Next() {
var symbol gogotypes.StringValue
k.cdc.MustUnmarshalBinaryBare(it.Value(), &symbol)
token, err := k.getFanTokenBySymbol(ctx, symbol.Value)
if err != nil {
continue
}
tokens = append(tokens, token)
}
return
}
// GetToken returns the token of the specified symbol or min uint
func (k Keeper) GetFanToken(ctx sdk.Context, denom string) (tokentypes.FanTokenI, error) {
// query token by symbol
if token, err := k.getFanTokenBySymbol(ctx, denom); err == nil {
return &token, nil
}
// query token by min unit
if token, err := k.getFanTokenByDenom(ctx, denom); err == nil {
return &token, nil
}
return nil, sdkerrors.Wrapf(tokentypes.ErrTokenNotExists, "token: %s does not exist", denom)
}
// AddToken saves a new token
func (k Keeper) AddFanToken(ctx sdk.Context, token tokentypes.FanToken) error {
if k.HasFanToken(ctx, token.GetSymbol()) {
return sdkerrors.Wrapf(tokentypes.ErrSymbolAlreadyExists, "symbol already exists: %s", token.GetSymbol())
}
if k.HasFanToken(ctx, token.GetDenom()) {
return sdkerrors.Wrapf(tokentypes.ErrDenomAlreadyExists, "denom already exists: %s", token.GetDenom())
}
// set token
k.setFanToken(ctx, token)
// set token to be prefixed with denom
k.setWithDenom(ctx, token.GetDenom(), token.GetSymbol())
if len(token.Owner) != 0 {
// set token to be prefixed with owner
k.setWithOwner(ctx, token.GetOwner(), token.GetSymbol())
}
k.bankKeeper.SetDenomMetaData(ctx, token.MetaData)
return nil
}
// HasSymbol asserts a token exists by symbol
func (k Keeper) HasSymbol(ctx sdk.Context, symbol string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(tokentypes.KeySymbol(symbol))
}
// HasToken asserts a token exists
func (k Keeper) HasFanToken(ctx sdk.Context, denom string) bool {
store := ctx.KVStore(k.storeKey)
if k.HasSymbol(ctx, denom) {
return true
}
return store.Has(tokentypes.KeyDenom(denom))
}
// GetOwner returns the owner of the specified token
func (k Keeper) GetOwner(ctx sdk.Context, denom string) (sdk.AccAddress, error) {
token, err := k.GetFanToken(ctx, denom)
if err != nil {
return nil, err
}
return token.GetOwner(), nil
}
// AddBurnCoin saves the total amount of the burned tokens
func (k Keeper) AddBurnCoin(ctx sdk.Context, coin sdk.Coin) {
var total = coin
if hasCoin, err := k.GetBurnCoin(ctx, coin.Denom); err == nil {
total = total.Add(hasCoin)
}
bz := k.cdc.MustMarshalBinaryBare(&total)
key := tokentypes.KeyBurnFanTokenAmt(coin.Denom)
store := ctx.KVStore(k.storeKey)
store.Set(key, bz)
}
// GetBurnCoin returns the total amount of the burned tokens
func (k Keeper) GetBurnCoin(ctx sdk.Context, denom string) (sdk.Coin, error) {
key := tokentypes.KeyBurnFanTokenAmt(denom)
store := ctx.KVStore(k.storeKey)
bz := store.Get(key)
if len(bz) == 0 {
return sdk.Coin{}, sdkerrors.Wrapf(tokentypes.ErrNotFoundTokenAmt, "not found symbol: %s", denom)
}
var coin sdk.Coin
k.cdc.MustUnmarshalBinaryBare(bz, &coin)
return coin, nil
}
// GetAllBurnCoin returns the total amount of all the burned tokens
func (k Keeper) GetAllBurnCoin(ctx sdk.Context) []sdk.Coin {
store := ctx.KVStore(k.storeKey)
var coins []sdk.Coin
it := sdk.KVStorePrefixIterator(store, tokentypes.PefixBurnFanTokenAmt)
for ; it.Valid(); it.Next() {
var coin sdk.Coin
k.cdc.MustUnmarshalBinaryBare(it.Value(), &coin)
coins = append(coins, coin)
}
return coins
}
// GetParamSet returns token params from the global param store
func (k Keeper) GetParamSet(ctx sdk.Context) tokentypes.Params {
var p tokentypes.Params
k.paramSpace.GetParamSet(ctx, &p)
return p
}
// SetParamSet sets token params to the global param store
func (k Keeper) SetParamSet(ctx sdk.Context, params tokentypes.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
func (k Keeper) setWithOwner(ctx sdk.Context, owner sdk.AccAddress, symbol string) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: symbol})
store.Set(tokentypes.KeyFanTokens(owner, symbol), bz)
}
func (k Keeper) setWithDenom(ctx sdk.Context, denom, symbol string) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: symbol})
store.Set(tokentypes.KeyDenom(denom), bz)
}
func (k Keeper) setFanToken(ctx sdk.Context, token tokentypes.FanToken) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&token)
store.Set(tokentypes.KeySymbol(token.GetSymbol()), bz)
}
func (k Keeper) getFanTokenBySymbol(ctx sdk.Context, symbol string) (token tokentypes.FanToken, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(tokentypes.KeySymbol(symbol))
if bz == nil {
return token, sdkerrors.Wrap(tokentypes.ErrTokenNotExists, fmt.Sprintf("token symbol %s does not exist", symbol))
}
k.cdc.MustUnmarshalBinaryBare(bz, &token)
return token, nil
}
func (k Keeper) getFanTokenByDenom(ctx sdk.Context, denom string) (token tokentypes.FanToken, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(tokentypes.KeyDenom(denom))
if bz == nil {
return token, sdkerrors.Wrap(tokentypes.ErrTokenNotExists, fmt.Sprintf("token denom %s does not exist", denom))
}
var symbol gogotypes.StringValue
k.cdc.MustUnmarshalBinaryBare(bz, &symbol)
token, err = k.getFanTokenBySymbol(ctx, symbol.Value)
if err != nil {
return token, err
}
return token, nil
}
// reset all indices by the new owner for token query
func (k Keeper) resetStoreKeyForQueryToken(ctx sdk.Context, symbol string, srcOwner, dstOwner sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
// delete the old key
store.Delete(tokentypes.KeyFanTokens(srcOwner, symbol))
// add the new key
k.setWithOwner(ctx, dstOwner, symbol)
}
// getTokenSupply queries the token supply from the total supply
func (k Keeper) getFanTokenSupply(ctx sdk.Context, denom string) sdk.Int {
return k.bankKeeper.GetSupply(ctx).GetTotal().AmountOf(denom)
}