Skip to content

Commit 75b8df4

Browse files
99adarshavkr003
andauthored
Fix max vesting cancellation (#1188)
* fix max vesting cancellation * add cancelled amount as event attributes --------- Co-authored-by: Abhinav Kumar <57705190+avkr003@users.noreply.github.com>
1 parent 746a57d commit 75b8df4

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

x/commitment/keeper/msg_server_cancel_vest.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (
3737
commitments := k.GetCommitments(ctx, creator)
3838

3939
remainingToCancel := msg.Amount
40+
totalCancelled := sdkmath.ZeroInt()
4041

4142
for i := len(commitments.VestingTokens) - 1; i >= 0; i-- {
4243
vesting := commitments.VestingTokens[i]
@@ -58,6 +59,7 @@ func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (
5859
commitments.VestingTokens[i] = vesting
5960

6061
remainingToCancel = remainingToCancel.Sub(cancelAmount)
62+
totalCancelled = totalCancelled.Add(cancelAmount)
6163
}
6264

6365
newVestingTokens := []*types.VestingTokens{}
@@ -70,15 +72,19 @@ func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (
7072

7173
commitments.VestingTokens = newVestingTokens
7274

73-
if !remainingToCancel.IsZero() {
75+
if totalCancelled.IsZero() {
7476
return nil, errorsmod.Wrapf(types.ErrInsufficientVestingTokens, "denom: %s, amount: %s", ptypes.Eden, msg.Amount)
7577
}
78+
ctx.Logger().Info("Successfully Cancelled vesting token",
79+
"creator", msg.Creator,
80+
"amount", totalCancelled.String(),
81+
"denom", msg.Denom)
7682

7783
// Update the unclaimed tokens amount
78-
commitments.AddClaimed(sdk.NewCoin(ptypes.Eden, msg.Amount))
84+
commitments.AddClaimed(sdk.NewCoin(ptypes.Eden, totalCancelled))
7985

8086
prev := k.GetTotalSupply(ctx)
81-
prev.TotalEdenSupply = prev.TotalEdenSupply.Add(msg.Amount)
87+
prev.TotalEdenSupply = prev.TotalEdenSupply.Add(totalCancelled)
8288
k.SetTotalSupply(ctx, prev)
8389
k.SetCommitments(ctx, commitments)
8490

@@ -88,6 +94,7 @@ func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (
8894
types.EventTypeCommitmentChanged,
8995
sdk.NewAttribute(types.AttributeCreator, msg.Creator),
9096
sdk.NewAttribute(types.AttributeAmount, msg.Amount.String()),
97+
sdk.NewAttribute(types.AttributeCancelledAmount, totalCancelled.String()),
9198
sdk.NewAttribute(types.AttributeDenom, ptypes.Eden),
9299
),
93100
)

x/commitment/keeper/msg_server_cancel_vest_test.go

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,78 @@ import (
1414
"github.com/stretchr/testify/require"
1515
)
1616

17+
func TestCancelVest_CompleteAmount(t *testing.T) {
18+
app := app.InitElysTestApp(true, t)
19+
20+
ctx := app.BaseApp.NewContext(true)
21+
// Create a test context and keeper
22+
keeper := app.CommitmentKeeper
23+
24+
msgServer := commitmentkeeper.NewMsgServerImpl(*keeper)
25+
26+
vestingInfos := []types.VestingInfo{
27+
{
28+
BaseDenom: ptypes.Eden,
29+
VestingDenom: ptypes.Elys,
30+
NumBlocks: 10,
31+
VestNowFactor: sdkmath.NewInt(90),
32+
NumMaxVestings: 10,
33+
},
34+
}
35+
36+
params := types.Params{
37+
VestingInfos: vestingInfos,
38+
}
39+
40+
keeper.SetParams(ctx, params)
41+
42+
// Create a new account
43+
creator, _ := sdk.AccAddressFromBech32("cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5")
44+
acc := app.AccountKeeper.GetAccount(ctx, creator)
45+
if acc == nil {
46+
acc = app.AccountKeeper.NewAccountWithAddress(ctx, creator)
47+
app.AccountKeeper.SetAccount(ctx, acc)
48+
}
49+
// Create a cancel vesting message
50+
cancelVestMsg := &types.MsgCancelVest{
51+
Creator: creator.String(),
52+
Denom: ptypes.Eden,
53+
Amount: sdkmath.NewInt(100),
54+
}
55+
56+
// Set up the commitments for the creator
57+
commitments := types.Commitments{
58+
Creator: creator.String(),
59+
VestingTokens: []*types.VestingTokens{
60+
{
61+
Denom: ptypes.Elys,
62+
TotalAmount: sdkmath.NewInt(100),
63+
ClaimedAmount: sdkmath.NewInt(0),
64+
NumBlocks: 100,
65+
StartBlock: 0,
66+
},
67+
},
68+
}
69+
keeper.SetCommitments(ctx, commitments)
70+
71+
// Increase the block height
72+
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 25)
73+
74+
// From 100 tokens, some of tokens are already vested and that will be claimed,
75+
// so CancelVest should cancel the remaining amount without any issue
76+
// Execute the CancelVest function
77+
_, err := msgServer.CancelVest(ctx, cancelVestMsg)
78+
require.NoError(t, err)
79+
80+
newCommitments := keeper.GetCommitments(ctx, creator)
81+
require.Len(t, newCommitments.VestingTokens, 0, "vesting tokens should be empty after cancelling all remaining amount")
82+
83+
// No vesting tokens, so should throw an error
84+
_, err = msgServer.CancelVest(ctx, cancelVestMsg)
85+
require.Error(t, err)
86+
require.True(t, types.ErrInsufficientVestingTokens.Is(err), "Error should be insufficient vesting tokens")
87+
}
88+
1789
func TestCancelVest(t *testing.T) {
1890
app := app.InitElysTestApp(true, t)
1991

@@ -83,11 +155,12 @@ func TestCancelVest(t *testing.T) {
83155
// check if the unclaimed tokens were updated correctly
84156
require.Equal(t, sdkmath.NewInt(25), newCommitments.GetClaimedForDenom(ptypes.Eden))
85157

86-
// Try to cancel an amount that exceeds the unvested amount
158+
// Try to cancel an amount that exceeds the unvested amount, should cancel all the remaining amount
87159
cancelVestMsg.Amount = sdkmath.NewInt(100)
88160
_, err = msgServer.CancelVest(ctx, cancelVestMsg)
89-
require.Error(t, err, "should throw an error when trying to cancel more tokens than available")
90-
require.True(t, types.ErrInsufficientVestingTokens.Is(err), "error should be insufficient vesting tokens")
161+
require.NoError(t, err)
162+
newCommitments = keeper.GetCommitments(ctx, creator)
163+
require.Len(t, newCommitments.VestingTokens, 0, "vesting tokens should be empty after cancelling all remaining amount")
91164
}
92165

93166
func TestCancelVest_WithPreviousClaimed(t *testing.T) {
@@ -246,5 +319,5 @@ func TestCancelVestNoVestingInfo(t *testing.T) {
246319
_, err := msgServer.CancelVest(ctx, cancelVestMsg)
247320
require.Error(t, err, "should throw an error when trying to cancel tokens with no vesting info")
248321
fmt.Println(err.Error())
249-
require.True(t, types.ErrInsufficientVestingTokens.Is(err), "error should be invalid denom")
322+
require.True(t, types.ErrInsufficientVestingTokens.Is(err), "Error should be insufficient vesting tokens")
250323
}

x/commitment/types/events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const (
88
EventTypeBurnCoins = "burn_coins"
99
EventTypeSendCoins = "send_coins"
1010

11-
AttributeCreator = "creator"
12-
AttributeAmount = "token_amount"
13-
AttributeDenom = "token_denom"
11+
AttributeCreator = "creator"
12+
AttributeAmount = "token_amount"
13+
AttributeCancelledAmount = "cancelled_amount"
14+
AttributeDenom = "token_denom"
1415
)

0 commit comments

Comments
 (0)