def Y_to_X(self, dY, fee=None):
    assert (dY >= 0)
    if (fee is None):
        fee = self.fee
    if (self.L == 0):
        self.i_a = self.tick(self.srP)
        return XY()
    fees = XY(0, (dY * fee))
    srp_new = (self.srP + ((dY - fees.y) / self.L))
    i_u = self.ticks[self.i_l].i_next
    tick_new = self.tick(srp_new)
    if (tick_new < i_u):
        dX = ((- (dY - fees.y)) / (self.srP * srp_new))
        self.srP = srp_new
        self.i_a = tick_new
        user = XY((- dX), (- dY))
        self.balance -= user
        self.feeGrowth += (fees * (1.0 / self.L))
        return user
    else:
        self.i_l = i_u
        srP_u = self.srp(i_u)
        dY_ = (self.L * (srP_u - self.srP))
        dX = ((- dY_) / (self.srP * srP_u))
        tmp = (dY_ / (1.0 - fee))
        (dY_, fees) = (tmp, XY(0, (tmp - dY_)))
        self.feeGrowth += (fees * (1.0 / self.L))
        self.L += self.ticks[i_u].Delta_L
        self.ticks[i_u].feeGrowthOutside = (self.feeGrowth - self.ticks[i_u].feeGrowthOutside)
        self.srP = srP_u
        user = XY((- dX), (- dY_))
        self.balance -= user
        return (user + self.Y_to_X((dY - dY_), fee))
def Y_to_X(self, dY, fee=None):
    assert (dY >= 0)
    if (fee is None):
        fee = self.fee
    if (self.L == 0):
        self.i_a = self.tick(self.srP)
        return XY()
    fees = XY(0, (dY * fee))
    srp_new = (self.srP + ((dY - fees.y) / self.L))
    i_u = self.ticks[self.i_l].i_next
    tick_new = self.tick(srp_new)
    if (tick_new < i_u):
        dX = ((- (dY - fees.y)) / (self.srP * srp_new))
        self.srP = srp_new
        self.i_a = tick_new
        user = XY((- dX), (- dY))
        self.balance -= user
        self.feeGrowth += (fees * (1.0 / self.L))
        return user
    else:
        self.i_l = i_u
        srP_u = self.srp(i_u)
        dY_ = (self.L * (srP_u - self.srP))
        dX = ((- dY_) / (self.srP * srP_u))
        tmp = (dY_ / (1.0 - fee))
        (dY_, fees) = (tmp, XY(0, (tmp - dY_)))
        self.feeGrowth += (fees * (1.0 / self.L))
        self.L += self.ticks[i_u].Delta_L
        self.ticks[i_u].feeGrowthOutside = (self.feeGrowth - self.ticks[i_u].feeGrowthOutside)
        self.srP = srP_u
        user = XY((- dX), (- dY_))
        self.balance -= user
        return (user + self.Y_to_X((dY - dY_), fee))