const QUOTER_ABI = [ { inputs: [ { internalType: "bytes", name: "path", type: "bytes", }, { internalType: "uint256", name: "amountIn", type: "uint256", }, ], name: "quoteExactInput", outputs: [ { internalType: "uint256", name: "amountOut", type: "uint256", }, { internalType: "uint16[]", name: "fees", type: "uint16[]", }, ], stateMutability: "nonpayable", type: "function", }, ]; const ROUTER_ABI = [ { inputs: [ { components: [ { internalType: "address", name: "tokenIn", type: "address", }, { internalType: "address", name: "tokenOut", type: "address", }, { internalType: "address", name: "recipient", type: "address", }, { internalType: "uint256", name: "deadline", type: "uint256", }, { internalType: "uint256", name: "amountIn", type: "uint256", }, { internalType: "uint256", name: "amountOutMinimum", type: "uint256", }, { internalType: "uint160", name: "limitSqrtPrice", type: "uint160", }, ], internalType: "struct ISwapRouter.ExactInputSingleParams", name: "params", type: "tuple", }, ], name: "exactInputSingle", outputs: [ { internalType: "uint256", name: "amountOut", type: "uint256", }, ], stateMutability: "payable", type: "function", }, { inputs: [ { internalType: "uint256", name: "amountMinimum", type: "uint256", }, { internalType: "address", name: "recipient", type: "address", }, ], name: "unwrapWETH9", outputs: [], stateMutability: "payable", type: "function", }, { inputs: [ { internalType: "bytes[]", name: "data", type: "bytes[]", }, ], name: "multicall", outputs: [ { internalType: "bytes[]", name: "results", type: "bytes[]", }, ], stateMutability: "payable", type: "function", }, ]; const { updater, routerAddress, quoterAddress, multicallAddress, wethAddress, inputCurrency, outputCurrency, inputCurrencyAmount, onLoad, slippage, account, prices, } = props; useEffect(() => { if (!updater || !prices) return; if ( (!inputCurrency.address && !inputCurrency.isNative) || (!outputCurrency.address && !outputCurrency.isNative) || !inputCurrencyAmount ) { return; } const wrapType = inputCurrency.isNative && outputCurrency.address === wethAddress ? 1 : inputCurrency.address === wethAddress && outputCurrency.isNative ? 2 : 0; if (wrapType) { onLoad({ outputCurrencyAmount: inputCurrencyAmount, noPair: false, }); return; } const amount = ethers.utils.parseUnits( Big(inputCurrencyAmount || 0).toFixed(inputCurrency.decimals), inputCurrency.decimals ); const path = [ inputCurrency.address === "native" ? wethAddress : inputCurrency.address, outputCurrency.address === "native" ? wethAddress : outputCurrency.address, ]; const Iface = new ethers.utils.Interface(QUOTER_ABI); const getAmountOut = () => { const pathBytes = "0x" + path.map((address) => address.substr(2)).join(""); const encodedData = Iface.encodeFunctionData("quoteExactInput", [ pathBytes, amount, ]); Ethers.provider() .call({ to: quoterAddress, data: encodedData, }) .then((res) => { const data = Iface.decodeFunctionResult("quoteExactInput", res); getTransaction({ amountOut: data.amountOut }); }) .catch((err) => { onLoad({ noPair: true, outputCurrencyAmount: "", }); }); }; const getTransaction = (result) => { const RouterIface = new ethers.utils.Interface(ROUTER_ABI); const deadline = Math.ceil(Date.now() / 1000) + 60; const options = { gasLimit: 500000, value: inputCurrency.isNative ? amount : "0", }; const _amountOut = Big(result.amountOut) .mul(1 - (slippage || 0.05)) .toFixed(0); const inputs = [ { tokenIn: path[0], tokenOut: path[1], recipient: outputCurrency.isNative ? routerAddress : account, deadline: deadline, amountIn: amount, amountOutMinimum: _amountOut, limitSqrtPrice: "0", }, ]; const multicallParams = []; const encodedDataCallSwap = RouterIface.encodeFunctionData( "exactInputSingle", inputs ); multicallParams.push(encodedDataCallSwap); if (outputCurrency.isNative) { multicallParams.push( RouterIface.encodeFunctionData("unwrapWETH9", ["0", account]) ); } const RouterContract = new ethers.Contract( routerAddress, ROUTER_ABI, Ethers.provider().getSigner() ); const _amount = Big( ethers.utils.formatUnits(result.amountOut, outputCurrency.decimals) ); let priceImpact = null; if (prices) { const isReverse = Number(path[0]) > Number(path[1]); const poolPrice = Big( prices[isReverse ? inputCurrency.symbol : outputCurrency.symbol] || 0 ).div( prices[!isReverse ? inputCurrency.symbol : outputCurrency.symbol] || 0 ); const amountoutPrice = !isReverse ? Big(inputCurrencyAmount).div(_amount) : Big(_amount).div(inputCurrencyAmount); priceImpact = poolPrice .minus(amountoutPrice) .div(poolPrice) .mul(100) .toString(); } const returnData = { outputCurrencyAmount: Big(_amount).gt(0.01) ? Big(_amount).toPrecision(10) : Big(_amount).toFixed(10), priceImpact, noPair: false, }; const getTx = (gas) => { RouterContract.populateTransaction .multicall(multicallParams, { ...options, gasLimit: gas }) .then((res) => { onLoad({ ...returnData, gas, unsignedTx: res, }); }) .catch((err) => { onLoad({ gas, ...returnData, }); }); }; RouterContract.estimateGas .multicall(multicallParams, options) .then((gas) => { getTx(gas); }) .catch((err) => { onLoad({ ...returnData, }); }); }; getAmountOut(); }, [updater, prices]);