/* eslint-disable @typescript-eslint/no-non-null-assertion */ import { constants } from "@gelatonetwork/limit-orders-lib"; import { isTransactionCostDependentChain } from "@gelatonetwork/limit-orders-lib/dist/utils"; import { CurrencyAmount } from "@uniswap/sdk-core"; import { formatUnits } from "@ethersproject/units"; import React, { useMemo } from "react"; import { useGelatoLimitOrders } from "../../hooks/gelato"; import useGelatoLimitOrdersLib from "../../hooks/gelato/useGelatoLimitOrdersLib"; import useGasOverhead from "../../hooks/useGasOverhead"; import useTheme from "../../hooks/useTheme"; import { Rate } from "../../state/gorder/actions"; import { TYPE } from "../../theme"; import { useWeb3 } from "../../web3"; import { AutoColumn } from "../Column"; import { RowBetween, RowFixed } from "../Row"; import { MouseoverTooltip } from "../Tooltip"; export function AdvancedSwapDetails() { const theme = useTheme(); const { chainId } = useWeb3(); const { derivedOrderInfo: { parsedAmounts, rawAmounts }, orderState: { rateType }, } = useGelatoLimitOrders(); const library = useGelatoLimitOrdersLib(); const { gasPrice, realExecutionPriceAsString } = useGasOverhead( parsedAmounts.input, parsedAmounts.output, rateType ); const isInvertedRate = rateType === Rate.DIV; const realExecutionRateWithSymbols = useMemo( () => parsedAmounts.input?.currency && parsedAmounts.output?.currency && realExecutionPriceAsString ? realExecutionPriceAsString === "never executes" ? realExecutionPriceAsString : `1 ${ isInvertedRate ? parsedAmounts.output.currency.symbol : parsedAmounts.input.currency.symbol } = ${realExecutionPriceAsString} ${ isInvertedRate ? parsedAmounts.input.currency.symbol : parsedAmounts.output.currency.symbol }` : undefined, [parsedAmounts, realExecutionPriceAsString, isInvertedRate] ); const outputAmount = parsedAmounts.output; const rawOutputAmount = rawAmounts.output ?? "0"; const { minReturn, slippagePercentage, gelatoFeePercentage } = useMemo(() => { if (!outputAmount || !library || !chainId) return { minReturn: undefined, slippagePercentage: undefined, gelatoFeePercentage: undefined, }; // if (utils.isTransactionCostDependentChain(chainId)) // return { // minReturn: outputAmount, // slippagePercentage: undefined, // gelatoFeePercentage: undefined, // }; const { minReturn } = library.getFeeAndSlippageAdjustedMinReturn( rawOutputAmount ); const slippagePercentage = library.slippageBPS / 100; const gelatoFeePercentage = library.gelatoFeeBPS / 100; const minReturnParsed = CurrencyAmount.fromRawAmount( outputAmount.currency, minReturn ); return { minReturn: minReturnParsed, slippagePercentage, gelatoFeePercentage, }; }, [outputAmount, chainId, library, rawOutputAmount]); const expiryDate = new Date( new Date().getTime() + constants.MAX_LIFETIME_IN_SECONDS * 1000 ).toLocaleString([], { year: "numeric", month: "2-digit", day: "numeric", }); return !chainId ? null : ( {!isTransactionCostDependentChain(chainId) ? ( <> Gelato Fee {gelatoFeePercentage ? `${gelatoFeePercentage}` : "-"}% Slippage {slippagePercentage ? `${slippagePercentage}` : "-"}% ) : ( <> Gas Price {gasPrice ? `${parseFloat(formatUnits(gasPrice, "gwei")).toFixed(0)} GWEI` : "-"} Real Execution Price (?) {" "} {realExecutionRateWithSymbols ? `${realExecutionRateWithSymbols}` : "-"} Gelato Fee {gelatoFeePercentage ? `${gelatoFeePercentage}` : "-"}% Slippage {slippagePercentage ? `${slippagePercentage}` : "-"}% )} Minimum Received (?) {minReturn ? `${minReturn.toSignificant(4)} ${ outputAmount ? outputAmount.currency.symbol : "-" }` : "-"} Expiration date (?) {expiryDate} ); }