/
keeper.go
170 lines (145 loc) · 6.44 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
errorsmod "cosmossdk.io/errors"
custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper"
customterratypes "github.com/terra-money/core/v2/x/bank/types"
)
type Keeper struct {
custombankkeeper.Keeper
hooks customterratypes.BankHooks
ak accountkeeper.AccountKeeper
}
var _ bankkeeper.Keeper = Keeper{}
func NewBaseKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ak accountkeeper.AccountKeeper,
blockedAddrs map[string]bool,
authority string,
) Keeper {
keeper := Keeper{
Keeper: custombankkeeper.NewBaseKeeper(cdc, storeKey, ak, blockedAddrs, authority),
hooks: nil,
ak: ak,
}
return keeper
}
// Set the bank hooks
func (k *Keeper) SetHooks(bh customterratypes.BankHooks) *Keeper {
if k.hooks != nil {
panic("cannot set bank hooks twice")
}
k.hooks = bh
return k
}
// SendCoins transfers amt coins from a sending account to a receiving account.
// An error is returned upon failure.
func (k Keeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
err := k.BlockBeforeSend(ctx, fromAddr, toAddr, amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, fromAddr, toAddr, amt)
return k.Keeper.SendCoins(ctx, fromAddr, toAddr, amt)
}
// SendCoinsFromAccountToModule transfers coins from an AccAddress to a ModuleAccount.
// It will panic if the module account does not exist.
func (k Keeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
recipientAcc := k.ak.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "module account %s does not exist", recipientModule))
}
err := k.BlockBeforeSend(ctx, senderAddr, recipientAcc.GetAddress(), amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, senderAddr, recipientAcc.GetAddress(), amt)
return k.Keeper.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, amt)
}
// SendCoinsFromModuleToAccount transfers coins from a ModuleAccount to an AccAddress.
// It will panic if the module account does not exist. An error is returned if
// the recipient address is black-listed or if sending the tokens fails.
func (k Keeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
senderAddr := k.ak.GetModuleAddress(senderModule)
if senderAddr == nil {
panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "module account %s does not exist", senderModule))
}
err := k.BlockBeforeSend(ctx, senderAddr, recipientAddr, amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, senderAddr, recipientAddr, amt)
return k.Keeper.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, amt)
}
// SendCoinsFromModuleToManyAccounts transfers coins from a ModuleAccount to multiple AccAddresses.
// It will panic if the module account does not exist. An error is returned if
// the recipient address is black-listed or if sending the tokens fails.
func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error {
senderAddr := k.ak.GetModuleAddress(senderModule)
if senderAddr == nil {
panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "senderModule address %s is nil", senderModule))
}
recipientAcc := k.ak.GetModuleAccount(ctx, recipientModule)
if recipientAcc == nil {
panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "recipientModule address %s is nil", recipientModule))
}
return k.Keeper.SendCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt)
}
// UndelegateCoins performs undelegation by crediting amt coins to an account with
// address addr. For vesting accounts, undelegation amounts are tracked for both
// vesting and vested coins. The coins are then transferred from a ModuleAccount
// address to the delegator address. If any of the undelegation amounts are
// negative, an error is returned.
func (k Keeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error {
err := k.BlockBeforeSend(ctx, moduleAccAddr, delegatorAddr, amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, moduleAccAddr, delegatorAddr, amt)
return k.Keeper.UndelegateCoins(ctx, moduleAccAddr, delegatorAddr, amt)
}
// DelegateCoins performs delegation by deducting amt coins from an account with
// address addr. For vesting accounts, delegations amounts are tracked for both
// vesting and vested coins. The coins are then transferred from the delegator
// address to a ModuleAccount address. If any of the delegation amounts are negative,
// an error is returned.
func (k Keeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error {
err := k.BlockBeforeSend(ctx, delegatorAddr, moduleAccAddr, amt)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, delegatorAddr, moduleAccAddr, amt)
return k.Keeper.DelegateCoins(ctx, delegatorAddr, moduleAccAddr, amt)
}
// InputOutputCoins performs multi-send functionality. It accepts a series of
// inputs that correspond to a series of outputs. It returns an error if the
// inputs and outputs don't line up or if any single transfer of tokens fails.
func (k Keeper) InputOutputCoins(ctx sdk.Context, inputs []banktypes.Input, outputs []banktypes.Output) error {
// Only 1 input is allowed for all outputs check the following url:
// https://github.com/terra-money/cosmos-sdk/blob/release/v0.47.x/x/bank/types/msgs.go#L87-L89
//
// This if statement is added here too so we know
// when multiple inputs are allowed in the future
// because ErrMultipleSenders will fail to import
// because will be removed from the code.
if len(inputs) != 1 {
return banktypes.ErrMultipleSenders
}
input := inputs[0]
inputaddress := sdk.MustAccAddressFromBech32(input.Address)
for _, output := range outputs {
outputaddress := sdk.MustAccAddressFromBech32(output.Address)
err := k.BlockBeforeSend(ctx, inputaddress, outputaddress, output.Coins)
if err != nil {
return err
}
k.TrackBeforeSend(ctx, inputaddress, outputaddress, output.Coins)
}
return k.Keeper.InputOutputCoins(ctx, inputs, outputs)
}