const amounts = [AmountIn,AmountOut]
const reserves = [ReserveIn,ReserveOut]
const tolerance = 6n//你自己的滑点
const calculateAmountInMax = (amounts, reserves, tolerance) => {
const amountsWithFee = [
amounts[0] * (10000n - router.fee - tolerance) / 10000n,
amounts[1]
];
const k = reserves[0] * reserves[1];
const a = amountsWithFee[1];
const b = amountsWithFee[1] * amountsWithFee[0];
const c = -(k * amountsWithFee[0]);
const x = (-b + sqrt(b * b - 4n * a * c)) / (2n * a);
return x - reserves[0];
};