From 2f3ab146994bff280a7c86fe270345e5fc8f2a2d Mon Sep 17 00:00:00 2001 From: Servio Zambrano Date: Thu, 16 May 2024 07:26:15 -0400 Subject: [PATCH] AddCommittedToken was edited to avoid amount duplicate on Lockup (#476) * AddCommittedToken was edited to avoid amount duplicate on Lockup * AddCommittedToken was edited to avoid amount duplicate on Lockup * a new test was added to check if the lockup amount is doubled for itself * a new test was added to check if the lockup amount is doubled for itself --------- Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> --- x/commitment/types/commitments.go | 18 ------------------ x/commitment/types/commitments_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/x/commitment/types/commitments.go b/x/commitment/types/commitments.go index 960c620f8..5c9062906 100644 --- a/x/commitment/types/commitments.go +++ b/x/commitment/types/commitments.go @@ -44,24 +44,6 @@ func (c *Commitments) AddCommittedTokens(denom string, amount math.Int, unlockTi Amount: amount, UnlockTimestamp: unlockTime, }) - - li := int(0) - for _, lockup := range token.Lockups { - if lockup.UnlockTimestamp < unlockTime { - li++ - continue - } else if lockup.UnlockTimestamp == unlockTime { - c.CommittedTokens[i].Lockups[li].Amount = lockup.Amount.Add(amount) - return - } else { - break - } - } - c.CommittedTokens[i].Lockups = append(token.Lockups[:li+1], token.Lockups[li:]...) - c.CommittedTokens[i].Lockups[li] = Lockup{ - Amount: amount, - UnlockTimestamp: unlockTime, - } return } } diff --git a/x/commitment/types/commitments_test.go b/x/commitment/types/commitments_test.go index bd4136f1a..0146b902e 100644 --- a/x/commitment/types/commitments_test.go +++ b/x/commitment/types/commitments_test.go @@ -25,6 +25,15 @@ func TestCommitments_AddCommittedTokens(t *testing.T) { require.Equal(t, commitments.CommittedTokens[0].Lockups[0].Amount.String(), "100") require.Equal(t, commitments.CommittedTokens[0].Lockups[0].UnlockTimestamp, uint64(100)) require.Len(t, commitments.CommittedTokens[1].Lockups, 1) + + commitments.AddCommittedTokens("lp/3", sdk.NewInt(1000), 100) + commitments.AddCommittedTokens("lp/3", sdk.NewInt(2000), 120) + commitments.AddCommittedTokens("lp/3", sdk.NewInt(3000), 130) + + require.Equal(t, commitments.CommittedTokens[2].Lockups[0].Amount.String(), "1000") + require.Equal(t, commitments.CommittedTokens[2].Lockups[1].Amount.String(), "2000") + require.Equal(t, commitments.CommittedTokens[2].Lockups[2].Amount.String(), "3000") + } func TestCommitments_WithdrawCommitedTokens(t *testing.T) { @@ -48,3 +57,18 @@ func TestCommitments_WithdrawCommitedTokens(t *testing.T) { err = commitments.DeductFromCommitted("lp/2", sdk.NewInt(200), 100) require.Error(t, err) } + +func TestLockupAmount_WithdrawCommited(t *testing.T) { + commitments := types.Commitments{ + Creator: "", + CommittedTokens: []*types.CommittedTokens{}, + VestingTokens: []*types.VestingTokens{}, + } + + commitments.AddCommittedTokens("lp/1", sdk.NewInt(1000), 1) + commitments.AddCommittedTokens("lp/1", sdk.NewInt(5000), 2) + commitments.AddCommittedTokens("lp/1", sdk.NewInt(3000), 4) + + err := commitments.DeductFromCommitted("lp/1", sdk.NewInt(9000), 3) + require.Error(t, err) +}