0g-chain/x/router/keeper/msg_server.go

203 lines
6.1 KiB
Go
Raw Normal View History

package keeper
import (
"context"
"time"
2023-11-21 19:08:07 +00:00
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
earntypes "github.com/kava-labs/kava/x/earn/types"
"github.com/kava-labs/kava/x/router/types"
)
type msgServer struct {
keeper Keeper
}
// NewMsgServerImpl returns an implementation of the module's MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// MintDeposit converts a delegation into staking derivatives and deposits it all into an earn vault
func (m msgServer) MintDeposit(goCtx context.Context, msg *types.MsgMintDeposit) (*types.MsgMintDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return nil, err
}
val, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
derivative, err := m.keeper.liquidKeeper.MintDerivative(ctx, depositor, val, msg.Amount)
if err != nil {
return nil, err
}
err = m.keeper.earnKeeper.Deposit(ctx, depositor, derivative, earntypes.STRATEGY_TYPE_SAVINGS)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
)
return &types.MsgMintDepositResponse{}, nil
}
// DelegateMintDeposit delegates tokens to a validator, then converts them into staking derivatives,
// then deposits to an earn vault.
func (m msgServer) DelegateMintDeposit(goCtx context.Context, msg *types.MsgDelegateMintDeposit) (*types.MsgDelegateMintDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return nil, err
}
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
validator, found := m.keeper.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return nil, stakingtypes.ErrNoValidatorFound
}
bondDenom := m.keeper.stakingKeeper.BondDenom(ctx)
if msg.Amount.Denom != bondDenom {
return nil, errorsmod.Wrapf(
sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Amount.Denom, bondDenom,
)
}
newShares, err := m.keeper.stakingKeeper.Delegate(ctx, depositor, msg.Amount.Amount, stakingtypes.Unbonded, validator, true)
if err != nil {
return nil, err
}
derivativeMinted, err := m.keeper.liquidKeeper.MintDerivative(ctx, depositor, valAddr, msg.Amount)
if err != nil {
return nil, err
}
err = m.keeper.earnKeeper.Deposit(ctx, depositor, derivativeMinted, earntypes.STRATEGY_TYPE_SAVINGS)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
stakingtypes.EventTypeDelegate,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, valAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()),
sdk.NewAttribute(stakingtypes.AttributeKeyNewShares, newShares.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
})
return &types.MsgDelegateMintDepositResponse{}, nil
}
// WithdrawBurn removes staking derivatives from an earn vault and converts them back to a staking delegation.
func (m msgServer) WithdrawBurn(goCtx context.Context, msg *types.MsgWithdrawBurn) (*types.MsgWithdrawBurnResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
depositor, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
val, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
tokenAmount, err := m.keeper.liquidKeeper.DerivativeFromTokens(ctx, val, msg.Amount)
if err != nil {
return nil, err
}
feat: add proposals for community pool deposits/withdrawals (#1304) * feat: community pool deposit/withdraw proposals * fix: check community pool balance in tests * add new msg type definitions * add msg methods and tests * add module and keeper skeleton * add deposit and withdraw methods (no delegation) * untested depsit/withdraw with delegation methods * add cli cmds * fix cli argument parsing * add tests for delegate/undelegate msgs * emit un/delegate events * add godoc comments * tally handler with liquid staking support * clean up * update for liquid keeper changes * Exclude non-bkava denoms from aggregate underlying ukava calculation * wip Add claim * Add distr keeper and claiming * Add claim test * Update claim test with failures * wip Add staking rewards * -S Fix savings to earn incentive methods * Use a single accural time for all earn incentives * Add additional required liquid methods * Update genesis to only include 1 accrual time for earn * Revert "Update genesis to only include 1 accrual time for earn" This reverts commit cc7e35347298681c0c8a4a0b9bf9b9b296c25531. * Revert "Use a single accural time for all earn incentives" This reverts commit aeb49c4622d4e3d99dc6421c8830932b1b546be9. * Update tests with incentive distribution * Add earn to incentive rewards query * add earn cli tx * Update claim example to use ukava large * add proposal to gov router * fix example tx formating * add proposal handlers to gov app module * fix: define gov router after earn keeper * fix: correct proposal type * remove outdated comment * refactor withdraw so that fee pool is allows adjusted by the actual withdraw amount * fix: lint proto file * use non blocked module account instead of dist acc * add fund mod account to app, enable receiving * update to new withdraw interface * add human readable apy test cases * remove duplicate changes from previous merge * remove deprecated io/ioutil package * standardize proposal type as a pointer (also matches sdk) * minior comments and formatting * use withdraw amount in router msgs Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Draco <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
2022-09-29 17:01:06 +00:00
withdrawnAmount, err := m.keeper.earnKeeper.Withdraw(ctx, depositor, tokenAmount, earntypes.STRATEGY_TYPE_SAVINGS)
if err != nil {
return nil, err
}
feat: add proposals for community pool deposits/withdrawals (#1304) * feat: community pool deposit/withdraw proposals * fix: check community pool balance in tests * add new msg type definitions * add msg methods and tests * add module and keeper skeleton * add deposit and withdraw methods (no delegation) * untested depsit/withdraw with delegation methods * add cli cmds * fix cli argument parsing * add tests for delegate/undelegate msgs * emit un/delegate events * add godoc comments * tally handler with liquid staking support * clean up * update for liquid keeper changes * Exclude non-bkava denoms from aggregate underlying ukava calculation * wip Add claim * Add distr keeper and claiming * Add claim test * Update claim test with failures * wip Add staking rewards * -S Fix savings to earn incentive methods * Use a single accural time for all earn incentives * Add additional required liquid methods * Update genesis to only include 1 accrual time for earn * Revert "Update genesis to only include 1 accrual time for earn" This reverts commit cc7e35347298681c0c8a4a0b9bf9b9b296c25531. * Revert "Use a single accural time for all earn incentives" This reverts commit aeb49c4622d4e3d99dc6421c8830932b1b546be9. * Update tests with incentive distribution * Add earn to incentive rewards query * add earn cli tx * Update claim example to use ukava large * add proposal to gov router * fix example tx formating * add proposal handlers to gov app module * fix: define gov router after earn keeper * fix: correct proposal type * remove outdated comment * refactor withdraw so that fee pool is allows adjusted by the actual withdraw amount * fix: lint proto file * use non blocked module account instead of dist acc * add fund mod account to app, enable receiving * update to new withdraw interface * add human readable apy test cases * remove duplicate changes from previous merge * remove deprecated io/ioutil package * standardize proposal type as a pointer (also matches sdk) * minior comments and formatting * use withdraw amount in router msgs Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Draco <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
2022-09-29 17:01:06 +00:00
_, err = m.keeper.liquidKeeper.BurnDerivative(ctx, depositor, val, withdrawnAmount)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
)
return &types.MsgWithdrawBurnResponse{}, nil
}
// WithdrawBurnUndelegate removes staking derivatives from an earn vault, converts them to a staking delegation,
// then undelegates them from their validator.
func (m msgServer) WithdrawBurnUndelegate(goCtx context.Context, msg *types.MsgWithdrawBurnUndelegate) (*types.MsgWithdrawBurnUndelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
depositor, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
val, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return nil, err
}
tokenAmount, err := m.keeper.liquidKeeper.DerivativeFromTokens(ctx, val, msg.Amount)
if err != nil {
return nil, err
}
feat: add proposals for community pool deposits/withdrawals (#1304) * feat: community pool deposit/withdraw proposals * fix: check community pool balance in tests * add new msg type definitions * add msg methods and tests * add module and keeper skeleton * add deposit and withdraw methods (no delegation) * untested depsit/withdraw with delegation methods * add cli cmds * fix cli argument parsing * add tests for delegate/undelegate msgs * emit un/delegate events * add godoc comments * tally handler with liquid staking support * clean up * update for liquid keeper changes * Exclude non-bkava denoms from aggregate underlying ukava calculation * wip Add claim * Add distr keeper and claiming * Add claim test * Update claim test with failures * wip Add staking rewards * -S Fix savings to earn incentive methods * Use a single accural time for all earn incentives * Add additional required liquid methods * Update genesis to only include 1 accrual time for earn * Revert "Update genesis to only include 1 accrual time for earn" This reverts commit cc7e35347298681c0c8a4a0b9bf9b9b296c25531. * Revert "Use a single accural time for all earn incentives" This reverts commit aeb49c4622d4e3d99dc6421c8830932b1b546be9. * Update tests with incentive distribution * Add earn to incentive rewards query * add earn cli tx * Update claim example to use ukava large * add proposal to gov router * fix example tx formating * add proposal handlers to gov app module * fix: define gov router after earn keeper * fix: correct proposal type * remove outdated comment * refactor withdraw so that fee pool is allows adjusted by the actual withdraw amount * fix: lint proto file * use non blocked module account instead of dist acc * add fund mod account to app, enable receiving * update to new withdraw interface * add human readable apy test cases * remove duplicate changes from previous merge * remove deprecated io/ioutil package * standardize proposal type as a pointer (also matches sdk) * minior comments and formatting * use withdraw amount in router msgs Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Draco <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
2022-09-29 17:01:06 +00:00
withdrawnAmount, err := m.keeper.earnKeeper.Withdraw(ctx, depositor, tokenAmount, earntypes.STRATEGY_TYPE_SAVINGS)
if err != nil {
return nil, err
}
feat: add proposals for community pool deposits/withdrawals (#1304) * feat: community pool deposit/withdraw proposals * fix: check community pool balance in tests * add new msg type definitions * add msg methods and tests * add module and keeper skeleton * add deposit and withdraw methods (no delegation) * untested depsit/withdraw with delegation methods * add cli cmds * fix cli argument parsing * add tests for delegate/undelegate msgs * emit un/delegate events * add godoc comments * tally handler with liquid staking support * clean up * update for liquid keeper changes * Exclude non-bkava denoms from aggregate underlying ukava calculation * wip Add claim * Add distr keeper and claiming * Add claim test * Update claim test with failures * wip Add staking rewards * -S Fix savings to earn incentive methods * Use a single accural time for all earn incentives * Add additional required liquid methods * Update genesis to only include 1 accrual time for earn * Revert "Update genesis to only include 1 accrual time for earn" This reverts commit cc7e35347298681c0c8a4a0b9bf9b9b296c25531. * Revert "Use a single accural time for all earn incentives" This reverts commit aeb49c4622d4e3d99dc6421c8830932b1b546be9. * Update tests with incentive distribution * Add earn to incentive rewards query * add earn cli tx * Update claim example to use ukava large * add proposal to gov router * fix example tx formating * add proposal handlers to gov app module * fix: define gov router after earn keeper * fix: correct proposal type * remove outdated comment * refactor withdraw so that fee pool is allows adjusted by the actual withdraw amount * fix: lint proto file * use non blocked module account instead of dist acc * add fund mod account to app, enable receiving * update to new withdraw interface * add human readable apy test cases * remove duplicate changes from previous merge * remove deprecated io/ioutil package * standardize proposal type as a pointer (also matches sdk) * minior comments and formatting * use withdraw amount in router msgs Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Draco <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
2022-09-29 17:01:06 +00:00
sharesReturned, err := m.keeper.liquidKeeper.BurnDerivative(ctx, depositor, val, withdrawnAmount)
if err != nil {
return nil, err
}
completionTime, err := m.keeper.stakingKeeper.Undelegate(ctx, depositor, val, sharesReturned)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
stakingtypes.EventTypeUnbond,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, val.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()),
sdk.NewAttribute(stakingtypes.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
})
return &types.MsgWithdrawBurnUndelegateResponse{}, nil
}