diff --git a/x/bank/keeper.go b/x/bank/keeper.go index 67c05d32068f..3a33870e1457 100644 --- a/x/bank/keeper.go +++ b/x/bank/keeper.go @@ -15,41 +15,40 @@ const ( costAddCoins sdk.Gas = 10 ) +//----------------------------------------------------------------------------- +// Keeper + +var _ Keeper = (*BaseKeeper)(nil) + // Keeper defines a module interface that facilitates the transfer of coins // between accounts. type Keeper interface { SendKeeper + SetCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) sdk.Error SubtractCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) } -var _ Keeper = (*BaseKeeper)(nil) - // BaseKeeper manages transfers between accounts. It implements the Keeper // interface. type BaseKeeper struct { - am auth.AccountKeeper -} + BaseSendKeeper -// NewBaseKeeper returns a new BaseKeeper -func NewBaseKeeper(am auth.AccountKeeper) BaseKeeper { - return BaseKeeper{am: am} + ak auth.AccountKeeper } -// GetCoins returns the coins at the addr. -func (keeper BaseKeeper) GetCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins { - return getCoins(ctx, keeper.am, addr) +// NewBaseKeeper returns a new BaseKeeper +func NewBaseKeeper(ak auth.AccountKeeper) BaseKeeper { + return BaseKeeper{ + BaseSendKeeper: NewBaseSendKeeper(ak), + ak: ak, + } } // SetCoins sets the coins at the addr. func (keeper BaseKeeper) SetCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) sdk.Error { - return setCoins(ctx, keeper.am, addr, amt) -} - -// HasCoins returns whether or not an account has at least amt coins. -func (keeper BaseKeeper) HasCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) bool { - return hasCoins(ctx, keeper.am, addr, amt) + return setCoins(ctx, keeper.ak, addr, amt) } // SubtractCoins subtracts amt from the coins at the addr. @@ -57,7 +56,7 @@ func (keeper BaseKeeper) SubtractCoins( ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, ) (sdk.Coins, sdk.Tags, sdk.Error) { - return subtractCoins(ctx, keeper.am, addr, amt) + return subtractCoins(ctx, keeper.ak, addr, amt) } // AddCoins adds amt to the coins at the addr. @@ -65,28 +64,17 @@ func (keeper BaseKeeper) AddCoins( ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, ) (sdk.Coins, sdk.Tags, sdk.Error) { - return addCoins(ctx, keeper.am, addr, amt) + return addCoins(ctx, keeper.ak, addr, amt) } -// SendCoins moves coins from one account to another -func (keeper BaseKeeper) SendCoins( - ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins, -) (sdk.Tags, sdk.Error) { - - return sendCoins(ctx, keeper.am, fromAddr, toAddr, amt) -} - -// InputOutputCoins handles a list of inputs and outputs -func (keeper BaseKeeper) InputOutputCoins(ctx sdk.Context, inputs []Input, outputs []Output) (sdk.Tags, sdk.Error) { - return inputOutputCoins(ctx, keeper.am, inputs, outputs) -} - -//______________________________________________________________________________________________ +//----------------------------------------------------------------------------- +// Send Keeper // SendKeeper defines a module interface that facilitates the transfer of coins // between accounts without the possibility of creating coins. type SendKeeper interface { ViewKeeper + SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) (sdk.Tags, sdk.Error) InputOutputCoins(ctx sdk.Context, inputs []Input, outputs []Output) (sdk.Tags, sdk.Error) } @@ -96,22 +84,17 @@ var _ SendKeeper = (*BaseSendKeeper)(nil) // SendKeeper only allows transfers between accounts without the possibility of // creating coins. It implements the SendKeeper interface. type BaseSendKeeper struct { - am auth.AccountKeeper -} - -// NewBaseSendKeeper returns a new BaseSendKeeper. -func NewBaseSendKeeper(am auth.AccountKeeper) BaseSendKeeper { - return BaseSendKeeper{am: am} -} + BaseViewKeeper -// GetCoins returns the coins at the addr. -func (keeper BaseSendKeeper) GetCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins { - return getCoins(ctx, keeper.am, addr) + ak auth.AccountKeeper } -// HasCoins returns whether or not an account has at least amt coins. -func (keeper BaseSendKeeper) HasCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) bool { - return hasCoins(ctx, keeper.am, addr, amt) +// NewBaseSendKeeper returns a new BaseSendKeeper. +func NewBaseSendKeeper(ak auth.AccountKeeper) BaseSendKeeper { + return BaseSendKeeper{ + BaseViewKeeper: NewBaseViewKeeper(ak), + ak: ak, + } } // SendCoins moves coins from one account to another @@ -119,7 +102,7 @@ func (keeper BaseSendKeeper) SendCoins( ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins, ) (sdk.Tags, sdk.Error) { - return sendCoins(ctx, keeper.am, fromAddr, toAddr, amt) + return sendCoins(ctx, keeper.ak, fromAddr, toAddr, amt) } // InputOutputCoins handles a list of inputs and outputs @@ -127,10 +110,13 @@ func (keeper BaseSendKeeper) InputOutputCoins( ctx sdk.Context, inputs []Input, outputs []Output, ) (sdk.Tags, sdk.Error) { - return inputOutputCoins(ctx, keeper.am, inputs, outputs) + return inputOutputCoins(ctx, keeper.ak, inputs, outputs) } -//______________________________________________________________________________________________ +//----------------------------------------------------------------------------- +// View Keeper + +var _ ViewKeeper = (*BaseViewKeeper)(nil) // ViewKeeper defines a module interface that facilitates read only access to // account balances. @@ -139,29 +125,29 @@ type ViewKeeper interface { HasCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) bool } -var _ ViewKeeper = (*BaseViewKeeper)(nil) - // BaseViewKeeper implements a read only keeper implementation of ViewKeeper. type BaseViewKeeper struct { - am auth.AccountKeeper + ak auth.AccountKeeper } // NewBaseViewKeeper returns a new BaseViewKeeper. -func NewBaseViewKeeper(am auth.AccountKeeper) BaseViewKeeper { - return BaseViewKeeper{am: am} +func NewBaseViewKeeper(ak auth.AccountKeeper) BaseViewKeeper { + return BaseViewKeeper{ + ak: ak, + } } // GetCoins returns the coins at the addr. func (keeper BaseViewKeeper) GetCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins { - return getCoins(ctx, keeper.am, addr) + return getCoins(ctx, keeper.ak, addr) } // HasCoins returns whether or not an account has at least amt coins. func (keeper BaseViewKeeper) HasCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) bool { - return hasCoins(ctx, keeper.am, addr, amt) + return hasCoins(ctx, keeper.ak, addr, amt) } -//______________________________________________________________________________________________ +//----------------------------------------------------------------------------- func getCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress) sdk.Coins { ctx.GasMeter().ConsumeGas(costGetCoins, "getCoins")