-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
241 lines (196 loc) · 5.92 KB
/
keeper.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
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/irisnet/irismod/modules/token/types"
)
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.Marshaler
bankKeeper types.BankKeeper
paramSpace paramstypes.Subspace
blockedAddrs map[string]bool
feeCollectorName string
}
func NewKeeper(
cdc codec.Marshaler,
key sdk.StoreKey,
paramSpace paramstypes.Subspace,
bankKeeper types.BankKeeper,
blockedAddrs map[string]bool,
feeCollectorName string,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
bankKeeper: bankKeeper,
feeCollectorName: feeCollectorName,
blockedAddrs: blockedAddrs,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("irismod/%s", types.ModuleName))
}
// IssueToken issues a new token
func (k Keeper) IssueToken(
ctx sdk.Context,
symbol string,
name string,
minUnit string,
scale uint32,
initialSupply uint64,
maxSupply uint64,
mintable bool,
owner sdk.AccAddress,
) error {
token := types.NewToken(
symbol, name, minUnit, scale, initialSupply,
maxSupply, mintable, owner,
)
if err := k.AddToken(ctx, token); err != nil {
return err
}
precision := sdk.NewIntWithDecimal(1, int(token.Scale))
initialCoin := sdk.NewCoin(
token.MinUnit,
sdk.NewIntFromUint64(token.InitialSupply).Mul(precision),
)
mintCoins := sdk.NewCoins(initialCoin)
// mint coins into module account
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, mintCoins); err != nil {
return err
}
// sent coins to owner's account
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, owner, mintCoins)
}
// EditToken edits the specified token
func (k Keeper) EditToken(
ctx sdk.Context,
symbol string,
name string,
maxSupply uint64,
mintable types.Bool,
owner sdk.AccAddress,
) error {
// get the destination token
token, err := k.getTokenBySymbol(ctx, symbol)
if err != nil {
return err
}
if owner.String() != token.Owner {
return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol)
}
if maxSupply > 0 {
issuedAmt := k.getTokenSupply(ctx, token.MinUnit)
issuedMainUnitAmt := uint64(issuedAmt.Quo(sdk.NewIntWithDecimal(1, int(token.Scale))).Int64())
if maxSupply < issuedMainUnitAmt {
return sdkerrors.Wrapf(types.ErrInvalidMaxSupply, "max supply must not be less than %d", issuedMainUnitAmt)
}
token.MaxSupply = maxSupply
}
if name != types.DoNotModify {
token.Name = name
metadata := k.bankKeeper.GetDenomMetaData(ctx, token.MinUnit)
metadata.Description = name
k.bankKeeper.SetDenomMetaData(ctx, metadata)
}
if mintable != types.Nil {
token.Mintable = mintable.ToBool()
}
k.setToken(ctx, token)
return nil
}
// TransferTokenOwner transfers the owner of the specified token to a new one
func (k Keeper) TransferTokenOwner(
ctx sdk.Context,
symbol string,
srcOwner sdk.AccAddress,
dstOwner sdk.AccAddress,
) error {
token, err := k.getTokenBySymbol(ctx, symbol)
if err != nil {
return err
}
if srcOwner.String() != token.Owner {
return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", srcOwner, symbol)
}
token.Owner = dstOwner.String()
// update token
k.setToken(ctx, token)
// reset all indices
k.resetStoreKeyForQueryToken(ctx, token.Symbol, srcOwner, dstOwner)
return nil
}
// MintToken mints the specified amount of token to the specified recipient
// NOTE: empty owner means that the external caller is responsible to manage the token authority
func (k Keeper) MintToken(
ctx sdk.Context,
symbol string,
amount uint64,
recipient sdk.AccAddress,
owner sdk.AccAddress,
) error {
token, err := k.getTokenBySymbol(ctx, symbol)
if err != nil {
return err
}
if owner.String() != token.Owner {
return sdkerrors.Wrapf(types.ErrInvalidOwner, "the address %s is not the owner of the token %s", owner, symbol)
}
if !token.Mintable {
return sdkerrors.Wrapf(types.ErrNotMintable, "%s", symbol)
}
supply := k.getTokenSupply(ctx, token.MinUnit)
precision := sdk.NewIntWithDecimal(1, int(token.Scale))
mintableAmt := sdk.NewIntFromUint64(token.MaxSupply).Mul(precision).Sub(supply)
mintableMainAmt := mintableAmt.Quo(precision).Uint64()
if amount > mintableMainAmt {
return sdkerrors.Wrapf(
types.ErrInvalidAmount,
"the amount exceeds the mintable token amount; expected (0, %d], got %d",
mintableMainAmt, amount,
)
}
mintCoin := sdk.NewCoin(token.MinUnit, sdk.NewIntFromUint64(amount).Mul(precision))
mintCoins := sdk.NewCoins(mintCoin)
// mint coins
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, mintCoins); err != nil {
return err
}
if recipient.Empty() {
recipient = owner
}
// sent coins to the recipient account
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, recipient, mintCoins)
}
// BurnToken burns the specified amount of token
func (k Keeper) BurnToken(
ctx sdk.Context,
symbol string,
amount uint64,
owner sdk.AccAddress,
) error {
token, err := k.getTokenBySymbol(ctx, symbol)
if err != nil {
return err
}
precision := sdk.NewIntWithDecimal(1, int(token.Scale))
burnCoin := sdk.NewCoin(token.GetMinUnit(), sdk.NewIntFromUint64(amount).Mul(precision))
burnCoins := sdk.NewCoins(burnCoin)
// burn coins
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, burnCoins); err != nil {
return err
}
k.AddBurnCoin(ctx, burnCoin)
return k.bankKeeper.BurnCoins(ctx, types.ModuleName, burnCoins)
}