Skip to content

Commit

Permalink
fix(x/accounts/lockup): prevent double withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 9, 2024
1 parent 5028893 commit 3c0a903
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions x/accounts/defaults/lockup/lockup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"time"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -302,7 +303,13 @@ func (bva *BaseLockup) WithdrawUnlockedCoins(

amount := sdk.Coins{}

// deduplicate the denoms
denoms := make(map[string]struct{})
for _, denom := range msg.Denoms {
denoms[denom] = struct{}{}
}

for denom := range maps.Keys(denoms) {
balance, err := bva.getBalance(ctx, fromAddress, denom)
if err != nil {
return nil, err
Expand Down
8 changes: 6 additions & 2 deletions x/accounts/defaults/lockup/periodic_locking_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,16 @@ func TestPeriodicAccountWithdrawUnlockedCoins(t *testing.T) {
Time: startTime.Add(time.Minute * 1),
})

_, err = acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
// withdraw unlocked token
resp, err := acc.WithdrawUnlockedCoins(sdkCtx, &lockuptypes.MsgWithdraw{
Withdrawer: "owner",
ToAddress: "receiver",
Denoms: []string{"test"},
Denoms: []string{"test", "test"}, // duplicate tokens should be ignored
})
require.NoError(t, err)
require.Equal(t, resp.AmountReceived.Len(), 1)
require.Equal(t, resp.AmountReceived, sdk.NewCoins(sdk.NewCoin("test", math.NewInt(5))))
require.Equal(t, resp.Receiver, "receiver")
}

func TestPeriodicAccountGetLockCoinInfo(t *testing.T) {
Expand Down

0 comments on commit 3c0a903

Please sign in to comment.