-
Notifications
You must be signed in to change notification settings - Fork 3
/
keeper.go
221 lines (182 loc) · 5.38 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
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/bitsongofficial/chainmodules/x/fantoken/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/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("go-bitsong/%s", types.ModuleName))
}
// IssueToken issues a new token
func (k Keeper) IssueFanToken(
ctx sdk.Context,
symbol string,
name string,
maxSupply sdk.Int,
description string,
owner sdk.AccAddress,
issueFee sdk.Coin,
) error {
issuePrice := k.GetParamSet(ctx).IssuePrice
if issueFee.Amount.LT(issuePrice.Amount) {
return sdkerrors.Wrapf(types.ErrLessIssueFee, "the issue fee %s is less than %s", issueFee.String(), issuePrice.String())
}
denom := fmt.Sprintf("%s%s", "u", symbol)
denomMetaData := banktypes.Metadata{
Description: description,
Base: denom,
Display: symbol,
DenomUnits: []*banktypes.DenomUnit{
{Denom: denom, Exponent: 0},
{Denom: symbol, Exponent: types.FanTokenDecimal},
},
}
token := types.NewFanToken(name, maxSupply, owner, denomMetaData)
if err := k.AddFanToken(ctx, token); err != nil {
return err
}
return nil
}
// EditToken edits the specified token
func (k Keeper) EditFanToken(
ctx sdk.Context,
symbol string,
mintable bool,
owner sdk.AccAddress,
) error {
// get the destination token
token, err := k.getFanTokenBySymbol(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, "the fantoken %s is not mintable", symbol)
}
token.Mintable = mintable
if !mintable {
supply := k.getFanTokenSupply(ctx, token.GetDenom())
precision := sdk.NewIntWithDecimal(1, types.FanTokenDecimal)
token.MaxSupply = supply.Quo(precision)
}
k.setFanToken(ctx, token)
return nil
}
// TransferTokenOwner transfers the owner of the specified token to a new one
func (k Keeper) TransferFanTokenOwner(
ctx sdk.Context,
symbol string,
srcOwner sdk.AccAddress,
dstOwner sdk.AccAddress,
) error {
token, err := k.getFanTokenBySymbol(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.setFanToken(ctx, token)
// reset all indices
k.resetStoreKeyForQueryToken(ctx, token.GetSymbol(), 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) MintFanToken(
ctx sdk.Context,
recipient sdk.AccAddress,
denom string,
amount sdk.Int,
owner sdk.AccAddress,
) error {
token, err := k.getFanTokenByDenom(ctx, denom)
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, denom)
}
if !token.Mintable {
return sdkerrors.Wrapf(types.ErrNotMintable, "%s", denom)
}
supply := k.getFanTokenSupply(ctx, token.GetDenom())
mintableAmt := token.MaxSupply.Sub(supply)
if amount.GT(mintableAmt) {
return sdkerrors.Wrapf(
types.ErrInvalidAmount,
"the amount exceeds the mintable token amount; expected (0, %d], got %d",
mintableAmt, amount,
)
}
mintCoin := sdk.NewCoin(token.GetDenom(), amount)
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) BurnFanToken(
ctx sdk.Context,
denom string,
amount sdk.Int,
owner sdk.AccAddress,
) error {
_, err := k.getFanTokenByDenom(ctx, denom)
if err != nil {
return err
}
burnCoin := sdk.NewCoin(denom, amount)
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)
}