Skip to content

Commit 5eefa47

Browse files
authored
chore: widget update logic (#1073)
* fix: enso enabled related errors * chore: parity * fix: fallback to token list token * fix: chain switch
1 parent 65d1f2f commit 5eefa47

File tree

7 files changed

+28
-41
lines changed

7 files changed

+28
-41
lines changed

api/server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { serve } from 'bun'
22

33
const ENSO_API_BASE = 'https://api.enso.finance'
44

5+
function handleEnsoStatus(): Response {
6+
const apiKey = process.env.ENSO_API_KEY
7+
return Response.json({ configured: !!apiKey })
8+
}
9+
510
async function handleEnsoRoute(req: Request): Promise<Response> {
611
const url = new URL(req.url)
712
const fromAddress = url.searchParams.get('fromAddress')
@@ -116,6 +121,10 @@ serve({
116121
async fetch(req) {
117122
const url = new URL(req.url)
118123

124+
if (url.pathname === '/api/enso/status') {
125+
return handleEnsoStatus()
126+
}
127+
119128
if (url.pathname === '/api/enso/balances') {
120129
return handleEnsoBalances(req)
121130
}

src/components/pages/vaults/components/widget/deposit/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type DepositRouteType = 'DIRECT_DEPOSIT' | 'DIRECT_STAKE' | 'ENSO'
1+
export type DepositRouteType = 'DIRECT_DEPOSIT' | 'DIRECT_STAKE' | 'ENSO' | 'NO_ROUTE'
22

33
export interface DepositWidgetProps {
44
vaultAddress: `0x${string}`

src/components/pages/vaults/components/widget/deposit/useDepositRoute.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ export const useDepositRoute = ({
2828
const ensoEnabled = useEnsoEnabled()
2929

3030
return useMemo(() => {
31-
// When Enso disabled, always use direct deposit
32-
if (!ensoEnabled) {
33-
return 'DIRECT_DEPOSIT'
34-
}
35-
3631
// Case 1: Direct vault deposit (asset → vault)
3732
if (
3833
toAddress(depositToken) === toAddress(assetAddress) &&
@@ -51,6 +46,9 @@ export const useDepositRoute = ({
5146
}
5247

5348
// Case 3: All other cases use Enso
54-
return 'ENSO'
49+
if (ensoEnabled) {
50+
return 'ENSO'
51+
}
52+
return 'NO_ROUTE'
5553
}, [ensoEnabled, depositToken, assetAddress, destinationToken, vaultAddress, stakingAddress])
5654
}

src/components/pages/vaults/hooks/solvers/useSolverEnso.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useEnsoStatus } from '@pages/vaults/contexts/useEnsoStatus'
21
import type { TNormalizedBN } from '@shared/types'
32
import { isZeroAddress, toNormalizedBN } from '@shared/utils'
43
import { useCallback, useState } from 'react'
@@ -88,7 +87,6 @@ export const useSolverEnso = ({
8887
decimalsOut = 18,
8988
enabled = true
9089
}: UseSolverEnsoProps): UseSolverEnsoReturn => {
91-
const { setEnsoFailed } = useEnsoStatus()
9290
const [route, setRoute] = useState<EnsoRouteResponse | undefined>()
9391
const [error, setError] = useState<EnsoError | undefined>()
9492
const [isLoadingRoute, setIsLoadingRoute] = useState(false)
@@ -142,21 +140,12 @@ export const useSolverEnso = ({
142140
requestId: data.requestId
143141
})
144142
setError(data)
145-
// Only trigger fallback for server errors (5xx) or auth issues (401/403)
146-
// Normal errors like "no route found" (4xx) should not disable Enso
147-
const statusCode = data.statusCode || response.status
148-
if (statusCode >= 500 || statusCode === 401 || statusCode === 403) {
149-
setEnsoFailed(true)
150-
}
143+
151144
throw new Error(`Enso API error: ${data.message}`)
152145
}
153146
setError(undefined)
154147
setRoute(data)
155148
} catch (err) {
156-
// Network errors (fetch failed) indicate API is unreachable
157-
if (err instanceof TypeError && err.message.includes('fetch')) {
158-
setEnsoFailed(true)
159-
}
160149
console.error('Failed to get Enso route:', err, {
161150
chainId,
162151
destinationChainId,
@@ -167,19 +156,7 @@ export const useSolverEnso = ({
167156
} finally {
168157
setIsLoadingRoute(false)
169158
}
170-
}, [
171-
tokenIn,
172-
tokenOut,
173-
amountIn,
174-
fromAddress,
175-
receiver,
176-
chainId,
177-
destinationChainId,
178-
slippage,
179-
enabled,
180-
isCrossChain,
181-
setEnsoFailed
182-
])
159+
}, [tokenIn, tokenOut, amountIn, fromAddress, receiver, chainId, destinationChainId, slippage, enabled, isCrossChain])
183160

184161
const getEnsoTransaction = useCallback((): EnsoRouteResponse['tx'] | undefined => {
185162
return route?.tx

src/components/pages/vaults/hooks/useEnsoOrder.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react'
22
import type { Address, Hash, Hex } from 'viem'
33
import type { UseSimulateContractReturnType } from 'wagmi'
44
import { usePublicClient, useWaitForTransactionReceipt, useWalletClient } from 'wagmi'
5+
import { supportedChains } from '@/config/supportedChains'
56

67
interface EnsoTransaction {
78
to: Address
@@ -51,19 +52,16 @@ export const useEnsoOrder = ({
5152
if (!walletClient) throw new Error('No wallet client available')
5253
if (!publicClient) throw new Error('No public client available')
5354

54-
// Verify wallet is on the correct chain (use chainId prop, not ensoTx.chainId which may be undefined)
55-
if (walletClient.chain?.id !== chainId) {
56-
throw new Error(
57-
`Chain mismatch: wallet on ${walletClient.chain?.id}, transaction requires ${chainId}. Please try again.`
58-
)
59-
}
55+
// Note: Chain switching is handled by TransactionOverlay before calling executeOrder
56+
// We use the target chain from props, not walletClient.chain which may be stale
57+
const targetChain = supportedChains.find((c) => c.id === chainId)
6058

6159
// Send the transaction
6260
const hash = await walletClient.sendTransaction({
6361
to: ensoTx.to,
6462
data: ensoTx.data,
6563
value: BigInt(ensoTx.value || 0),
66-
chain: walletClient.chain
64+
chain: targetChain ?? walletClient.chain
6765
})
6866

6967
// Store hash for receipt monitoring

src/components/shared/contexts/WithTokenList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isAddressEqual } from 'viem'
55
import { useAsyncTrigger } from '../hooks/useAsyncTrigger'
66
import type { TAddress } from '../types/address'
77
import type { TDict, TNDict, TToken, TTokenList } from '../types/mixed'
8+
import { DEFAULT_ERC20 } from '../utils'
89
import { zeroNormalizedBN } from '../utils/format'
910
import { toAddress } from '../utils/tools.address'
1011
import { useWeb3 } from './useWeb3'
@@ -321,7 +322,7 @@ export const WithTokenList = ({
321322
if (fromTokenList) {
322323
return fromTokenList
323324
}
324-
return {} as TToken
325+
return DEFAULT_ERC20
325326
},
326327
[aggregatedTokenList]
327328
)

src/components/shared/contexts/useWallet.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DEFAULT_ERC20, isZeroAddress, toAddress, zeroNormalizedBN } from '../ut
1010
import { useWeb3 } from './useWeb3'
1111
import { useYearn } from './useYearn'
1212
import { useYearnTokens } from './useYearn.helper'
13+
import { useTokenList } from './WithTokenList'
1314

1415
const USE_ENSO_BALANCES = import.meta.env.VITE_BALANCE_SOURCE !== 'multicall'
1516

@@ -55,6 +56,9 @@ export const WalletContextApp = memo(function WalletContextApp(props: {
5556
isLoadingVaultList,
5657
isEnabled: Boolean(userAddress)
5758
})
59+
60+
const { getToken: getTokenListToken } = useTokenList()
61+
5862
const useBalancesHook = USE_ENSO_BALANCES ? useBalancesCombined : useBalancesWithQuery
5963
const {
6064
data: tokensRaw, // Expected to be TDict<TNormalizedBN | undefined>
@@ -106,7 +110,7 @@ export const WalletContextApp = memo(function WalletContextApp(props: {
106110
return token
107111
}
108112
// If balances is empty (during refetch), return cached token if available
109-
return tokenCache.current[cacheKey] || DEFAULT_ERC20
113+
return tokenCache.current[cacheKey] || getTokenListToken({ address, chainID })
110114
},
111115
[balances, userAddress]
112116
)

0 commit comments

Comments
 (0)