Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove recipient amount from map #20625

Merged
merged 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions x/protocolpool/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ func (k MsgServer) CancelContinuousFund(ctx context.Context, msg *types.MsgCance
return nil, fmt.Errorf("failed to remove continuous fund for recipient %s: %w", msg.RecipientAddress, err)
}

if err := k.RecipientFundPercentage.Remove(ctx, recipient); err != nil {
return nil, fmt.Errorf("failed to remove recipient fund percentage for recipient %s: %w", msg.RecipientAddress, err)
}
// clean up state after canceling continuous fund, all funds must be removed before
value, err := k.RecipientFundDistribution.Get(ctx, recipient)
if err != nil {
return nil, fmt.Errorf("failed to get recipient fund distribution for recipient %s: %w", msg.RecipientAddress, err)
}
if value.IsZero() {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
if err := k.RecipientFundDistribution.Remove(ctx, recipient); err != nil {
return nil, fmt.Errorf("failed to remove recipient fund distribution for recipient %s: %w", msg.RecipientAddress, err)
}
}

return &types.MsgCancelContinuousFundResponse{
CanceledTime: canceledTime,
CanceledHeight: uint64(canceledHeight),
Expand Down
27 changes: 22 additions & 5 deletions x/protocolpool/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,10 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
recipient2 := sdk.AccAddress([]byte("recipientAddr2___________________"))
recipient2StrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipient2)
suite.Require().NoError(err)
recipient3 := sdk.AccAddress([]byte("recipientAddr3___________________"))
recipient3StrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipient3)
suite.Require().NoError(err)

testCases := map[string]struct {
preRun func()
recipientAddr sdk.AccAddress
Expand Down Expand Up @@ -908,20 +912,26 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
cf := types.ContinuousFund{
Recipient: recipientStrAddr,
Recipient: recipient3StrAddr,
Percentage: percentage,
Expiry: &expiry,
}
err = suite.poolKeeper.ContinuousFund.Set(suite.ctx, recipientAddr, cf)
suite.mockWithdrawContinuousFund()
err = suite.poolKeeper.ContinuousFund.Set(suite.ctx, recipient3, cf)
suite.Require().NoError(err)
err = suite.poolKeeper.RecipientFundPercentage.Set(suite.ctx, recipient3, math.ZeroInt())
suite.Require().NoError(err)
err = suite.poolKeeper.RecipientFundDistribution.Set(suite.ctx, recipient3, math.ZeroInt())
suite.Require().NoError(err)
},
recipientAddr: recipientAddr,
recipientAddr: recipient3,
expErr: false,
postRun: func() {
_, err := suite.poolKeeper.ContinuousFund.Get(suite.ctx, recipientAddr)
_, err := suite.poolKeeper.ContinuousFund.Get(suite.ctx, recipient3)
suite.Require().Error(err)
suite.Require().ErrorIs(err, collections.ErrNotFound)
},
withdrawnFunds: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(0)),
},
}

Expand All @@ -943,7 +953,14 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
suite.Require().Contains(err.Error(), tc.expErrMsg)
} else {
suite.Require().NoError(err)
suite.Require().Equal(resp.WithdrawnAllocatedFund, tc.withdrawnFunds)
suite.Require().Equal(tc.withdrawnFunds, resp.WithdrawnAllocatedFund)
// All items below should return error as they are removed from the store
_, err := suite.poolKeeper.RecipientFundPercentage.Get(suite.ctx, tc.recipientAddr)
suite.Require().Contains(err.Error(), "collections: not found")
_, err = suite.poolKeeper.ContinuousFund.Get(suite.ctx, tc.recipientAddr)
suite.Require().Contains(err.Error(), "collections: not found")
_, err = suite.poolKeeper.RecipientFundDistribution.Get(suite.ctx, tc.recipientAddr)
suite.Require().Contains(err.Error(), "collections: not found")
}
if tc.postRun != nil {
tc.postRun()
Expand Down
Loading