-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
293 lines (236 loc) · 9.36 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package keeper
import (
"fmt"
"github.com/ci123chain/ci123chain/pkg/abci/codec"
"github.com/ci123chain/ci123chain/pkg/abci/store"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
"github.com/ci123chain/ci123chain/pkg/account"
"github.com/ci123chain/ci123chain/pkg/account/exported"
supply "github.com/ci123chain/ci123chain/pkg/supply/exported"
"github.com/ci123chain/ci123chain/pkg/supply/types"
vmtypes "github.com/ci123chain/ci123chain/pkg/vm/types"
"github.com/tendermint/tendermint/libs/log"
)
///supply/keeper/keeper.go
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
ak account.AccountKeeper
evmKeeper vmtypes.Keeper
permAddrs map[string]types.PermissionsForAddress
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ak account.AccountKeeper, maccPerms map[string][]string) Keeper {
permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
permAddrs[name] = types.NewPermissionForAddress(name, perms)
//fmt.Println(name, permAddrs[name].GetAddress().String())
}
return Keeper{
cdc: cdc,
storeKey: key,
ak: ak,
permAddrs: permAddrs,
}
}
func (k *Keeper) SetVMKeeper(vmkeeper vmtypes.Keeper) {
k.evmKeeper = vmkeeper
}
func (k Keeper) GetModuleAccount(ctx sdk.Context, moduleName string ) exported.ModuleAccountI {
acc, _ := k.GetModuleAccountAndPermissions(ctx, moduleName)
return acc
}
// GetModuleAddress returns an address based on the module name
func (k Keeper) GetModuleAddress(moduleName string) sdk.AccAddress {
permAddr, ok := k.permAddrs[moduleName]
if !ok {
return sdk.AccAddress{}
}
return permAddr.GetAddress()
}
func (k Keeper) GetModuleAccountAndPermissions(ctx sdk.Context, moduleName string) (exported.ModuleAccountI, []string) {
addr, perms := k.GetModuleAddressAndPermissions(moduleName)
if addr.Empty() {
return nil, []string{}
}
acc := k.ak.GetAccount(ctx, addr)
var macc exported.ModuleAccountI
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if ok {
if len(perms) != len(macc.GetPermissions()) {
macc.SetPermissions(perms)
}
return macc, perms
} else {
k.Logger(ctx).Error("ModuleAccount not found ", "ModuleName", moduleName)
}
}
macc = types.NewEmptyModuleAccount(moduleName, perms...)
maccI := (k.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)
k.SetModuleAccount(ctx, maccI)
return maccI, perms
}
func (k Keeper) GetModuleAddressAndPermissions(moduleName string) (addr sdk.AccAddress, permissions []string) {
permAddr, ok := k.permAddrs[moduleName]
if !ok {
return addr, permissions
}
return permAddr.GetAddress(), permAddr.GetPermissions()
}
func (k Keeper) SetModuleAccount(ctx sdk.Context, macc exported.ModuleAccountI) {
k.ak.SetAccount(ctx, macc)
}
// SendCoinsFromAccountToModule transfers coins from an AccAddress to a ModuleAccount
func (k Keeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress,
recipientModule string, amt sdk.Coins) error {
// create the account if it doesn't yet exist
recipientAcc := k.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
panic(fmt.Sprintf("module account %s isn't able to be created", recipientModule))
}
return k.ak.Transfer(ctx, senderAddr, recipientAcc.GetAddress(), amt)
}
// SendCoinsFromModuleToAccount transfers coins from a ModuleAccount to an AccAddress
func (k Keeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string,
recipientAddr sdk.AccAddress, amt sdk.Coins) error {
senderAddr := k.GetModuleAddress(senderModule)
if senderAddr.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("module account %s does not exist", senderModule))
}
return k.ak.Transfer(ctx, senderAddr, recipientAddr, amt)
}
func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coin) error {
senderAddr := k.GetModuleAddress(senderModule)
if senderAddr.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("module account %s does not exist", senderModule))
}
recipientAcc := k.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("module account %s isn't able to be created", recipientModule))
}
return k.ak.Transfer(ctx, senderAddr, recipientAcc.GetAddress(), sdk.NewCoins(amt))
}
func (k Keeper) DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string,
amt sdk.Coin) error {
recipientAcc := k.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("module account %s isn't able to be created", recipientModule))
}
if !recipientAcc.HasPermission(types.Staking) {
return sdkerrors.Wrap(sdkerrors.ErrParams, fmt.Sprintf("module account %s has no expected permission", recipientModule))
}
return k.ak.Transfer(ctx, senderAddr, recipientAcc.GetAddress(), sdk.NewCoins(amt))
}
// UndelegateCoinsFromModuleToAccount undelegates the unbonding coins and transfers
// them from a module account to the delegator account. It will panic if the
// module account does not exist or is unauthorized.
func (k Keeper) UndelegateCoinsFromModuleToAccount(
ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coin,
) error {
acc := k.GetModuleAccount(ctx, senderModule)
if acc == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("module account %s isn't able to be created", recipientAddr))
}
if !acc.HasPermission(types.Staking) {
return sdkerrors.Wrap(sdkerrors.ErrParams, fmt.Sprintf("module account %s has no expected permission", recipientAddr))
}
return k.ak.Transfer(ctx, acc.GetAddress(), recipientAddr, sdk.NewCoins(amt))
}
///-------------
//func (k Keeper) SetAccountSequence(ctx sdk.Context, addr sdk.AccAddress, nonce uint64) sdk.Error {
// k.ak.SetSequence(ctx, addr, nonce)
// return nil
//}
// GetSupply retrieves the Supply from store
func (k Keeper) GetSupply(ctx sdk.Context) (supply supply.SupplyI) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.SupplyKey)
if b == nil {
panic("stored supply should not have been nil")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &supply)
return
}
// SetSupply sets the Supply to store
func (k Keeper) SetSupply(ctx sdk.Context, supply supply.SupplyI) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(supply)
store.Set(types.SupplyKey, b)
}
// ValidatePermissions validates that the module account has been granted
// permissions within its set of allowed permissions.
func (k Keeper) ValidatePermissions(macc exported.ModuleAccountI) error {
permAddr := k.permAddrs[macc.GetName()]
for _, perm := range macc.GetPermissions() {
if !permAddr.HasPermission(perm) {
return fmt.Errorf("invalid module permission %s", perm)
}
}
return nil
}
// MintCoins creates new coins from thin air and adds it to the module account.
// It will panic if the module account does not exist or is unauthorized.
func (k Keeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
acc := k.GetModuleAccount(ctx, moduleName)
if acc == nil {
//panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName))
panic(fmt.Errorf("module account %s does not exist", moduleName))
}
if !acc.HasPermission(types.Minter) {
//panic(sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "module account %s does not have permissions to mint tokens", moduleName))
panic(fmt.Errorf( "module account %s does not have permissions to mint tokens", moduleName))
}
_, err := k.ak.AddBalance(ctx, acc.GetAddress(), amt)
if err != nil {
return err
}
// update total supply
supply := k.GetSupply(ctx)
// todo fix inflate coins
supply = supply.Inflate(amt)
k.SetSupply(ctx, supply)
logger := k.Logger(ctx)
logger.Debug(fmt.Sprintf("minted %s from %s module account", amt.String(), moduleName))
return nil
}
func (k Keeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
return k.ak.Transfer(ctx, fromAddr, toAddr, amt)
}
func (k Keeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
acc := k.GetModuleAccount(ctx, moduleName)
if acc == nil {
panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName))
}
if !acc.HasPermission(types.Minter) {
panic(sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "module account %s does not have permissions to mint tokens", moduleName))
}
_, err := k.ak.AddBalance(ctx, acc.GetAddress(), amt)
if err != nil {
return err
}
// update total supply
supply := k.GetSupply(ctx)
// todo fix to coins
supply.Inflate(amt)
k.SetSupply(ctx, supply)
logger := k.Logger(ctx)
logger.Info("burned coins from module account", "amount", amt.String(), "from", moduleName)
return nil
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// GetDenomMetaData retrieves the denomination metadata
func (k Keeper) GetDenomMetaData(ctx sdk.Context, denom string) types.Metadata {
st := ctx.KVStore(k.storeKey)
st = store.NewPrefixStore(st, types.DenomMetadataKey(denom))
bz := st.Get([]byte(denom))
if bz == nil {
return types.Metadata{}
}
var metadata types.Metadata
k.cdc.MustUnmarshalBinaryBare(bz, &metadata)
return metadata
}