pub fn compute_y(
        &self, 
        x_c_amount: Balance, // new x_token amount in comparable precision, 
        current_c_amounts: &Vec<Balance>,  // in-pool tokens amount in comparable precision,
        index_x: usize, // x token's index
        index_y: usize, // y token's index
    ) -> Option<U256> {
        let n_coins = current_c_amounts.len() as u128;
        let amp_factor = self.compute_amp_factor()?;
        let ann = amp_factor.checked_mul(n_coins.checked_pow(n_coins as u32)?.into())?;
        // invariant
        let d = self.compute_d(current_c_amounts)?;
        let mut s_ = x_c_amount;
        let mut c = d.checked_mul(d)?.checked_div(x_c_amount.into())?;
        for (idx, c_amount) in current_c_amounts.iter().enumerate() {
            if idx != index_x && idx != index_y {
                s_ += *c_amount;
                c = c.checked_mul(d)?
                    .checked_div((*c_amount).into())?;
            }
        }
        c = c
            .checked_mul(d)?
            .checked_div(ann.checked_mul((n_coins as u128).checked_pow(n_coins as u32)?.into())?.into())?;

        let b = d.checked_div(ann.into())?.checked_add(s_.into())?; // d will be subtracted later

        // Solve for y by approximating: y**2 + b*y = c
        let mut y_prev: U256;
        let mut y = d;
        for _ in 0..256 {
            y_prev = y;
            // $ y_{k+1} = \frac{y_k^2 + c}{2y_k + b - D} $
            let y_numerator = y.checked_pow(2.into())?.checked_add(c)?;
            let y_denominator = y.checked_mul(2.into())?.checked_add(b)?.checked_sub(d)?;
            y = y_numerator.checked_div(y_denominator)?;
            if y > y_prev {
                if y.checked_sub(y_prev)? <= 1.into() {
                    break;
                }
            } else if y_prev.checked_sub(y)? <= 1.into() {
                break;
            }
        }
        Some(y)
    }
/// Compute new amount of token 'y' with new amount of token 'x'
/// return new y_token amount according to the equation
Compute new amount of token 'y' with new amount of token 'x'
return new y_token amount according to the equation
Compute new amount of token ' y ' with new amount of token ' x ' return new y_token amount according to the equation
pub fn compute_y(
        &self, 
        x_c_amount: Balance, // new x_token amount in comparable precision, 
        current_c_amounts: &Vec<Balance>,  // in-pool tokens amount in comparable precision,
        index_x: usize, // x token's index
        index_y: usize, // y token's index
    ) -> Option<U256> {
        let n_coins = current_c_amounts.len() as u128;
        let amp_factor = self.compute_amp_factor()?;
        let ann = amp_factor.checked_mul(n_coins.checked_pow(n_coins as u32)?.into())?;
        // invariant
        let d = self.compute_d(current_c_amounts)?;
        let mut s_ = x_c_amount;
        let mut c = d.checked_mul(d)?.checked_div(x_c_amount.into())?;
        for (idx, c_amount) in current_c_amounts.iter().enumerate() {
            if idx != index_x && idx != index_y {
                s_ += *c_amount;
                c = c.checked_mul(d)?
                    .checked_div((*c_amount).into())?;
            }
        }
        c = c
            .checked_mul(d)?
            .checked_div(ann.checked_mul((n_coins as u128).checked_pow(n_coins as u32)?.into())?.into())?;
        let b = d.checked_div(ann.into())?.checked_add(s_.into())?; // d will be subtracted later
        // Solve for y by approximating: y**2 + b*y = c
        let mut y_prev: U256;
        let mut y = d;
        for _ in 0..256 {
            y_prev = y;
            // $ y_{k+1} = \frac{y_k^2 + c}{2y_k + b - D} $
            let y_numerator = y.checked_pow(2.into())?.checked_add(c)?;
            let y_denominator = y.checked_mul(2.into())?.checked_add(b)?.checked_sub(d)?;
            y = y_numerator.checked_div(y_denominator)?;
            if y > y_prev {
                if y.checked_sub(y_prev)? <= 1.into() {
                    break;
                }
            } else if y_prev.checked_sub(y)? <= 1.into() {
                break;
            }
        }
        Some(y)
    }
pub fn compute_y ( & self , x_c_amount : Balance , // new x_token amount in comparable precision,  current_c_amounts : & Vec < Balance > , // in-pool tokens amount in comparable precision, index_x : usize , // x token's index index_y : usize , // y token's index ) -> Option < U256 > { let n_coins = current_c_amounts . len ( ) as u128 ; let amp_factor = self . compute_amp_factor ( ) ? ; let ann = amp_factor . checked_mul ( n_coins . checked_pow ( n_coins as u32 ) ? . into ( ) ) ? ; // invariant let d = self . compute_d ( current_c_amounts ) ? ; let mut s_ = x_c_amount ; let mut c = d . checked_mul ( d ) ? . checked_div ( x_c_amount . into ( ) ) ? ; for ( idx , c_amount ) in current_c_amounts . iter ( ) . enumerate ( ) { if idx != index_x && idx != index_y { s_ += * c_amount ; c = c . checked_mul ( d ) ? . checked_div ( ( * c_amount ) . into ( ) ) ? ; } } c = c . checked_mul ( d ) ? . checked_div ( ann . checked_mul ( ( n_coins as u128 ) . checked_pow ( n_coins as u32 ) ? . into ( ) ) ? . into ( ) ) ? ; let b = d . checked_div ( ann . into ( ) ) ? . checked_add ( s_ . into ( ) ) ? ; // d will be subtracted later // Solve for y by approximating: y**2 + b*y = c let mut y_prev : U256 ; let mut y = d ; for _ in 0 .. 256 { y_prev = y ; // $ y_{k+1} = \frac{y_k^2 + c}{2y_k + b - D} $ let y_numerator = y . checked_pow ( 2 . into ( ) ) ? . checked_add ( c ) ? ; let y_denominator = y . checked_mul ( 2 . into ( ) ) ? . checked_add ( b ) ? . checked_sub ( d ) ? ; y = y_numerator . checked_div ( y_denominator ) ? ; if y > y_prev { if y . checked_sub ( y_prev ) ? <= 1 . into ( ) { break ; } } else if y_prev . checked_sub ( y ) ? <= 1 . into ( ) { break ; } } Some ( y ) }
Compute new amount of token 'y' with new amount of token 'x'
return new y_token amount according to the equation
Compute new amount of token ' y ' with new amount of token ' x ' return new y_token amount according to the equation
&self
x_c_amount
Balance
current_c_amounts
&Vec<Balance>
index_x
usize
index_y
usize
&self
x_c_amount
Balance
current_c_amounts
&Vec<Balance>
index_x
usize
index_y
usize