---
# System prepended metadata

title: 2023-02-01 testSwapYForXGivenY debugging

---

# 2023-02-01 testSwapYForXGivenY debugging

```solidity
function testSwapYForXGivenY() public {
	
    uint160 sqrtP = 1 << 95;
    uint256 yToSpend = 5;
    (uint256 x, uint256 spentY) = swapper.swapYForXGivenYExt(yToSpend, Price.wrap(sqrtP));

    console2.log(spentY);

    assertTrue(x > 0);
}
```
[FAIL. Reason: Arithmetic over/underflow] testSwapYForXGivenY() (gas: 62815)

Problem: when accumulating x, more y is being spent than what is passed in

In `swapYForXGivenY`: 
```solidity
uint256 usedY = SwapMath.calcYFromPriceDelta(newSP, sCache.sqrtP, liq, true);

uint256 feeAmount = fCache.feeCalc.calcFeeAmount(sCache.mLiq, sCache.tLiq, usedY);

console2.log("usedY: %d ", usedY);
// usedY: 49500000000000000000

console2.log("feeAmt: %d ", feeAmount);
// feeAmt: 10033483146067417

console2.log("y: %d ", y);
// y: 5
  

y -= usedY + feeAmount; // overflows

```

Test fails with underflow for y<100e18, passes when over 100e18 (test just looks for any x to be sent back)

_Initial values:_
* mLiq: 100e18
* tLiq: 1e18
* sqrtP: Price.wrap(1 << 96)



When swapping x for y given x, we can swap 5 x for y without overflow. 
```
usedX: 4
feeamt: 1
x: 5
```
Q: why do we pass in y, calculate an effective y balance from fees and a price chance from that, and then once again try and calculate how much y we're swapping for from that price change?


Solution to overflow in test(?) : price being passed in was far too less than the actual price, so when calulating y from the price delta the value was far higher than the amount passed in




