聚合多链 DEX 流动性,实现最优跨链交易路由与一键跨链 Swap
┌─────────────────────────────────────────────────────────────┐ │ GraphQL API Gateway │ ├─────────────────────────────────────────────────────────────┤ │ Quote Engine │ Route Optimizer │ Order Executor │ ├─────────────────────────────────────────────────────────────┤ │ DEX Aggregator Layer │ │ ┌─────────────────────────────────────────────────────┐ │ │ │ Uniswap │ Sushiswap │ Curve │ Balancer │ 1inch │... │ │ │ └─────────────────────────────────────────────────────┘ │ ├─────────────────────────────────────────────────────────────┤ │ Bridge Aggregator Layer │ │ ┌─────────────────────────────────────────────────────┐ │ │ │ Stargate │ Across │ Hop │ Celer │ Synapse │ ... │ │ │ └─────────────────────────────────────────────────────┘ │ ├─────────────────────────────────────────────────────────────┤ │ Multi-Chain Connectors (ETH, Arb, OP, Polygon, BSC, ...) │ ├─────────────────────────────────────────────────────────────┤ │ Price Oracle │ Liquidity Cache │ Transaction DB │ └─────────────────────────────────────────────────────────────┘
1// CrossChainRouter 跨链路由优化器2type CrossChainRouter struct {3 dexAggregators map[string]DEXAggregator4 bridgeAggregator *BridgeAggregator5 priceOracle *PriceOracle6}7 8// SwapRoute 完整交易路径9type SwapRoute struct {10 Steps []SwapStep11 TotalAmountIn *big.Int12 TotalAmountOut *big.Int13 TotalFee *big.Int14 EstimatedTime time.Duration15 PriceImpact float6416}17 18// SwapStep 单步交易19type SwapStep struct {20 Type string // "swap" or "bridge"21 Chain string22 Protocol string23 TokenIn common.Address24 TokenOut common.Address25 AmountIn *big.Int26 AmountOut *big.Int27 Fee *big.Int28}29 30// FindOptimalRoute 查找最优跨链路径31func (r *CrossChainRouter) FindOptimalRoute(32 ctx context.Context,33 req *SwapRequest,34) (*SwapRoute, error) {35 // 1. 同链直接 swap36 if req.SourceChain == req.DestChain {37 return r.findSameChainRoute(ctx, req)38 }39 40 // 2. 跨链场景:生成所有可能路径41 candidates := make([]*SwapRoute, 0)42 43 // 策略 A: 源链 swap + bridge + 目标链 swap44 routeA, _ := r.buildSwapBridgeSwapRoute(ctx, req)45 if routeA != nil {46 candidates = append(candidates, routeA)47 }48 49 // 策略 B: bridge 稳定币 + 目标链 swap50 routeB, _ := r.buildBridgeStableRoute(ctx, req)51 if routeB != nil {52 candidates = append(candidates, routeB)53 }54 55 // 策略 C: 源链 swap 成桥接代币 + bridge56 routeC, _ := r.buildSwapAndBridgeRoute(ctx, req)57 if routeC != nil {58 candidates = append(candidates, routeC)59 }60 61 // 3. 按净收益排序62 sort.Slice(candidates, func(i, j int) bool {63 netI := new(big.Int).Sub(64 candidates[i].TotalAmountOut,65 candidates[i].TotalFee,66 )67 netJ := new(big.Int).Sub(68 candidates[j].TotalAmountOut,69 candidates[j].TotalFee,70 )71 return netI.Cmp(netJ) > 072 })73 74 if len(candidates) == 0 {75 return nil, ErrNoRouteFound76 }77 78 return candidates[0], nil79}80 81// buildSwapBridgeSwapRoute 构建 Swap-Bridge-Swap 路径82func (r *CrossChainRouter) buildSwapBridgeSwapRoute(83 ctx context.Context,84 req *SwapRequest,85) (*SwapRoute, error) {86 // 1. 源链 Swap: TokenIn -> BridgeToken87 bridgeToken := r.getBestBridgeToken(req.SourceChain, req.DestChain)88 sourceSwap, err := r.dexAggregators[req.SourceChain].GetQuote(89 ctx, req.TokenIn, bridgeToken, req.AmountIn,90 )91 if err != nil {92 return nil, err93 }94 95 // 2. Bridge: SourceChain -> DestChain96 bridgeQuote, err := r.bridgeAggregator.GetQuote(97 ctx, req.SourceChain, req.DestChain, 98 bridgeToken, sourceSwap.AmountOut,99 )100 if err != nil {101 return nil, err102 }103 104 // 3. 目标链 Swap: BridgeToken -> TokenOut105 destSwap, err := r.dexAggregators[req.DestChain].GetQuote(106 ctx, bridgeToken, req.TokenOut, bridgeQuote.AmountOut,107 )108 if err != nil {109 return nil, err110 }111 112 return &SwapRoute{113 Steps: []SwapStep{114 sourceSwap.ToStep(),115 bridgeQuote.ToStep(),116 destSwap.ToStep(),117 },118 TotalAmountIn: req.AmountIn,119 TotalAmountOut: destSwap.AmountOut,120 TotalFee: r.sumFees(sourceSwap, bridgeQuote, destSwap),121 EstimatedTime: bridgeQuote.EstimatedTime + 2*time.Minute,122 }, nil123}