Skip to content

Commit

Permalink
Fix event emitting order in BurnEdenBoost function
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed May 15, 2024
1 parent aa7e0fa commit 5ea1b36
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 129 deletions.
111 changes: 17 additions & 94 deletions x/commitment/keeper/commitments.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/elys-network/elys/x/commitment/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)

// SetCommitments set a specific commitments in the store from its index
Expand Down Expand Up @@ -126,19 +123,13 @@ func (k Keeper) DeductClaimed(ctx sdk.Context, creator string, denom string, amo
return commitments, nil
}

func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) (types.Commitments, error) {
func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) error {
// Get the Commitments for the creator
commitments := k.GetCommitments(ctx, creator)

addr := sdk.MustAccAddressFromBech32(creator)
err := k.hooks.BeforeEdenBCommitChange(ctx, addr)
if err != nil {
return commitments, err
}

// if deduction amount is zero
if amount.Equal(sdk.ZeroInt()) {
return commitments, nil
if amount.IsZero() {
return nil
}

// Subtract the amount from the claimed balance
Expand All @@ -147,109 +138,41 @@ func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator string, denom string, amo
if claimed.LT(claimedRemovalAmount) {
claimedRemovalAmount = claimed
}
err = commitments.SubClaimed(sdk.NewCoin(denom, claimedRemovalAmount))
err := commitments.SubClaimed(sdk.NewCoin(denom, claimedRemovalAmount))
if err != nil {
return types.Commitments{}, err
return err
}

amount = amount.Sub(claimedRemovalAmount)
if amount.Equal(sdk.ZeroInt()) {
return commitments, nil
if amount.IsZero() {
return nil
}

committedAmount := commitments.GetCommittedAmountForDenom(denom)
if committedAmount.LT(amount) {
amount = committedAmount
}

// Subtract the amount from the committed balance
err = commitments.DeductFromCommitted(denom, amount, uint64(ctx.BlockTime().Unix()))
if err != nil {
return types.Commitments{}, err
if amount.IsZero() {
return nil
}

err = k.hooks.CommitmentChanged(ctx, creator, sdk.Coins{sdk.NewCoin(denom, amount)})
addr := sdk.MustAccAddressFromBech32(creator)
err = k.hooks.BeforeEdenBCommitChange(ctx, addr)
if err != nil {
return types.Commitments{}, err
return err
}
return commitments, nil
}

func (k Keeper) HandleWithdrawFromCommitment(ctx sdk.Context, commitments *types.Commitments, amount sdk.Coins, sendCoins bool, addr sdk.AccAddress) error {
edenAmount := amount.AmountOf(ptypes.Eden)
edenBAmount := amount.AmountOf(ptypes.EdenB)
commitments.AddClaimed(sdk.NewCoin(ptypes.Eden, edenAmount))
commitments.AddClaimed(sdk.NewCoin(ptypes.EdenB, edenBAmount))
k.SetCommitments(ctx, *commitments)

// Emit Hook commitment changed
err := k.CommitmentChanged(ctx, commitments.Creator, amount)
// Subtract the amount from the committed balance
err = commitments.DeductFromCommitted(denom, amount, uint64(ctx.BlockTime().Unix()))
if err != nil {
return err
}

withdrawCoins := amount.
Sub(sdk.NewCoin(ptypes.Eden, edenAmount)).
Sub(sdk.NewCoin(ptypes.EdenB, edenBAmount))

if sendCoins && !withdrawCoins.Empty() {
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, withdrawCoins)
}
return nil
}

// Process delegation hook - create commitment entities for delegator and validator
func (k Keeper) BeforeDelegationCreated(ctx sdk.Context, delegator string, validator string) error {
_, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "unable to convert address from bech32")
}
k.SetCommitments(ctx, commitments)

_, err = sdk.ValAddressFromBech32(validator)
err = k.hooks.CommitmentChanged(ctx, creator, sdk.Coins{sdk.NewCoin(denom, amount)})
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "unable to convert validator address from bech32")
}

/***********************************************************/
////////////////// Delegator entity //////////////////////////
/***********************************************************/
// Get the Commitments for the delegator
commitments := k.GetCommitments(ctx, delegator)
if commitments.IsEmpty() {
k.SetCommitments(ctx, commitments)

// Emit Hook commitment changed
err := k.CommitmentChanged(ctx, delegator, sdk.Coins{})
if err != nil {
return err
}

// Emit blockchain event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCommitmentChanged,
sdk.NewAttribute(types.AttributeCreator, delegator),
sdk.NewAttribute(types.AttributeAmount, sdk.ZeroInt().String()),
),
)
}

/***************************************************************/
////////////////////// Validator entity /////////////////////////
// Get the Commitments for the validator
commitments = k.GetCommitments(ctx, validator)
if commitments.IsEmpty() {
k.SetCommitments(ctx, commitments)

// Emit blockchain event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCommitmentChanged,
sdk.NewAttribute(types.AttributeCreator, validator),
sdk.NewAttribute(types.AttributeAmount, sdk.ZeroInt().String()),
),
)
return err
}

return nil
}
33 changes: 22 additions & 11 deletions x/commitment/keeper/msg_server_uncommit_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,43 @@ func (k Keeper) UncommitTokens(ctx sdk.Context, addr sdk.AccAddress, denom strin
k.SetCommitments(ctx, commitments)

liquidCoins := sdk.NewCoins(sdk.NewCoin(denom, amount))
edenAmount := liquidCoins.AmountOf(ptypes.Eden)
edenBAmount := liquidCoins.AmountOf(ptypes.EdenB)
commitments.AddClaimed(sdk.NewCoin(ptypes.Eden, edenAmount))
commitments.AddClaimed(sdk.NewCoin(ptypes.EdenB, edenBAmount))
k.SetCommitments(ctx, commitments)

err = k.HandleWithdrawFromCommitment(ctx, &commitments, liquidCoins, true, addr)
// Emit Hook commitment changed
err = k.CommitmentChanged(ctx, addr.String(), sdk.Coins{sdk.NewCoin(denom, amount)})
if err != nil {
return err
}

// Emit Hook if Eden is uncommitted
if denom == ptypes.Eden {
err = k.EdenUncommitted(ctx, addr.String(), sdk.NewCoin(denom, amount))
withdrawCoins := liquidCoins.
Sub(sdk.NewCoin(ptypes.Eden, edenAmount)).
Sub(sdk.NewCoin(ptypes.EdenB, edenBAmount))

if !withdrawCoins.Empty() {
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, withdrawCoins)
if err != nil {
return err
}
}

// Emit Hook commitment changed
err = k.CommitmentChanged(ctx, addr.String(), sdk.Coins{sdk.NewCoin(denom, amount)})
if err != nil {
return err
}

// Update total commitment
params := k.GetParams(ctx)
params.TotalCommitted = params.TotalCommitted.Add(liquidCoins...)
k.SetParams(ctx, params)

// Emit blockchain event
// Emit Hook if Eden is uncommitted
if denom == ptypes.Eden {
err = k.EdenUncommitted(ctx, addr.String(), sdk.NewCoin(denom, amount))
if err != nil {
return err
}
}

// Emit event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCommitmentChanged,
Expand Down
28 changes: 24 additions & 4 deletions x/estaking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ func (k Keeper) CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Co
}

del := k.Delegation(ctx, addr, edenValAddr)
if del != nil {
if del == nil {
err = k.Keeper.Hooks().BeforeDelegationRemoved(ctx, addr, edenValAddr)
if err != nil {
return err
}
} else {
err = k.Keeper.Hooks().AfterDelegationModified(ctx, addr, edenValAddr)
if err != nil {
return err
Expand All @@ -33,7 +38,12 @@ func (k Keeper) CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Co
}

del := k.Delegation(ctx, addr, edenBValAddr)
if del != nil {
if del == nil {
err = k.Keeper.Hooks().BeforeDelegationRemoved(ctx, addr, edenBValAddr)
if err != nil {
return err
}
} else {
err = k.Keeper.Hooks().AfterDelegationModified(ctx, addr, edenBValAddr)
if err != nil {
return err
Expand Down Expand Up @@ -83,7 +93,12 @@ func (k Keeper) BeforeEdenCommitChange(ctx sdk.Context, addr sdk.AccAddress) err
}

del := k.Delegation(ctx, addr, edenValAddr)
if del != nil {
if del == nil {
err = k.Keeper.Hooks().BeforeDelegationCreated(ctx, addr, edenValAddr)
if err != nil {
return err
}
} else {
err = k.Keeper.Hooks().BeforeDelegationSharesModified(ctx, addr, edenValAddr)
if err != nil {
return err
Expand All @@ -100,7 +115,12 @@ func (k Keeper) BeforeEdenBCommitChange(ctx sdk.Context, addr sdk.AccAddress) er
}

del := k.Delegation(ctx, addr, edenBValAddr)
if del != nil {
if del == nil {
err = k.Keeper.Hooks().BeforeDelegationCreated(ctx, addr, edenBValAddr)
if err != nil {
return err
}
} else {
err = k.Keeper.Hooks().BeforeDelegationSharesModified(ctx, addr, edenBValAddr)
if err != nil {
return err
Expand Down
8 changes: 0 additions & 8 deletions x/estaking/keeper/hooks_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ import (

// Creating a commitment object for a delegator if one does not exist:
func (k Keeper) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
// must not run on genesis
if ctx.BlockHeight() <= 1 {
return nil
}

// Create an entity in commitment module
k.commKeeper.BeforeDelegationCreated(ctx, delAddr.String(), valAddr.String())

return nil
}

Expand Down
7 changes: 2 additions & 5 deletions x/estaking/keeper/keeper_burn_edenB.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAdd
}

// Burn EdenB in commitment module
commitment, err := k.commKeeper.BurnEdenBoost(ctx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt())
err := k.commKeeper.BurnEdenBoost(ctx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt())
if err != nil {
k.Logger(ctx).Error("EdenB burn failure", err)
} else {
k.commKeeper.SetCommitments(ctx, commitment)
}
}

Expand Down Expand Up @@ -89,7 +87,6 @@ func (k Keeper) BurnEdenBFromEdenUncommitted(ctx sdk.Context, delegator string,
}

// Burn EdenB in commitment module
commitment, err := k.commKeeper.BurnEdenBoost(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt())
k.commKeeper.SetCommitments(ctx, commitment)
err := k.commKeeper.BurnEdenBoost(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt())
return err
}
3 changes: 1 addition & 2 deletions x/estaking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ type CommitmentKeeper interface {
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error
SetCommitments(sdk.Context, commitmenttypes.Commitments)
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) (commitmenttypes.Commitments, error)
BeforeDelegationCreated(sdk.Context, string, string) error
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) error
}

type DistrKeeper interface {
Expand Down
4 changes: 1 addition & 3 deletions x/incentive/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (

// CommitmentKeeper
type CommitmentKeeper interface {
// Initiate commitment according to standard staking
BeforeDelegationCreated(sdk.Context, string, string) error
// Iterate all commitment
IterateCommitments(sdk.Context, func(ctypes.Commitments) (stop bool))
// Update commitment
SetCommitments(sdk.Context, ctypes.Commitments)
// Get commitment
GetCommitments(sdk.Context, string) ctypes.Commitments
// Burn eden boost
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) (ctypes.Commitments, error)
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) error
}

// Staking keeper
Expand Down
3 changes: 1 addition & 2 deletions x/masterchef/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (

// CommitmentKeeper
type CommitmentKeeper interface {
BeforeDelegationCreated(sdk.Context, string, string) error
IterateCommitments(sdk.Context, func(ctypes.Commitments) (stop bool))
GetCommitments(sdk.Context, string) ctypes.Commitments
SetCommitments(sdk.Context, ctypes.Commitments)
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) (ctypes.Commitments, error)
BurnEdenBoost(ctx sdk.Context, creator string, denom string, amount math.Int) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
GetParams(sdk.Context) ctypes.Params
}
Expand Down

0 comments on commit 5ea1b36

Please sign in to comment.