Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove swap route legs #1746

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions apps/web/src/lib/hooks/react-query/trade/apiAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { Native, Token, type Type } from 'sushi/currency'
import { PoolType } from 'sushi/router'
import z from 'zod'
import type {
legValidator as legValidator01,
tokenValidator as tokenValidator01,
tradeValidator01,
} from './validator01'
import type {
legValidator as legValidator02,
tokenValidator as tokenValidator02,
tradeValidator02,
} from './validator02'
import type { tradeValidator02 } from './validator02'

type token1 = z.infer<typeof tokenValidator01>
type token2 = z.infer<typeof tokenValidator02>
type leg1 = z.infer<typeof legValidator01>
type leg2 = z.infer<typeof legValidator02>
export type swapApi1 = z.infer<typeof tradeValidator01>
export type swapApi2 = z.infer<typeof tradeValidator02>

Expand All @@ -42,47 +33,13 @@ function getApi1Token(token: Token | Native): token1 {
tokenId: token.id,
}
}
function getApi1TokenFromToken2(token: token2, chainId: number): token1 {
const isNative =
token.address.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
return {
chainId,
decimals: token.decimals,
symbol: token.symbol,
name: token.name,
isNative,
isToken: !isNative,
address: token.address,
tokenId: `${token.address}_${chainId}`,
}
}

function getApi1TokenAddr(token: Token | Native): string {
return token instanceof Token
? token.address
: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
}

function getApi1PoolType(pt: PoolType) {
if (pt === PoolType.Classic || pt === PoolType.Stable) return pt
return 'Unknown'
}

function getApi1Leg(leg: leg2, tokens: token2[], chainId: number): leg1 {
return {
poolAddress: leg.poolAddress,
poolType: getApi1PoolType(leg.poolType),
poolFee: leg.poolFee,
tokenFrom: getApi1TokenFromToken2(tokens[leg.tokenFrom] as token2, chainId),
tokenTo: getApi1TokenFromToken2(tokens[leg.tokenTo] as token2, chainId),
assumedAmountIn: Number(leg.assumedAmountIn),
assumedAmountOut: Number(leg.assumedAmountOut),
swapPortion: 0,
absolutePortion: leg.share,
poolName: leg.poolName,
}
}

// converts API 2 to API 1 response
export function apiAdapter02To01(
res: swapApi2,
Expand All @@ -99,7 +56,6 @@ export function apiAdapter02To01(
amountInBI: 0n,
amountOut: 0,
amountOutBI: 0n,
legs: [],
gasSpent: 0,
totalAmountOut: 0,
totalAmountOutBI: 0n,
Expand Down Expand Up @@ -131,7 +87,6 @@ export function apiAdapter02To01(
amountInBI: BigInt(res.amountIn),
amountOut: Number(res.assumedAmountOut),
amountOutBI: BigInt(res.assumedAmountOut),
legs: res.route.map((l) => getApi1Leg(l, res.tokens, fromToken.chainId)),
gasSpent: res.gasSpent,
totalAmountOut: Number(res.assumedAmountOut),
totalAmountOutBI: BigInt(res.assumedAmountOut),
Expand Down
3 changes: 0 additions & 3 deletions apps/web/src/lib/hooks/react-query/trade/validator01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export const tokenValidator = z.object({
})

export const legValidator = z.object({
poolAddress: z.string(),
poolType: z.enum(['Classic', 'Stable', 'Unknown']),
poolFee: z.number(),
tokenFrom: tokenValidator,
tokenTo: tokenValidator,
assumedAmountIn: z.number(),
Expand All @@ -39,7 +37,6 @@ export const tradeValidator01 = z.object({
amountInBI: z.bigint(),
amountOut: z.number(),
amountOutBI: z.bigint(),
legs: z.array(legValidator).optional(),
gasSpent: z.number(),
totalAmountOut: z.number(),
totalAmountOutBI: z.bigint(),
Expand Down
4 changes: 0 additions & 4 deletions apps/web/src/lib/hooks/react-query/trade/validator02.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export const tokenValidator = z.object({
})

export const legValidator = z.object({
poolAddress: z.string(),
poolType: z.nativeEnum(PoolType),
poolName: z.string(),
poolFee: z.number(),
tokenFrom: z.number(), // index in tokens array
tokenTo: z.number(), // index in tokens array
share: z.number(),
Expand All @@ -36,8 +34,6 @@ const routeExistValidator = z.object({
assumedAmountOut: z.string(),
gasSpent: z.number(),

route: z.array(legValidator),

routeProcessorAddr: z.string().optional(),
routeProcessorArgs: z
.object({
Expand Down
16 changes: 1 addition & 15 deletions apps/web/src/lib/swap/cross-chain/lib/SquidAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { squidRouterAbi } from 'sushi/abi'
import { ChainId } from 'sushi/chain'
import { SquidAdapterChainId } from 'sushi/config'
import { Token } from 'sushi/currency'
import { PoolType, RouteStatus } from 'sushi/router'
import { RouteStatus } from 'sushi/router'
import {
Hex,
decodeFunctionData,
encodeAbiParameters,
parseAbiParameters,
zeroAddress,
} from 'viem'
import { z } from 'zod'

Expand Down Expand Up @@ -87,18 +86,5 @@ export const getSquidTrade = (
amountIn: '',
assumedAmountOut: '',
gasSpent: 0,
route: [
{
poolAddress: zeroAddress,
poolType: PoolType.Unknown,
poolName: 'Squid',
poolFee: 0,
tokenFrom: 0,
tokenTo: 1,
share: 1,
assumedAmountIn: '',
assumedAmountOut: '',
},
],
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use client'

import { Currency, SkeletonBox, SkeletonText } from '@sushiswap/ui'
import { Currency, SkeletonText } from '@sushiswap/ui'
import { SquidIcon } from '@sushiswap/ui/icons/SquidIcon'
import { SushiXSwap2Adapter } from 'src/lib/swap/cross-chain'
import { Chain } from 'sushi/chain'
import { STARGATE_TOKEN } from 'sushi/config'
import { TradeRoutePathView } from '../trade-route-path-view'
import {
useCrossChainSwapTrade,
useDerivedStateCrossChainSwap,
Expand All @@ -32,18 +31,6 @@ export const CrossChainSwapTradeReviewRoute = () => {
Swap {token0.symbol} to {trade.srcBridgeToken.symbol}
</span>
)}
{isTradeLoading ? (
<SkeletonBox className="h-2.5 py-0.5 w-[80px]" />
) : trade?.srcTrade ? (
<TradeRoutePathView trade={trade.srcTrade}>
<button
type="button"
className="text-[10px] text-blue font-semibold text-left"
>
View Route
</button>
</TradeRoutePathView>
) : null}
</div>
</div>
<div className="flex items-center opacity-20 -ml-1 gap-1">
Expand Down Expand Up @@ -78,18 +65,6 @@ export const CrossChainSwapTradeReviewRoute = () => {
Swap {trade.dstBridgeToken.symbol} to {token1.symbol}
</span>
)}
{isTradeLoading ? (
<SkeletonBox className="h-2.5 py-0.5 w-[80px]" />
) : trade?.dstTrade ? (
<TradeRoutePathView trade={trade.dstTrade}>
<button
type="button"
className="text-[10px] text-blue font-semibold text-left"
>
View Route
</button>
</TradeRoutePathView>
) : null}
</div>
</div>
</div>
Expand Down
38 changes: 27 additions & 11 deletions apps/web/src/ui/swap/simple/simple-swap-trade-review-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
useWaitForTransactionReceipt,
} from 'wagmi'
import { useRefetchBalances } from '~evm/_common/ui/balance-provider/use-refetch-balances'
import { APPROVE_TAG_SWAP } from '../../../lib/constants'
import { APPROVE_TAG_SWAP, NativeAddress } from '../../../lib/constants'
import {
warningSeverity,
warningSeverityClassName,
Expand Down Expand Up @@ -93,8 +93,12 @@ export const SimpleSwapTradeReviewDialog: FC<{
amountIn: trade?.amountIn?.toSignificant(6),
amountOut: trade?.amountOut?.toSignificant(6),
minAmountOut: trade?.minAmountOut?.toSignificant(6),
fromToken: trade?.route?.fromToken,
toToken: trade?.route?.toToken,
fromToken: trade?.amountIn?.currency.isToken
? trade?.amountIn?.currency.address
: NativeAddress,
toToken: trade?.amountOut?.currency.isToken
? trade?.amountOut?.currency.address
: NativeAddress,
priceImpact: trade?.priceImpact?.toPercentageString(),
tokenTax:
trade?.tokenTax instanceof Percent
Expand Down Expand Up @@ -138,11 +142,10 @@ export const SimpleSwapTradeReviewDialog: FC<{
}

sendAnalyticsEvent(SwapEventName.SWAP_ESTIMATE_GAS_CALL_FAILED, {
route: stringify(trade?.route),
slippageTolerance: slippagePercent.toPercentageString(),
error: error.message,
})
}, [error, slippagePercent, trade?.route])
}, [error, slippagePercent])

const trace = useTrace()

Expand All @@ -158,7 +161,6 @@ export const SimpleSwapTradeReviewDialog: FC<{

sendAnalyticsEvent(SwapEventName.SWAP_SIGNED, {
...trace,
route: stringify(trade?.route),
txHash: hash,
})

Expand Down Expand Up @@ -205,15 +207,19 @@ export const SimpleSwapTradeReviewDialog: FC<{
txHash: hash,
from: receipt.from,
chain_id: chainId,
route: stringify(trade?.route),
tx: stringify(trade?.tx),
})
} else {
sendAnalyticsEvent(SwapEventName.SWAP_TRANSACTION_FAILED, {
txHash: hash,
from: receipt.from,
chain_id: chainId,
route: stringify(trade?.route),
token_from: trade?.amountIn?.currency.isToken
? trade?.amountIn?.currency.address
: NativeAddress,
token_to: trade?.amountOut?.currency.isToken
? trade?.amountOut?.currency.address
: NativeAddress,
tx: stringify(trade?.tx),
})
}
Expand Down Expand Up @@ -243,13 +249,18 @@ export const SimpleSwapTradeReviewDialog: FC<{
}

sendAnalyticsEvent(SwapEventName.SWAP_ERROR, {
route: stringify(trade?.route),
token_from: trade?.amountIn?.currency.isToken
? trade?.amountIn?.currency.address
: NativeAddress,
token_to: trade?.amountOut?.currency.isToken
? trade?.amountOut?.currency.address
: NativeAddress,
tx: stringify(trade?.tx),
error: e instanceof Error ? e.message : undefined,
})
createErrorToast(e.message, false)
},
[trade?.route, trade?.tx],
[trade?.amountIn?.currency, trade?.amountOut?.currency, trade?.tx],
)

const {
Expand Down Expand Up @@ -432,7 +443,12 @@ export const SimpleSwapTradeReviewDialog: FC<{
element={InterfaceElementName.CONFIRM_SWAP_BUTTON}
name={SwapEventName.SWAP_SUBMITTED_BUTTON_CLICKED}
properties={{
route: stringify(trade?.route),
token_from: trade?.amountIn?.currency.isToken
? trade?.amountIn?.currency.address
: NativeAddress,
token_to: trade?.amountOut?.currency.isToken
? trade?.amountOut?.currency.address
: NativeAddress,
...trace,
}}
>
Expand Down
21 changes: 0 additions & 21 deletions apps/web/src/ui/swap/simple/simple-swap-trade-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
warningSeverity,
warningSeverityClassName,
} from '../../../lib/swap/warningSeverity'
import { TradeRoutePathView } from '../trade-route-path-view'
import {
useDerivedStateSimpleSwap,
useSimpleSwapTrade,
Expand Down Expand Up @@ -131,26 +130,6 @@ export const SimpleSwapTradeStats: FC = () => {
</span>
</div>

<div className="flex justify-between items-center">
<span className="text-sm text-gray-700 dark:text-slate-400">
Route
</span>
<span className="text-sm font-semibold text-gray-700 text-right dark:text-slate-400">
{loading || !trade ? (
<SkeletonBox className="h-4 py-0.5 w-[40px]" />
) : (
<TradeRoutePathView trade={trade}>
<button
type="button"
className="text-sm text-blue font-semibold"
>
View
</button>
</TradeRoutePathView>
)}
</span>
</div>

{recipient && isAddress(recipient) && isMounted && (
<div className="flex justify-between items-center border-t border-gray-200 dark:border-slate-200/5 mt-2 pt-2">
<span className="font-medium text-sm text-gray-700 dark:text-slate-300">
Expand Down
Loading
Loading