fix(gateway): remove C-Chain defaults, use env-var config

Gateway now requires --rpc/RPC_URL and --chain-id/CHAIN_ID.
No hardcoded C-Chain or Zoo endpoints. Precompile addresses
fixed to LX-series (0x9010-0x9060). White-label ready.
This commit is contained in:
2026-03-27 20:07:43 -07:00
parent 6ffa3f096f
commit df8418c706
3 changed files with 81 additions and 95 deletions
+16
View File
@@ -0,0 +1,16 @@
FROM golang:1.26-alpine AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o gateway ./cmd/gateway/
FROM alpine:3.21
RUN apk --no-cache add ca-certificates && addgroup -g 1000 -S gw && adduser -u 1000 -S gw -G gw
WORKDIR /home/gw
COPY --from=builder /app/gateway .
RUN chown gw:gw gateway
USER gw
EXPOSE 8080
ENTRYPOINT ["./gateway"]
+42 -67
View File
@@ -1,103 +1,78 @@
// Gateway API server that aggregates multiple DEX providers.
// Connects to real Lux nodes for on-chain pool data with Uniswap fallback.
// Connects to EVM nodes for on-chain pool data via DEX precompiles.
//
// Configuration via flags or environment variables:
//
// --rpc / RPC_URL EVM JSON-RPC endpoint (required)
// --chain-id / CHAIN_ID EVM chain ID (required)
// --addr Listen address (default :8080)
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"strconv"
"github.com/luxfi/dex/pkg/gateway"
"github.com/luxfi/dex/pkg/gateway/lux"
"github.com/luxfi/dex/pkg/gateway/uniswap"
)
func main() {
// Command line flags
addr := flag.String("addr", ":8080", "Server address")
uniswapAPIKey := flag.String("uniswap-api-key", "", "Uniswap API key (optional)")
enableFallback := flag.Bool("fallback", true, "Enable fallback to next provider on failure")
// RPC endpoints for Lux chains
luxRPC := flag.String("lux-rpc", "", "Lux mainnet C-Chain RPC endpoint")
zooRPC := flag.String("zoo-rpc", "", "Zoo chain RPC endpoint")
addr := flag.String("addr", ":8080", "Server listen address")
rpcURL := flag.String("rpc", "", "EVM JSON-RPC endpoint (env: RPC_URL)")
chainIDFlag := flag.String("chain-id", "", "EVM chain ID (env: CHAIN_ID)")
flag.Parse()
// Check environment variables for RPC endpoints
if *luxRPC == "" {
*luxRPC = os.Getenv("LUX_RPC_URL")
// Resolve from env if not set via flags
if *rpcURL == "" {
*rpcURL = os.Getenv("RPC_URL")
}
if *zooRPC == "" {
*zooRPC = os.Getenv("ZOO_RPC_URL")
if *chainIDFlag == "" {
*chainIDFlag = os.Getenv("CHAIN_ID")
}
// Default endpoints if not provided
if *luxRPC == "" {
*luxRPC = "http://127.0.0.1:9630/ext/bc/C/rpc"
if *rpcURL == "" {
log.Fatal("--rpc or RPC_URL is required")
}
if *zooRPC == "" {
*zooRPC = "http://127.0.0.1:9630/ext/bc/2iJykKjE7gpWNjGUvGG6fVtj7u5Tbvo89CVCu6gjNPCnEdCVpY/rpc"
if *chainIDFlag == "" {
log.Fatal("--chain-id or CHAIN_ID is required")
}
// Create gateway with configuration
chainID, err := strconv.ParseUint(*chainIDFlag, 10, 64)
if err != nil {
log.Fatalf("invalid chain ID %q: %v", *chainIDFlag, err)
}
gwChainID := gateway.ChainID(chainID)
config := gateway.GatewayConfig{
DefaultChainID: gateway.ChainIDLux,
EnableFallback: *enableFallback,
DefaultChainID: gwChainID,
EnableFallback: false,
CacheEnabled: true,
CacheTTLSeconds: 60,
}
gw := gateway.New(config)
// Register Lux native provider with RPC connectivity (highest priority)
rpcEndpoints := map[gateway.ChainID]string{
gateway.ChainIDLux: *luxRPC,
gateway.ChainIDZoo: *zooRPC,
}
luxProvider := lux.NewProvider(lux.ProviderConfig{
Name: "lux",
Priority: 10, // Lower number = higher priority
Chains: []gateway.ChainID{gateway.ChainIDLux, gateway.ChainIDZoo},
RPCEndpoints: rpcEndpoints,
})
if err := gw.RegisterProvider(luxProvider); err != nil {
log.Fatalf("Failed to register Lux provider: %v", err)
}
log.Printf("Registered Lux native provider (priority: 10)")
log.Printf(" LUX RPC: %s", *luxRPC)
log.Printf(" ZOO RPC: %s", *zooRPC)
// Register Uniswap provider (fallback for all chains)
uniswapProvider := uniswap.NewProvider(uniswap.ProviderConfig{
Name: "uniswap",
Priority: 100, // Lower priority - used as fallback
APIKey: *uniswapAPIKey,
Timeout: 30 * time.Second,
Chains: []gateway.ChainID{
gateway.ChainIDEthereum,
gateway.ChainIDArbitrum,
gateway.ChainIDOptimism,
gateway.ChainIDPolygon,
gateway.ChainIDBase,
gateway.ChainIDBNB,
// Also supports Lux chains as fallback
gateway.ChainIDLux,
gateway.ChainIDZoo,
provider := lux.NewProvider(lux.ProviderConfig{
Name: "native",
Priority: 10,
Chains: []gateway.ChainID{gwChainID},
RPCEndpoints: map[gateway.ChainID]string{
gwChainID: *rpcURL,
},
})
if err := gw.RegisterProvider(uniswapProvider); err != nil {
log.Fatalf("Failed to register Uniswap provider: %v", err)
if err := gw.RegisterProvider(provider); err != nil {
log.Fatalf("Failed to register provider: %v", err)
}
log.Println("Registered Uniswap provider (priority: 100, fallback)")
// Start gateway with graceful shutdown
log.Printf("Gateway server starting on %s", *addr)
log.Println("Provider priority: lux (10) > uniswap (100)")
log.Println("Supported chains: Lux (96369), Zoo (200200), Ethereum (1), Arbitrum (42161), Optimism (10), Polygon (137), Base (8453), BNB (56)")
log.Printf("DEX gateway starting on %s", *addr)
log.Printf(" Chain ID: %d", chainID)
log.Printf(" RPC: %s", *rpcURL)
fmt.Println()
if err := gw.StartWithGracefulShutdown(*addr); err != nil {
log.Fatalf("Server error: %v", err)
+23 -28
View File
@@ -1,5 +1,5 @@
// Package lux provides native Lux DEX provider implementations.
// This is where native liquidity, AMM, and conversion APIs will be implemented.
// Package lux provides a native EVM DEX provider that queries on-chain
// pool data via LX-series precompiles (0x9010-0x9060).
package lux
import (
@@ -23,16 +23,22 @@ var (
ErrNoRPCEndpoint = errors.New("no RPC endpoint configured")
)
// DEX precompile addresses on Lux
// DEX precompile addresses (LX-series, activated at genesis via dexConfig)
const (
PoolManagerAddress = "0x0000000000000000000000000000000000000400"
SwapRouterAddress = "0x0000000000000000000000000000000000000401"
HooksRegistryAddr = "0x0000000000000000000000000000000000000402"
FlashLoanAddress = "0x0000000000000000000000000000000000000403"
PoolManagerAddress = "0x0000000000000000000000000000000000009010" // LXPool — singleton AMM pool manager
OracleAddress = "0x0000000000000000000000000000000000009011" // LXOracle — multi-source price aggregation
SwapRouterAddress = "0x0000000000000000000000000000000000009012" // LXRouter — swap router
HooksRegistryAddr = "0x0000000000000000000000000000000000009013" // LXHooks — pool hooks registry
FlashLoanAddress = "0x0000000000000000000000000000000000009014" // LXFlash — flash loans
CLOBAddress = "0x0000000000000000000000000000000000009020" // LXBook — central limit order book
VaultAddress = "0x0000000000000000000000000000000000009030" // LXVault — clearinghouse, margin, positions
FeedAddress = "0x0000000000000000000000000000000000009040" // LXFeed — computed price feeds
LendAddress = "0x0000000000000000000000000000000000009050" // LXLend — lending pool
LiquidAddress = "0x0000000000000000000000000000000000009060" // LXLiquid — self-repaying loans
)
// Provider implements native Lux DEX functionality.
// Connects to real Lux nodes to query on-chain pool data from DEX precompiles.
// Provider implements native DEX functionality via LX precompiles.
// Connects to EVM nodes to query on-chain pool data.
type Provider struct {
name string
priority int
@@ -52,36 +58,25 @@ type ProviderConfig struct {
RPCEndpoints map[gateway.ChainID]string // Map of chainID to RPC endpoint URL
}
// DefaultConfig returns default configuration with RPC endpoints
// DefaultConfig returns a minimal config — caller must provide chains and RPC endpoints.
func DefaultConfig() ProviderConfig {
return ProviderConfig{
Name: "lux",
Priority: 10, // Higher priority than Uniswap (lower number = higher priority)
Chains: []gateway.ChainID{
gateway.ChainIDLux,
gateway.ChainIDZoo,
},
RPCEndpoints: map[gateway.ChainID]string{
gateway.ChainIDLux: "http://127.0.0.1:9630/ext/bc/C/rpc", // Lux mainnet C-Chain
gateway.ChainIDZoo: "http://127.0.0.1:9630/ext/bc/2iJykKjE7gpWNjGUvGG6fVtj7u5Tbvo89CVCu6gjNPCnEdCVpY/rpc", // Zoo chain
},
Name: "native",
Priority: 10,
Chains: nil,
RPCEndpoints: nil,
}
}
// NewProvider creates a new Lux native provider with RPC connectivity
// NewProvider creates a native DEX provider with RPC connectivity.
// Chains and RPCEndpoints must be provided by the caller.
func NewProvider(cfg ProviderConfig) *Provider {
if cfg.Name == "" {
cfg.Name = "lux"
cfg.Name = "native"
}
if cfg.Priority == 0 {
cfg.Priority = 10
}
if len(cfg.Chains) == 0 {
cfg.Chains = DefaultConfig().Chains
}
if cfg.RPCEndpoints == nil {
cfg.RPCEndpoints = DefaultConfig().RPCEndpoints
}
p := &Provider{
name: cfg.Name,