2022-03-23 14:34:23 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-03-23 14:34:23 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/savings/types"
|
2022-03-23 14:34:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2022-04-21 14:19:03 +00:00
|
|
|
|
|
|
|
deposit := types.NewDeposit(depositor, coins)
|
2022-03-23 14:34:23 +00:00
|
|
|
if foundDeposit {
|
2022-04-21 14:19:03 +00:00
|
|
|
deposit.Amount = deposit.Amount.Add(currDeposit.Amount...)
|
2022-09-29 20:18:06 +00:00
|
|
|
k.BeforeSavingsDepositModified(ctx, deposit, setDifference(getDenoms(coins), getDenoms(deposit.Amount)))
|
2022-04-21 14:19:03 +00:00
|
|
|
|
2022-03-23 14:34:23 +00:00
|
|
|
}
|
2022-04-21 14:19:03 +00:00
|
|
|
|
2022-03-23 14:34:23 +00:00
|
|
|
k.SetDeposit(ctx, deposit)
|
|
|
|
|
2022-04-21 14:19:03 +00:00
|
|
|
if !foundDeposit {
|
2022-09-29 20:18:06 +00:00
|
|
|
k.AfterSavingsDepositCreated(ctx, deposit)
|
2022-04-21 14:19:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 14:34:23 +00:00
|
|
|
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)
|
2022-04-21 14:19:03 +00:00
|
|
|
if !supported {
|
2023-04-05 23:21:59 +00:00
|
|
|
return errorsmod.Wrapf(types.ErrInvalidDepositDenom, ": %s", coin.Denom)
|
2022-03-23 14:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalDeposited returns the total amount deposited for the deposit denom
|
2023-04-05 23:21:59 +00:00
|
|
|
func (k Keeper) GetTotalDeposited(ctx sdk.Context, depositDenom string) (total sdkmath.Int) {
|
2022-03-23 14:34:23 +00:00
|
|
|
macc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
|
|
|
return k.bankKeeper.GetBalance(ctx, macc.GetAddress(), depositDenom).Amount
|
|
|
|
}
|
2022-04-21 14:19:03 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|