0g-chain/x/savings/keeper/deposit.go
Denali Marsh eaeaf20e83
Incentive savings hooks + init/sync of savings claims (#1209)
* update savings module macc balances getter

* add savings keeper to incentive module

* add savings keeper to incentive module #2

* savings reward syncing

* claim savings reward

* update txs, queries

* update txs, queries #2

* update claim test

* add savings keeper to incentive module in app.go

* re-commit files to disk

* define and call hooks

* keeper methods for init/sync savings reward

* update other tests for easier extendibility

* init savings reward test

* add helper methods to global incentive unit tester

* sync savings test progress

* savings init fix + completed tests

* sync savings updates + tests

* nit: simplify false check

* fix: calculate set difference of incoming deposit denoms

Co-authored-by: karzak <kjydavis3@gmail.com>
2022-04-21 16:19:03 +02:00

90 lines
2.1 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)
deposit := types.NewDeposit(depositor, coins)
if foundDeposit {
deposit.Amount = deposit.Amount.Add(currDeposit.Amount...)
k.hooks.BeforeSavingsDepositModified(ctx, deposit, setDifference(getDenoms(coins), getDenoms(deposit.Amount)))
}
k.SetDeposit(ctx, deposit)
if !foundDeposit {
k.hooks.AfterSavingsDepositCreated(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 {
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
}
// Set setDifference: A - B
func setDifference(a, b []string) (diff []string) {
m := make(map[string]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return
}
func getDenoms(coins sdk.Coins) []string {
denoms := []string{}
for _, coin := range coins {
denoms = append(denoms, coin.Denom)
}
return denoms
}