Submitted by givn, also found by 0xAlix2, bareli, and den-sosnowsky
https://github.com/code-423n4/2025-04-cabal/blob/5b5f92ab4f95e5f9f405bbfa252860472d164705/sources/pool_router.move#L386-L420
When users unstake their LP tokens they call initiate_unstake for the required amount. This creates UnbondingEntry and increases the pending unstake amount - unstaked_pending_amounts[unstaking_type] + unbonding_amount.
At some point an admin (or user) will invoke batch_undelegate_pending_lps():
The pool_router::unlock calculates what % of every pool should be undelegated so that the desired LP token amount is reached. This happens by calculating a fraction, iterating over the pools and subtracting an amount equal to that fraction. The issue is that when the last pool element is reached, the remaining amount is all removed from there:
This means that if the last pool is empty or with insufficient funds an underflow will occur here:
The protocol tries to always fund the pool with least staked tokens by using get_most_underutilized_pool, but this does not prevent situations of imbalance, like:
Place this test in pool_router.move. Run it with yarn run test- test_unlock_lp_amounts.
Instead of doing one iteration over the pools and subtracting the remaining amount from the last one, use an loop and modulo arithmetic to iterate multiple times and subtract any possible remaining amounts from the other pools.
Separate undelegate amount calculation from the stargate calls so that multiple MsgUndelegate messages are not sent for the same validator.
