使用Vite集成Uniswap Smart Order Router时出现Could not parse fraction错误
Vite项目中Uniswap Smart Order Router触发"Could not parse fraction"错误排查
在Vite项目中使用Uniswap Smart Order Router(已尝试最新版本及原CRA环境可用版本)时,每次获取路由都会触发Could not parse fraction错误。相同代码在Create React App环境中可正常运行,但仅复制组件文件到新Vite项目(未复制配置文件)时出现该问题。已尝试参考Stack Overflow相关解决方案,但未解决问题。
使用的版本
- @uniswap/sdk-core@5.3.1
- @uniswap/smart-order-router@3.35.13
- @uniswap/v3-sdk@3.13.1
- jsbi@3.2.5(也试过3.2.0版本,问题依旧)
触发错误的路由代码
const options: SmartRouter.SwapOptionsSwapRouter02 = { recipient: address, slippageTolerance: new Core.Percent(1, 1), deadline: Math.floor(Date.now() / 1000 + 1800), type: SmartRouter.SwapType.SWAP_ROUTER_02 } const route = await router.route( Core.CurrencyAmount.fromRawAmount(inputToken, inputAmount.toString()), outputToken, Core.TradeType.EXACT_INPUT, options ) if (!route || !route.methodParameters) { console.warn("Routing failed") return }
错误日志
useUniswap.ts:353 Failed to route swap, error occured: Error: Could not parse fraction at Function.tryParseFraction (chunk-YN4HSXAX.js?v=61b21d1c:1546:15) at Percent2.lessThan (chunk-YN4HSXAX.js?v=61b21d1c:1567:37) at Trade2.maximumAmountIn (chunk-4RU6S4JE.js?v=61b21d1c:5246:29) at addV3Swap (@uniswap_smart-order-router.js?v=61b21d1c:50785:135) at UniswapTrade2.encode3 [as encode] (@uniswap_smart-order-router.js?v=61b21d1c:50710:15) at Function.swapERC20CallParameters (@uniswap_smart-order-router.js?v=61b21d1c:50985:15) at buildSwapMethodParameters (@uniswap_smart-order-router.js?v=61b21d1c:110390:49) at calculateArbitrumToL1SecurityFee (@uniswap_smart-order-router.js?v=61b21d1c:110712:18) at calculateL1GasFeesHelper (@uniswap_smart-order-router.js?v=61b21d1c:110677:56) at Object.calculateL1GasFees (@uniswap_smart-order-router.js?v=61b21d1c:112279:20)
额外测试情况
尽管示例中slippageTolerance设为100%,我也尝试过(50, 10_000)和(50, 100)等值,问题仍存在。
不同链的表现差异:
- Ethereum:正常
- BNB Chain:正常
- Arbitrum One:触发Fraction错误(原CRA环境可正常运行)
- Polygon:正常
- AVAX C-Chain:失败,Provider Gas Error
- Base:触发Fraction错误
Vite配置
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import macrosPlugin from 'vite-plugin-babel-macros' // https://vitejs.dev/config/ export default defineConfig({ plugins: [react(), macrosPlugin()], define: { 'process.env': process.env }, build: { commonjsOptions: { transformMixedEsModules: true, }, }, })
请问这是Uniswap的bug,还是我的代码或Vite配置问题?
内容的提问来源于stack exchange,提问作者Ethan
相关产品推荐
相关产品推荐

