-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathbank.go
146 lines (111 loc) · 4.92 KB
/
bank.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/supply/internal/types"
)
// SendCoinsFromModuleToAccount transfers coins from a ModuleAccount to an AccAddress
func (k Keeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string,
recipientAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
senderAddr := k.GetModuleAddress(senderModule)
if senderAddr == nil {
return sdk.ErrUnknownAddress(fmt.Sprintf("module account %s does not exist", senderModule))
}
return k.bk.SendCoins(ctx, senderAddr, recipientAddr, amt)
}
// SendCoinsFromModuleToModule transfers coins from a ModuleAccount to another
func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) sdk.Error {
senderAddr := k.GetModuleAddress(senderModule)
if senderAddr == nil {
return sdk.ErrUnknownAddress(fmt.Sprintf("module account %s does not exist", senderModule))
}
// 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.bk.SendCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt)
}
// SendCoinsFromAccountToModule transfers coins from an AccAddress to a ModuleAccount
func (k Keeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress,
recipientModule string, amt sdk.Coins) sdk.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.bk.SendCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt)
}
// DelegateCoinsFromAccountToModule delegates coins and transfers
// them from a delegator account to a module account
func (k Keeper) DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress,
recipientModule string, amt sdk.Coins) sdk.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))
}
if !recipientAcc.HasPermission(types.Staking) {
panic(fmt.Sprintf("module account %s does not have permissions to receive delegated coins", recipientModule))
}
return k.bk.DelegateCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt)
}
// UndelegateCoinsFromModuleToAccount undelegates the unbonding coins and transfers
// them from a module account to the delegator account
func (k Keeper) UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string,
recipientAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
acc := k.GetModuleAccount(ctx, senderModule)
if acc == nil {
return sdk.ErrUnknownAddress(fmt.Sprintf("module account %s does not exist", senderModule))
}
if !acc.HasPermission(types.Staking) {
panic(fmt.Sprintf("module account %s does not have permissions to undelegate coins", senderModule))
}
return k.bk.UndelegateCoins(ctx, acc.GetAddress(), recipientAddr, amt)
}
// MintCoins creates new coins from thin air and adds it to the module account.
// Panics if the name maps to a non-minter module account or if the amount is invalid.
func (k Keeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) sdk.Error {
// create the account if it doesn't yet exist
acc := k.GetModuleAccount(ctx, moduleName)
if acc == nil {
return sdk.ErrUnknownAddress(fmt.Sprintf("module account %s does not exist", moduleName))
}
if !acc.HasPermission(types.Minter) {
panic(fmt.Sprintf("module account %s does not have permissions to mint tokens", moduleName))
}
_, err := k.bk.AddCoins(ctx, acc.GetAddress(), amt)
if err != nil {
panic(err)
}
// update total supply
supply := k.GetSupply(ctx)
supply = supply.Inflate(amt)
k.SetSupply(ctx, supply)
logger := k.Logger(ctx)
logger.Info(fmt.Sprintf("minted %s from %s module account", amt.String(), moduleName))
return nil
}
// BurnCoins burns coins deletes coins from the balance of the module account.
// Panics if the name maps to a non-burner module account or if the amount is invalid.
func (k Keeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) sdk.Error {
// create the account if it doesn't yet exist
acc := k.GetModuleAccount(ctx, moduleName)
if acc == nil {
return sdk.ErrUnknownAddress(fmt.Sprintf("module account %s does not exist", moduleName))
}
if !acc.HasPermission(types.Burner) {
panic(fmt.Sprintf("module account %s does not have permissions to burn tokens", moduleName))
}
_, err := k.bk.SubtractCoins(ctx, acc.GetAddress(), amt)
if err != nil {
panic(err)
}
// update total supply
supply := k.GetSupply(ctx)
supply = supply.Deflate(amt)
k.SetSupply(ctx, supply)
logger := k.Logger(ctx)
logger.Info(fmt.Sprintf("burned %s from %s module account", amt.String(), moduleName))
return nil
}