/
balance.go
172 lines (137 loc) · 4.69 KB
/
balance.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
package keeper
import (
"bytes"
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furynet/furymod/modules/mt/types"
)
// AddBalance adds amounts to an account
func (k Keeper) AddBalance(ctx sdk.Context,
denomID, mtID string,
amount uint64,
addr sdk.AccAddress) error {
balance := k.GetBalance(ctx, denomID, mtID, addr)
if math.MaxUint64-balance < amount {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "overflow: max %d, got %d", math.MaxUint64-balance, amount)
}
balance += amount
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalAmount(k.cdc, balance)
store.Set(types.KeyBalance(addr, denomID, mtID), bz)
return nil
}
// SubBalance subs amounts from an account
func (k Keeper) SubBalance(ctx sdk.Context,
denomID, mtID string,
amount uint64,
addr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
balance := k.GetBalance(ctx, denomID, mtID, addr)
balance -= amount
bz := types.MustMarshalAmount(k.cdc, balance)
store.Set(types.KeyBalance(addr, denomID, mtID), bz)
}
// GetBalance gets balance of an MT
func (k Keeper) GetBalance(ctx sdk.Context,
denomID, mtID string,
addr sdk.AccAddress) uint64 {
store := ctx.KVStore(k.storeKey)
amount := store.Get(types.KeyBalance(addr, denomID, mtID))
if len(amount) == 0 {
return 0
}
return types.MustUnMarshalAmount(k.cdc, amount)
}
// getBalances gets balances of all accounts, should only be used in exporting genesis states
func (k Keeper) getBalances(ctx sdk.Context) []types.Owner {
store := ctx.KVStore(k.storeKey)
it := sdk.KVStorePrefixIterator(store, types.PrefixBalance)
defer it.Close()
var ownerMap map[string]map[string]map[string]uint64
ownerMap = make(map[string]map[string]map[string]uint64)
for ; it.Valid(); it.Next() {
keys := bytes.Split(it.Key(), types.Delimiter)
address := string(keys[1])
denomID := string(keys[2])
mtID := string(keys[3])
amount := types.MustUnMarshalAmount(k.cdc, it.Value())
if _, ok := ownerMap[address]; !ok {
ownerMap[address] = make(map[string]map[string]uint64)
}
if _, ok := ownerMap[address][denomID]; !ok {
ownerMap[address][denomID] = make(map[string]uint64)
}
ownerMap[address][denomID][mtID] = amount
}
var owners []types.Owner
for addr, denomMap := range ownerMap {
var denomBalances []types.DenomBalance
for denomID, mtMap := range denomMap {
var balances []types.Balance
for mtID, amount := range mtMap {
balance := types.NewBalance(mtID, amount)
balances = append(balances, balance)
}
denomBalance := types.NewDenomBalance(denomID, balances)
denomBalances = append(denomBalances, denomBalance)
}
owner := types.NewOwner(addr, denomBalances)
owners = append(owners, owner)
}
return owners
}
// Transfer transfers mts
func (k Keeper) Transfer(ctx sdk.Context,
denomID, mtID string,
amount uint64,
from, to sdk.AccAddress) error {
k.SubBalance(ctx, denomID, mtID, amount, from)
return k.AddBalance(ctx, denomID, mtID, amount, to)
}
// GetDenomSupply returns the number of Mts by the specified denom ID
func (k Keeper) GetDenomSupply(ctx sdk.Context, denomID string) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeySupply(denomID, ""))
if len(bz) == 0 {
return 0
}
return types.MustUnMarshalSupply(k.cdc, bz)
}
// GetMTSupply returns the supply of a specified MT
func (k Keeper) GetMTSupply(ctx sdk.Context, denomID, mtID string) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeySupply(denomID, mtID))
if len(bz) == 0 {
return 0
}
return types.MustUnMarshalSupply(k.cdc, bz)
}
// IncreaseDenomSupply increase total supply (count of MTs) of a denom
func (k Keeper) IncreaseDenomSupply(ctx sdk.Context, denomID string) {
supply := k.GetDenomSupply(ctx, denomID)
supply++
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalSupply(k.cdc, supply)
store.Set(types.KeySupply(denomID, ""), bz)
}
// IncreaseMTSupply increase total supply of an MT
func (k Keeper) IncreaseMTSupply(ctx sdk.Context, denomID, mtID string, amount uint64) error {
supply := k.GetMTSupply(ctx, denomID, mtID)
if math.MaxUint64-supply < amount {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "overflow: max %d, got %d", math.MaxUint64-supply, amount)
}
supply += amount
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalSupply(k.cdc, supply)
store.Set(types.KeySupply(denomID, mtID), bz)
return nil
}
// decreaseMTSupply decrease total supply of an MT
func (k Keeper) decreaseMTSupply(ctx sdk.Context, denomID, mtID string, amount uint64) {
supply := k.GetMTSupply(ctx, denomID, mtID)
supply -= amount
store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalSupply(k.cdc, supply)
store.Set(types.KeySupply(denomID, mtID), bz)
}