def X_to_Y(self, dX, fee=None):
    assert (dX >= 0)
    if (fee is None):
        fee = self.fee
    if (self.L == 0):
        self.i_a = self.tick(self.srP)
        return XY()
    fees = XY((dX * fee), 0)
    srp_new = (1.0 / ((1.0 / self.srP) + ((dX - fees.x) / self.L)))
    i_l = self.i_l
    tick_new = self.tick(srp_new)
    if (tick_new >= i_l):
        dY = (((- (dX - fees.x)) * 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 = self.ticks[self.i_l].i_prev
        srP_l = self.srp(i_l)
        dY = (self.L * (srP_l - self.srP))
        dX_ = ((- dY) / (self.srP * srP_l))
        tmp = (dX_ / (1.0 - fee))
        (dX_, fees) = (tmp, XY((tmp - dX_), 0))
        self.feeGrowth += (fees * (1.0 / self.L))
        self.L -= self.ticks[i_l].Delta_L
        self.ticks[i_l].feeGrowthOutside = (self.feeGrowth - self.ticks[i_l].feeGrowthOutside)
        self.srP = (self.srp(i_l) - 1e-16)
        user = XY((- dX_), (- dY))
        self.balance -= user
        return (user + self.X_to_Y((dX - dX_), fee))
def X_to_Y(self, dX, fee=None):
    assert (dX >= 0)
    if (fee is None):
        fee = self.fee
    if (self.L == 0):
        self.i_a = self.tick(self.srP)
        return XY()
    fees = XY((dX * fee), 0)
    srp_new = (1.0 / ((1.0 / self.srP) + ((dX - fees.x) / self.L)))
    i_l = self.i_l
    tick_new = self.tick(srp_new)
    if (tick_new >= i_l):
        dY = (((- (dX - fees.x)) * 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 = self.ticks[self.i_l].i_prev
        srP_l = self.srp(i_l)
        dY = (self.L * (srP_l - self.srP))
        dX_ = ((- dY) / (self.srP * srP_l))
        tmp = (dX_ / (1.0 - fee))
        (dX_, fees) = (tmp, XY((tmp - dX_), 0))
        self.feeGrowth += (fees * (1.0 / self.L))
        self.L -= self.ticks[i_l].Delta_L
        self.ticks[i_l].feeGrowthOutside = (self.feeGrowth - self.ticks[i_l].feeGrowthOutside)
        self.srP = (self.srp(i_l) - 1e-16)
        user = XY((- dX_), (- dY))
        self.balance -= user
        return (user + self.X_to_Y((dX - dX_), fee))