From 27163a0a77a153c735ffa2e80de259f7b430f450 Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:39:54 +0800 Subject: [PATCH] fix: lifi bridges final trade quote (#8348) --- .../getTradeQuote/getTradeQuote.ts | 32 +++++++++++++++---- .../src/swappers/LifiSwapper/utils/types.ts | 9 ++++-- packages/swapper/src/types.ts | 3 +- .../useGetTradeQuotes/getTradeQuoteInput.ts | 3 +- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/packages/swapper/src/swappers/LifiSwapper/getTradeQuote/getTradeQuote.ts b/packages/swapper/src/swappers/LifiSwapper/getTradeQuote/getTradeQuote.ts index cd5569a04cd..36cb50c3b1d 100644 --- a/packages/swapper/src/swappers/LifiSwapper/getTradeQuote/getTradeQuote.ts +++ b/packages/swapper/src/swappers/LifiSwapper/getTradeQuote/getTradeQuote.ts @@ -35,7 +35,7 @@ import { transformLifiStepFeeData } from '../utils/transformLifiFeeData/transfor import type { LifiTradeQuote } from '../utils/types' export async function getTrade( - input: GetEvmTradeQuoteInput & { lifiAllowedTools?: string[] | undefined }, + input: GetEvmTradeQuoteInput, deps: SwapperDeps, lifiChainMap: Map, ): Promise> { @@ -97,9 +97,12 @@ export async function getTrade( // are currently incompatible with our fee calculations, leading to incorrect fee display, // reverts, partial swaps, wrong received tokens (due to out-of-gas mid-trade), etc. For now, // these bridges are disabled. - bridges: { deny: ['stargate', 'stargateV2', 'stargateV2Bus', 'amarok', 'arbitrum'] }, - ...(lifiAllowedTools && { - exchanges: { allow: lifiAllowedTools }, + bridges: { + deny: ['stargate', 'stargateV2', 'stargateV2Bus', 'amarok', 'arbitrum'], + ...(lifiAllowedTools?.bridges ? { allow: lifiAllowedTools.bridges } : {}), + }, + ...(lifiAllowedTools?.exchanges && { + exchanges: { allow: lifiAllowedTools.exchanges }, }), allowSwitchChain: true, fee: affiliateBpsDecimalPercentage.isZero() @@ -245,13 +248,30 @@ export async function getTrade( .dividedBy(bn(selectedLifiRoute.fromAmount)) .toString() + const lifiBridgeTools = selectedLifiRoute.steps + .filter( + current => current.includedSteps?.some(includedStep => includedStep.type === 'cross'), + ) + .map(current => current.tool) + + const lifiExchangeTools = selectedLifiRoute.steps + .filter( + current => + // A step tool is an exchange (swap) tool if all of its steps are non-cross-chain steps + current.includedSteps?.every(includedStep => includedStep.type !== 'cross'), + ) + .map(current => current.tool) + return { id: selectedLifiRoute.id, // TODO(gomes): when https://github.com/shapeshift/web/pull/8309 goes in, this goes out // We do need receiveAddress in *input* to send it as fromAddress for routes req for more reliable rates, but with receiveAddress currently being the quotes/rates discriminator, // we need to exclude it from method in *output* receiveAddress: input.quoteOrRate === 'quote' ? receiveAddress : undefined, - lifiTools: selectedLifiRoute.steps.map(step => step.tool), + lifiTools: { + bridges: lifiBridgeTools.length ? lifiBridgeTools : undefined, + exchanges: lifiExchangeTools.length ? lifiExchangeTools : undefined, + }, affiliateBps, potentialAffiliateBps, steps, @@ -297,7 +317,7 @@ export async function getTrade( // This isn't a mistake - With Li.Fi, we get the exact same thing back whether quote or rate, however, the input *is* different export const getTradeQuote = ( - input: GetEvmTradeQuoteInputBase & { lifiAllowedTools?: string[] | undefined }, + input: GetEvmTradeQuoteInputBase, deps: SwapperDeps, lifiChainMap: Map, ): Promise> => getTrade(input, deps, lifiChainMap) diff --git a/packages/swapper/src/swappers/LifiSwapper/utils/types.ts b/packages/swapper/src/swappers/LifiSwapper/utils/types.ts index 9bb4f8679f1..386cde78476 100644 --- a/packages/swapper/src/swappers/LifiSwapper/utils/types.ts +++ b/packages/swapper/src/swappers/LifiSwapper/utils/types.ts @@ -2,13 +2,18 @@ import type { Route } from '@lifi/sdk' import type { TradeQuote, TradeRate } from '../../../types' +export type LifiTools = { + bridges: string[] | undefined + exchanges: string[] | undefined +} + export interface LifiTradeQuote extends TradeQuote { selectedLifiRoute?: Route - lifiTools?: string[] | undefined + lifiTools?: LifiTools } export interface LifiTradeRate extends TradeRate { selectedLifiRoute?: Route - lifiTools?: string[] | undefined + lifiTools?: LifiTools } export type LifiTool = { diff --git a/packages/swapper/src/types.ts b/packages/swapper/src/types.ts index 96d6d6a6ec9..116887f5af5 100644 --- a/packages/swapper/src/types.ts +++ b/packages/swapper/src/types.ts @@ -29,6 +29,7 @@ import type { InterpolationOptions } from 'node-polyglot' import type { Address } from 'viem' import type { CowMessageToSign } from './swappers/CowSwapper/types' +import type { LifiTools } from './swappers/LifiSwapper/utils/types' import type { makeSwapperAxiosServiceMonadic } from './utils' // TODO: Rename all properties in this type to be camel case and not react specific @@ -157,7 +158,7 @@ type CommonTradeInputBase = { potentialAffiliateBps: string affiliateBps: string allowMultiHop: boolean - lifiAllowedTools?: string[] | undefined + lifiAllowedTools?: LifiTools slippageTolerancePercentageDecimal?: string } diff --git a/src/components/MultiHopTrade/hooks/useGetTradeQuotes/getTradeQuoteInput.ts b/src/components/MultiHopTrade/hooks/useGetTradeQuotes/getTradeQuoteInput.ts index 49113591382..651315425a1 100644 --- a/src/components/MultiHopTrade/hooks/useGetTradeQuotes/getTradeQuoteInput.ts +++ b/src/components/MultiHopTrade/hooks/useGetTradeQuotes/getTradeQuoteInput.ts @@ -2,6 +2,7 @@ import { CHAIN_NAMESPACE, fromChainId } from '@shapeshiftoss/caip' import type { HDWallet } from '@shapeshiftoss/hdwallet-core' import { supportsETH } from '@shapeshiftoss/hdwallet-core' import type { GetTradeQuoteInput } from '@shapeshiftoss/swapper' +import type { LifiTradeQuote } from '@shapeshiftoss/swapper/src/swappers/LifiSwapper/utils/types' import type { Asset, CosmosSdkChainId, EvmChainId, UtxoChainId } from '@shapeshiftoss/types' import { UtxoAccountType } from '@shapeshiftoss/types' import type { TradeQuoteInputCommonArgs } from 'components/MultiHopTrade/types' @@ -19,7 +20,7 @@ export type GetTradeQuoteInputArgs = { slippageTolerancePercentageDecimal?: string sellAmountBeforeFeesCryptoPrecision: string allowMultiHop: boolean - lifiAllowedTools?: string[] + lifiAllowedTools?: LifiTradeQuote['lifiTools'] | undefined // Potential affiliate bps - may be waved out either entirely or partially with FOX discounts potentialAffiliateBps: string // Actual affiliate bps - if the FOX discounts is off, this will be the same as *affiliateBps*