    pub fn compute_d(amp_factor: &u64, deposits: &[Coin]) -> Option<Uint512> {
        let n_coins = Uint128::from(deposits.len() as u128);

        // sum(x_i), a.k.a S
        let sum_x = deposits
            .iter()
            .fold(Uint128::zero(), |acc, x| acc.checked_add(x.amount).unwrap());

        if sum_x == Uint128::zero() {
            Some(Uint512::zero())
        } else {
            // do as below but for a generic number of assets
            let amount_times_coins: Vec<Uint128> = deposits
                .iter()
                .map(|coin| coin.amount.checked_mul(n_coins).unwrap())
                .collect();

            // Newton's method to approximate D
            let mut d_prev: Uint512;
            let mut d: Uint512 = sum_x.into();
            for _ in 0..256 {
                let mut d_prod = d;
                for amount in amount_times_coins.clone().into_iter() {
                    d_prod = d_prod
                        .checked_mul(d)
                        .unwrap()
                        .checked_div(amount.into())
                        .unwrap();
                }
                d_prev = d;
                d = compute_next_d(amp_factor, d, d_prod, sum_x, n_coins).unwrap();
                // Equality with the precision of 1
                if d > d_prev {
                    if d.checked_sub(d_prev).unwrap() <= Uint512::one() {
                        break;
                    }
                } else if d_prev.checked_sub(d).unwrap() <= Uint512::one() {
                    break;
                }
            }

->          Some(d)
        }
    }
        d = compute_next_d(amp_factor, d, d_prod, sum_x, n_coins).unwrap();
        // Equality with the precision of 1
        if d > d_prev {
            if d.checked_sub(d_prev).unwrap() <= Uint512::one() {
-               break;
+               return Some(d);
            }
        } else if d_prev.checked_sub(d).unwrap() <= Uint512::one() {
-               break;
+               return Some(d);
        }
    }

-   Some(d)
+   Err(ContractError::ConvergeError)
