const ROUTER_ABI = [ { inputs: [ { internalType: "enum IVault.SwapKind", name: "kind", type: "uint8", }, { components: [ { internalType: "bytes32", name: "poolId", type: "bytes32" }, { internalType: "uint256", name: "assetInIndex", type: "uint256", }, { internalType: "uint256", name: "assetOutIndex", type: "uint256", }, { internalType: "uint256", name: "amount", type: "uint256" }, { internalType: "bytes", name: "userData", type: "bytes" }, ], internalType: "struct IVault.BatchSwapStep[]", name: "swaps", type: "tuple[]", }, { internalType: "contract IAsset[]", name: "assets", type: "address[]", }, { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "bool", name: "fromInternalBalance", type: "bool", }, { internalType: "address payable", name: "recipient", type: "address", }, { internalType: "bool", name: "toInternalBalance", type: "bool", }, ], internalType: "struct IVault.FundManagement", name: "funds", type: "tuple", }, ], name: "queryBatchSwap", outputs: [{ internalType: "int256[]", name: "", type: "int256[]" }], stateMutability: "nonpayable", type: "function", }, { inputs: [ { internalType: "enum IVault.SwapKind", name: "kind", type: "uint8", }, { components: [ { internalType: "bytes32", name: "poolId", type: "bytes32", }, { internalType: "uint256", name: "assetInIndex", type: "uint256", }, { internalType: "uint256", name: "assetOutIndex", type: "uint256", }, { internalType: "uint256", name: "amount", type: "uint256", }, { internalType: "bytes", name: "userData", type: "bytes", }, ], internalType: "struct IVault.BatchSwapStep[]", name: "swaps", type: "tuple[]", }, { internalType: "contract IAsset[]", name: "assets", type: "address[]", }, { components: [ { internalType: "address", name: "sender", type: "address", }, { internalType: "bool", name: "fromInternalBalance", type: "bool", }, { internalType: "address payable", name: "recipient", type: "address", }, { internalType: "bool", name: "toInternalBalance", type: "bool", }, ], internalType: "struct IVault.FundManagement", name: "funds", type: "tuple", }, { internalType: "int256[]", name: "limits", type: "int256[]", }, { internalType: "uint256", name: "deadline", type: "uint256", }, ], name: "batchSwap", outputs: [ { internalType: "int256[]", name: "assetDeltas", type: "int256[]", }, ], stateMutability: "payable", type: "function", }, { inputs: [ { components: [ { internalType: "bytes32", name: "poolId", type: "bytes32" }, { internalType: "enum IVault.SwapKind", name: "kind", type: "uint8", }, { internalType: "contract IAsset", name: "assetIn", type: "address", }, { internalType: "contract IAsset", name: "assetOut", type: "address", }, { internalType: "uint256", name: "amount", type: "uint256" }, { internalType: "bytes", name: "userData", type: "bytes" }, ], internalType: "struct IVault.SingleSwap", name: "singleSwap", type: "tuple", }, { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "bool", name: "fromInternalBalance", type: "bool", }, { internalType: "address payable", name: "recipient", type: "address", }, { internalType: "bool", name: "toInternalBalance", type: "bool", }, ], internalType: "struct IVault.FundManagement", name: "funds", type: "tuple", }, { internalType: "uint256", name: "limit", type: "uint256" }, { internalType: "uint256", name: "deadline", type: "uint256" }, ], name: "swap", outputs: [ { internalType: "uint256", name: "amountCalculated", type: "uint256", }, ], stateMutability: "payable", type: "function", }, ]; const { updater, routerAddress, wethAddress, inputCurrency, outputCurrency, inputCurrencyAmount, onLoad, slippage, account, fees, prices, pools, poolsGraph, } = props; useEffect(() => { if (!updater || !prices) return; if ( (!inputCurrency.address && !inputCurrency.isNative) || (!outputCurrency.address && !outputCurrency.isNative) || !inputCurrencyAmount ) { return; } const path = [ inputCurrency.address === "native" ? wethAddress : inputCurrency.address, outputCurrency.address === "native" ? wethAddress : outputCurrency.address, ]; const RouterContract = new ethers.Contract( routerAddress, ROUTER_ABI, Ethers.provider().getSigner() ); const finalPool = pools ?.filter( (poolData) => poolData[0].includes(path[0].toLowerCase()) && poolData[0].includes(path[1].toLowerCase()) ) .map((poolData) => poolData[1]); if (finalPool.length === 0) { onLoad({ noPair: true, outputCurrencyAmount: "", }); return; } const amount = ethers.utils.parseUnits( Big(inputCurrencyAmount || 0).toFixed(inputCurrency.decimals), inputCurrency.decimals ); const _inputAddress = inputCurrency.address === "native" ? wethAddress : inputCurrency.address; const _outputAddress = outputCurrency.address === "native" ? wethAddress : outputCurrency.address; const assets = [_inputAddress, _outputAddress]; const funds = [account, false, account, false]; const swap_steps = [ { poolId: finalPool[0], assetIn: _inputAddress, assetOut: _outputAddress, amount, }, ]; const token_indices = {}; for (let i = 0; i < assets.length; i++) { token_indices[assets[i]] = i; } const swap_steps_struct = []; for (const step of swap_steps) { const swap_step_struct = [ step["poolId"], token_indices[step["assetIn"]], token_indices[step["assetOut"]], step["amount"], "0x", ]; swap_steps_struct.push(swap_step_struct); } const wrapType = inputCurrency.isNative && outputCurrency.address === wethAddress ? 1 : inputCurrency.address === wethAddress && outputCurrency.isNative ? 2 : 0; if (wrapType) { onLoad({ outputCurrencyAmount: inputCurrencyAmount, noPair: false, }); return; } const options = { value: inputCurrency.isNative ? amount : "0", }; const getAmountOut = () => { RouterContract.callStatic .queryBatchSwap(0, swap_steps_struct, assets, funds) .then((res) => { const _amountOut = res[1]?.abs() || Big(0); if (_amountOut.gt(0)) { getTransaction({ amountOut: _amountOut }); } else { onLoad({ noPair: true, outputCurrencyAmount: "", }); } }) .catch((err) => { onLoad({ noPair: true, outputCurrencyAmount: "", }); }); }; const getTransaction = (result) => { const deadline = Math.ceil(Date.now() / 1000) + 60; const _amountOut = Big(result.amountOut) .mul(1 - (slippage || 0.05)) .toFixed(0); const params = [ [finalPool[0], 0, _inputAddress, _outputAddress, amount, "0x"], funds, _amountOut, deadline.toFixed(), ]; const _amount = Big( ethers.utils.formatUnits(result.amountOut, outputCurrency.decimals) ); let priceImpact = null; if (prices) { const poolPrice = Big(prices[inputCurrency.symbol] || 1).div( prices[outputCurrency.symbol] || 1 ); const amountoutPrice = 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 .swap(...params, { ...options, gasLimit: gas }) .then((res) => { onLoad({ ...returnData, gas, unsignedTx: res, }); }) .catch((err) => { onLoad({ ...returnData, gas, }); }); }; RouterContract.estimateGas .swap(...params, options) .then((gas) => { getTx(gas); }) .catch((err) => { getTx(); }); }; getAmountOut(); }, [updater, prices]);