Skip to content

Commit

Permalink
fix: lifi bridges final trade quote (#8348)
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre authored Dec 12, 2024
1 parent 0c7156b commit 27163a0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChainId, ChainKey>,
): Promise<Result<LifiTradeQuote[], SwapErrorRight>> {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<ChainId, ChainKey>,
): Promise<Result<LifiTradeQuote[], SwapErrorRight>> => getTrade(input, deps, lifiChainMap)
9 changes: 7 additions & 2 deletions packages/swapper/src/swappers/LifiSwapper/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion packages/swapper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,7 +158,7 @@ type CommonTradeInputBase = {
potentialAffiliateBps: string
affiliateBps: string
allowMultiHop: boolean
lifiAllowedTools?: string[] | undefined
lifiAllowedTools?: LifiTools
slippageTolerancePercentageDecimal?: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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*
Expand Down

0 comments on commit 27163a0

Please sign in to comment.