Skip to content

Commit

Permalink
EdenB Burn fixes (#1054)
Browse files Browse the repository at this point in the history
* add events

* fix burn and verify in tests

* edge cases

* remove

* review

* restore
  • Loading branch information
amityadav0 authored Dec 10, 2024
1 parent b7f0316 commit 75b5218
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions x/commitment/keeper/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator sdk.AccAddress, denom str

amount = amount.Sub(claimedRemovalAmount)
if amount.IsZero() {
k.SetCommitments(ctx, commitments)
return nil
}

Expand Down
12 changes: 9 additions & 3 deletions x/commitment/keeper/commitments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ func TestKeeper_BurnEdenBoost(t *testing.T) {
{
name: "deduct amount is greater than claimed amount",
claimedAmount: math.NewInt(100),
committedTokens: math.NewInt(100),
deductAmount: math.NewInt(200),
committedTokens: math.NewInt(0),
deductAmount: math.NewInt(100),
expectedError: false,
},
{
name: "deduct amount is greater than claimed amount with no committed tokens",
claimedAmount: math.NewInt(100),
claimedAmount: math.NewInt(200),
committedTokens: math.NewInt(0),
deductAmount: math.NewInt(200),
expectedError: false,
Expand Down Expand Up @@ -193,8 +193,14 @@ func TestKeeper_BurnEdenBoost(t *testing.T) {
commitments.AddCommittedTokens("denom", tt.committedTokens, 0)
keeper.SetCommitments(ctx, commitments)

comm := keeper.GetCommitments(ctx, addr)
totalPrev := comm.Claimed.AmountOf("denom").Add(comm.GetCommittedAmountForDenom("denom"))

// Test BurnEdenBoost
err := keeper.BurnEdenBoost(ctx, addr, "denom", tt.deductAmount)
comm = keeper.GetCommitments(ctx, addr)
total := comm.Claimed.AmountOf("denom").Add(comm.GetCommittedAmountForDenom("denom"))
assert.Equal(t, tt.deductAmount.String(), totalPrev.Sub(total).String())
if tt.expectedError {
assert.Error(t, err)
} else {
Expand Down
2 changes: 2 additions & 0 deletions x/estaking/keeper/hooks_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand All @@ -19,6 +20,7 @@ func (k Keeper) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress,
}

func (k Keeper) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
k.SetElysStakeChange(ctx, delAddr)
return nil
}

Expand Down
25 changes: 25 additions & 0 deletions x/estaking/keeper/keeper_burn_edenB.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAdd
if edenCommittedAndElysStakedDec.GT(math.LegacyZeroDec()) {
edenBToBurn = unstakedElysDec.Quo(edenCommittedAndElysStakedDec).MulInt(totalEdenB)
}
if edenCommittedAndElysStakedDec.IsZero() {
edenBToBurn = math.LegacyNewDecFromInt(totalEdenB)
}
if edenBToBurn.IsZero() {
return nil
}

// Add event for burning edenB
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.TypeEvtBurnEdenB,
sdk.NewAttribute(types.AttributeDelegatorAddress, delegator.String()),
sdk.NewAttribute(types.AttributeAmount, edenBToBurn.String()),
),
)

// Burn EdenB in commitment module
err = k.commKeeper.BurnEdenBoost(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt())
if err != nil {
Expand Down Expand Up @@ -89,10 +101,23 @@ func (k Keeper) BurnEdenBFromEdenUncommitted(ctx sdk.Context, delegator sdk.AccA
if edenCommittedAndElysStakedDec.GT(math.LegacyZeroDec()) {
edenBToBurn = unclaimedAmtDec.Quo(edenCommittedAndElysStakedDec).MulInt(totalEdenB)
}
if edenCommittedAndElysStakedDec.IsZero() {
edenBToBurn = math.LegacyNewDecFromInt(totalEdenB)
}

if edenBToBurn.IsZero() {
return nil
}

// Add event for burning edenB
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.TypeEvtBurnEdenB,
sdk.NewAttribute(types.AttributeDelegatorAddress, delegator.String()),
sdk.NewAttribute(types.AttributeAmount, edenBToBurn.String()),
),
)

// Burn EdenB in commitment module
err = k.commKeeper.BurnEdenBoost(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt())
return err
Expand Down
1 change: 1 addition & 0 deletions x/estaking/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

const (
TypeEvtWithdrawReward = "withdraw_reward"
TypeEvtBurnEdenB = "burn_edenB"

AttributeDelegatorAddress = "delegator_address"
AttributeValidatorAddress = "validator_address"
Expand Down

0 comments on commit 75b5218

Please sign in to comment.