0g-chain/x/savings/keeper/deposit.go
Denali Marsh a073238f34
Savings module deposits (#1192)
* module files

* proto types

* types and generated proto types

* keeper

* client scaffold

* add savings module to app

* remove placeholder types file

* implement rest and add to module

* update proto types

* validation for supported denoms

* generate updates proto types

* update comments

* update comments

* remove unused imports from proto files

* regenerate proto files

* update proto types

* client

* deposit type and generated proto types

* deposit keeper methods + tests

* update savings module file

* update app.go + test common

* remove abci

* remove refs to other modules

* remove endblocker call

* genesis init test for module account

* update genesis test with params

* add get/set params test

* fix up keeper test

* use params getter

* simplify if/else statement

* fix: add msgServer to keeper

* fix: register deposit message

* update deposit test

* wrap invalid deposit denom error msg

Co-authored-by: karzak <kjydavis3@gmail.com>
2022-03-23 15:34:23 +01:00

59 lines
1.6 KiB
Go

package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/kava-labs/kava/x/savings/types"
)
// Deposit deposit
func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coins) error {
err := k.ValidateDeposit(ctx, coins)
if err != nil {
return err
}
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins)
if err != nil {
return err
}
currDeposit, foundDeposit := k.GetDeposit(ctx, depositor)
amount := coins
if foundDeposit {
amount = amount.Add(currDeposit.Amount...)
}
deposit := types.NewDeposit(depositor, amount)
k.SetDeposit(ctx, deposit)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSavingsDeposit,
sdk.NewAttribute(sdk.AttributeKeyAmount, coins.String()),
sdk.NewAttribute(types.AttributeKeyDepositor, deposit.Depositor.String()),
),
)
return nil
}
// ValidateDeposit validates a deposit
func (k Keeper) ValidateDeposit(ctx sdk.Context, coins sdk.Coins) error {
for _, coin := range coins {
supported := k.IsDenomSupported(ctx, coin.Denom)
if supported == false {
return sdkerrors.Wrapf(types.ErrInvalidDepositDenom, ": %s", coin.Denom)
}
}
return nil
}
// GetTotalDeposited returns the total amount deposited for the deposit denom
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositDenom string) (total sdk.Int) {
macc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
return k.bankKeeper.GetBalance(ctx, macc.GetAddress(), depositDenom).Amount
}