Skip to content

Commit

Permalink
cleanup expired lockups
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed May 16, 2024
1 parent 974dc2b commit 0d53584
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
4 changes: 2 additions & 2 deletions x/amm/keeper/pool_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (k Keeper) MintPoolShareToAccount(ctx sdk.Context, pool types.Pool, addr sd
}

// Commit LP token minted
lockUntil := uint64(ctx.BlockTime().Unix())
lockUntil := uint64(0)
if pool.PoolParams.UseOracle {
lockUntil += uint64(time.Hour.Seconds())
lockUntil = uint64(ctx.BlockTime().Unix()) + uint64(time.Hour.Seconds())
}

err = k.commitmentKeeper.CommitLiquidTokens(ctx, addr, poolShareDenom, amount, lockUntil)
Expand Down
56 changes: 25 additions & 31 deletions x/commitment/types/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,28 @@ func (c *Commitments) AddCommittedTokens(denom string, amount math.Int, unlockTi
for i, token := range c.CommittedTokens {
if token.Denom == denom {
c.CommittedTokens[i].Amount = token.Amount.Add(amount)
c.CommittedTokens[i].Lockups = append(token.Lockups, Lockup{
Amount: amount,
UnlockTimestamp: unlockTime,
})
if unlockTime != 0 {
c.CommittedTokens[i].Lockups = append(token.Lockups, Lockup{
Amount: amount,
UnlockTimestamp: unlockTime,
})
}
return
}
}
c.CommittedTokens = append(c.CommittedTokens, &CommittedTokens{
Denom: denom,
Amount: amount,
Lockups: []Lockup{
{
Amount: amount,
UnlockTimestamp: unlockTime,
},
},
})

committedToken := &CommittedTokens{
Denom: denom,
Amount: amount,
Lockups: []Lockup{},
}
if unlockTime != 0 {
committedToken.Lockups = append(committedToken.Lockups, Lockup{
Amount: amount,
UnlockTimestamp: unlockTime,
})
}
c.CommittedTokens = append(c.CommittedTokens, committedToken)
}

func (c *Commitments) DeductFromCommitted(denom string, amount math.Int, currTime uint64) error {
Expand All @@ -67,29 +72,18 @@ func (c *Commitments) DeductFromCommitted(denom string, amount math.Int, currTim
return ErrInsufficientCommittedTokens
}

withdrawnTokens := sdk.ZeroInt()
newLockups := []Lockup{}

lockedAmount := sdk.ZeroInt()
for _, lockup := range token.Lockups {
if withdrawnTokens.LT(amount) {
if lockup.UnlockTimestamp <= currTime {
withdrawAmount := lockup.Amount
if withdrawAmount.GT(amount.Sub(withdrawnTokens)) {
withdrawAmount = amount.Sub(withdrawnTokens)
newLockups = append(newLockups, Lockup{
Amount: lockup.Amount.Sub(withdrawAmount),
UnlockTimestamp: lockup.UnlockTimestamp,
})
}
withdrawnTokens = withdrawnTokens.Add(withdrawAmount)
} else {
return ErrInsufficientWithdrawableTokens
}
} else {
if lockup.UnlockTimestamp > currTime {
newLockups = append(newLockups, lockup)
lockedAmount = lockedAmount.Add(lockup.Amount)
}
}
c.CommittedTokens[i].Lockups = newLockups
if lockedAmount.GT(c.CommittedTokens[i].Amount) {
return ErrInsufficientWithdrawableTokens
}
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/stablestake/keeper/msg_server_bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (k msgServer) Bond(goCtx context.Context, msg *types.MsgBond) (*types.MsgBo
}

// Commit LP token
err = k.commitmentKeeper.CommitLiquidTokens(ctx, sender, shareDenom, shareAmount, uint64(ctx.BlockTime().Unix()))
err = k.commitmentKeeper.CommitLiquidTokens(ctx, sender, shareDenom, shareAmount, 0)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0d53584

Please sign in to comment.