maximumAmountIn method

CurrencyAmount maximumAmountIn(
  1. Percent slippageTolerance
)

Get the maximum amount in that can be spent via this trade for the given slippage tolerance @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade

Implementation

CurrencyAmount maximumAmountIn(Percent slippageTolerance) {
  invariant(!(slippageTolerance.denominator < ZERO), 'SLIPPAGE_TOLERANCE');
  invariant(!(slippageTolerance.numerator < ZERO), 'SLIPPAGE_TOLERANCE');
  final inputAmountOutFraction = Fraction(inputAmount.raw);
  if (tradeType == TradeType.EXACT_INPUT) {
    return inputAmount;
  } else {
    final slippageAdjustedAmountIn = Fraction(ONE)
        .add(slippageTolerance)
        .multiply(inputAmountOutFraction)
        .quotient();
    return inputAmount is TokenAmount
        ? TokenAmount(
            (inputAmount as TokenAmount).token, slippageAdjustedAmountIn)
        : CurrencyAmount.ether(
            slippageAdjustedAmountIn,
          );
  }
}