for (i in 0..vector::length(&m_store.unbond_period)) {
	// undelegate
	pool_router::unlock(m_store.stake_token_metadata[i], m_store.unstaked_pending_amounts[i]);
	// clear pending
	m_store.unstaked_pending_amounts[i] = 0;
};
let temp_amount = if (i == vector::length(&pools) - 1) {
	remain_amount
} else {
	bigdecimal::mul_by_u64_truncate(ratio, temp_pool.amount)
};
remain_amount = remain_amount - temp_amount;
temp_pool.amount = temp_pool.amount - temp_amount;
#[test, expected_failure()]
public fun test_unlock_lp_amounts() {
	let unlock_amount = 2_000_000u64; // Unlock LP

	let pools = vector[ // LP staked in each pool
		20_000_000, 
		20_000_000,
		10_000
	];

	let i = 20;
	loop {
		debug::print(&string::utf8(b"Begin undelegation round"));
		pools = calculate_undelegates(pools, unlock_amount);
		i = i - 1;
		debug::print(&string::utf8(b""));
		if(i == 0) {
			break;
		}
	};

	// Pool amounts after last iteration
	// [debug] "New pool stake amounts"
	// [debug] 4500
	// [debug] 4500
	// [debug] 0

	// Now we continue undelegating smaller amounts, but action will underflow
	debug::print(&string::utf8(b" ---- Undelegate smaller amount #1 ---- "));
	pools = calculate_undelegates(pools, 1_000);
	debug::print(&string::utf8(b" ---- Undelegate smaller amount #2 ---- "));
	pools = calculate_undelegates(pools, 1_000);
}

/// Simplified version of pool_router::unlock_lp
#[test_only]
fun calculate_undelegates(pools: vector<u64>, unlock_amount: u64): vector<u64> {
	let pools_length = vector::length(&pools);
	let total_stakes = vector::fold(pools, 0u64, |acc, elem| acc + elem); // LP staked in across all pools
	let remain_amount: u64 = unlock_amount;
	let ratio = bigdecimal::from_ratio_u64(unlock_amount, total_stakes);

	debug::print(&string::utf8(b"Total staked before undelegate"));
	debug::print(&total_stakes);

	assert!(total_stakes >= unlock_amount, 1000777);

	for (i in 0..pools_length) {
		let pool_stake = vector::borrow_mut(&mut pools, i);
		let undelegate_amount = if (i == pools_length - 1) {
			remain_amount
		} else {
			bigdecimal::mul_by_u64_truncate(ratio, *pool_stake)
		};
		remain_amount = remain_amount - undelegate_amount;

		// Update state tracking
		*pool_stake = *pool_stake - undelegate_amount;
	};

	debug::print(&string::utf8(b"New pool stake amounts"));
	let total_staked_after_undelegate = vector::fold(pools, 0u64, |acc, elem| {
		debug::print(&elem);
		acc + elem
	});

	debug::print(&string::utf8(b"Total staked after undelegate"));
	debug::print(&total_staked_after_undelegate);
	pools
}
