Files
zeekayandHanzo Dev 45bff3f5b6 fix(dex): close price sources synchronously; build pkg/dpdk and pkg/orderbook on linux
`go test ./...` failed three ways on the runner. Two were build failures that
cannot happen on a mac, and one was a race that only loses under load.

pkg/dpdk — `undefined: syscall.SO_NOSIGPIPE`. DarwinOptimizations guarded itself
with `if runtime.GOOS != "darwin" { return }`, but SO_NOSIGPIPE is a COMPILE-time
constant that exists only on Darwin; a runtime check cannot save a reference the
linux build still has to resolve. Split into _darwin.go / _nodarwin.go. Verified
by cross-compiling both ways -- it failed for GOOS=linux before, both pass now.

pkg/orderbook — the cgo preamble uses uint64_t while including only <stdlib.h>.
macOS pulls stdint.h in transitively and glibc does not, so this built here and
never on the runner. Added the include.

pkg/price — TestXChainSourceStalePrice: "Expected price to be stale".

Not a flaky test; Close() was not a barrier. It closed the done channel and
returned WITHOUT waiting for the polling goroutine. A loop selecting on done
against a ticker picks a ready case AT RANDOM, so a poll could still be running
after Close() returned, and poll writes last[symbol] = time.Now(). The test
closes the source, backdates that timestamp by 10s, and asserts staleness -- an
in-flight poll overwrote it. Wide window on a loaded runner, near-zero on a
laptop, which is why it reproduces 0/30 here.

All EIGHT sources had it, so the fix is one shared `lifecycle` (embedded, so
`s.done` still resolves) whose stop() joins before returning and is safe to call
twice. Oracle had the same defect written as

    // Give time for goroutines to exit
    time.Sleep(100 * time.Millisecond)

which could not work either -- calcAverages blocks on a 1s ticker, so it ran up
to a full second after Stop() returned. Now on the same lifecycle, and its loops
select on done instead of polling a flag, so they leave promptly rather than
after one more tick. Nothing restarts a stopped source (checked: 0 tests), and
the other sources already could not.

    go build ./...                        OK
    GOOS=linux go build ./...             OK   (failed before)
    go vet price/dpdk/orderbook           OK
    go test ./pkg/price/ -race            ok, 3/3 runs
    go test -run StalePrice -count=30     ok

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-02 03:19:43 -07:00

326 lines
7.3 KiB
Go

package price
import (
"context"
"math/big"
"sync"
"time"
)
// ZooChainSource provides prices from Zoo Chain EVM DEX.
// Zoo Labs Foundation network with native ZOO token pairs.
type ZooChainSource struct {
rpcURL string
wsURL string
routers map[string]string
tokens map[string]ZooPair
prices map[string]*Data
last map[string]time.Time
reserves map[string]*ZooReserves
interval time.Duration
healthy bool
mu sync.RWMutex
lifecycle
polling bool
}
// ZooPair defines a Zoo Chain trading pair.
type ZooPair struct {
Token0 string
Token1 string
Decimals [2]uint8
Pool string
Router string
}
// ZooReserves tracks Zoo AMM pool state.
type ZooReserves struct {
R0 *big.Int
R1 *big.Int
Block uint64
Time time.Time
}
// NewZooChainSource creates a Zoo Chain price source.
func NewZooChainSource(rpcURL, wsURL string) *ZooChainSource {
return &ZooChainSource{
rpcURL: rpcURL,
wsURL: wsURL,
routers: zooRouters(),
tokens: zooTokens(),
prices: make(map[string]*Data),
last: make(map[string]time.Time),
reserves: make(map[string]*ZooReserves),
interval: 100 * time.Millisecond,
healthy: true,
lifecycle: newLifecycle(),
}
}
func zooRouters() map[string]string {
return map[string]string{
"zoo_dex": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", // Zoo DEX router
"zoo_swap": "0x1111111254EEB25477B68fb85Ed929f73A960582", // Zoo Swap aggregator
}
}
func zooTokens() map[string]ZooPair {
// Zoo Chain EVM DEX pairs
return map[string]ZooPair{
// Native ZOO pairs
"ZOO-USDC": {
Token0: "0x0000000000000000000000000000000000000001", // ZOO native
Token1: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
Decimals: [2]uint8{18, 6},
Router: "zoo_dex",
},
"ZOO-USDT": {
Token0: "0x0000000000000000000000000000000000000001",
Token1: "0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT
Decimals: [2]uint8{18, 6},
Router: "zoo_dex",
},
"ZOO-ETH": {
Token0: "0x0000000000000000000000000000000000000001",
Token1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
Decimals: [2]uint8{18, 18},
Router: "zoo_dex",
},
// ZOO/LUX bridge pair
"ZOO-LUX": {
Token0: "0x0000000000000000000000000000000000000001",
Token1: "0x0000000000000000000000000000000000000002", // Wrapped LUX
Decimals: [2]uint8{18, 18},
Router: "zoo_dex",
},
// Wrapped assets on Zoo Chain
"WZOO-USDC": {
Token0: "0x0000000000000000000000000000000000000003", // WZOO
Token1: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
Decimals: [2]uint8{18, 6},
Router: "zoo_dex",
},
// Cross-chain liquid tokens on Zoo
"LZOO-ZOO": {
Token0: "0x0000000000000000000000000000000000000004", // LZOO from Lux
Token1: "0x0000000000000000000000000000000000000001",
Decimals: [2]uint8{18, 18},
Router: "zoo_dex",
},
// Stablecoin pairs
"USDC-USDT": {
Token0: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
Token1: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
Decimals: [2]uint8{6, 6},
Router: "zoo_swap",
},
// ETH pairs
"ETH-USDC": {
Token0: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
Token1: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
Decimals: [2]uint8{18, 6},
Router: "zoo_dex",
},
"ETH-ZOO": {
Token0: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
Token1: "0x0000000000000000000000000000000000000001",
Decimals: [2]uint8{18, 18},
Router: "zoo_dex",
},
// BTC pairs
"WBTC-USDC": {
Token0: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", // WBTC
Token1: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
Decimals: [2]uint8{8, 6},
Router: "zoo_dex",
},
"WBTC-ZOO": {
Token0: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
Token1: "0x0000000000000000000000000000000000000001",
Decimals: [2]uint8{8, 18},
Router: "zoo_dex",
},
}
}
// Start begins polling Zoo Chain pools.
func (s *ZooChainSource) Start() error {
s.mu.Lock()
if s.polling {
s.mu.Unlock()
return nil
}
s.polling = true
s.mu.Unlock()
s.run(s.loop)
return nil
}
func (s *ZooChainSource) loop() {
ticker := time.NewTicker(s.interval)
defer ticker.Stop()
for {
select {
case <-s.done:
return
case <-ticker.C:
s.poll()
}
}
}
func (s *ZooChainSource) poll() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var wg sync.WaitGroup
for symbol, pair := range s.tokens {
wg.Add(1)
go func(sym string, p ZooPair) {
defer wg.Done()
s.fetch(ctx, sym, p)
}(symbol, pair)
}
wg.Wait()
}
func (s *ZooChainSource) fetch(ctx context.Context, symbol string, pair ZooPair) {
// In production: call getReserves() on the pool contract
// For now, simulate realistic prices
price := s.simulate(symbol)
s.mu.Lock()
defer s.mu.Unlock()
s.prices[symbol] = &Data{
Symbol: symbol,
Price: price,
Confidence: 0.94,
Timestamp: time.Now(),
Source: "zoo-chain",
}
s.last[symbol] = time.Now()
s.healthy = true
}
func (s *ZooChainSource) simulate(symbol string) float64 {
// Simulated prices for Zoo Chain DEX
base := map[string]float64{
// ZOO native pairs
"ZOO-USDC": 0.00015, // ZOO token price
"ZOO-USDT": 0.00015,
"ZOO-ETH": 0.0000000375, // ZOO/ETH ratio
"ZOO-LUX": 0.0647, // ZOO/LUX ratio
// Wrapped ZOO
"WZOO-USDC": 0.00015,
"LZOO-ZOO": 0.713, // LZOO/ZOO ratio (from Lux)
// Stablecoins
"USDC-USDT": 1.0,
// Major pairs
"ETH-USDC": 4000.0,
"ETH-ZOO": 26666666.0, // ETH/ZOO ratio
"WBTC-USDC": 105000.0,
"WBTC-ZOO": 700000000.0, // WBTC/ZOO ratio
}
b, ok := base[symbol]
if !ok {
return 0
}
// Slight AMM variation
now := time.Now().UnixNano()
v := float64(now%1000) / 100000.0
return b * (1.0 + v)
}
// Price returns the latest price for a symbol.
func (s *ZooChainSource) Price(symbol string) (*Data, error) {
s.mu.RLock()
defer s.mu.RUnlock()
p, ok := s.prices[symbol]
if !ok {
return nil, ErrNotFound
}
if time.Since(s.last[symbol]) > 5*time.Second {
copy := *p
copy.Stale = true
return &copy, nil
}
copy := *p
return &copy, nil
}
// Prices returns prices for multiple symbols.
func (s *ZooChainSource) Prices(symbols []string) (map[string]*Data, error) {
result := make(map[string]*Data)
for _, sym := range symbols {
if p, err := s.Price(sym); err == nil {
result[sym] = p
}
}
return result, nil
}
// Subscribe is a no-op (uses polling).
func (s *ZooChainSource) Subscribe(symbol string) error { return nil }
// Unsubscribe is a no-op.
func (s *ZooChainSource) Unsubscribe(symbol string) error { return nil }
// Healthy returns health status.
func (s *ZooChainSource) Healthy() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.healthy
}
// Name returns "zoo-chain".
func (s *ZooChainSource) Name() string { return "zoo-chain" }
// Weight returns 1.7 (Zoo DEX AMM - high trust).
func (s *ZooChainSource) Weight() float64 { return 1.7 }
// Close stops the source.
func (s *ZooChainSource) Close() error {
s.stop()
s.mu.Lock()
s.polling = false
s.mu.Unlock()
return nil
}
// GetReserves returns pool reserves for a symbol.
func (s *ZooChainSource) GetReserves(symbol string) (*ZooReserves, error) {
s.mu.RLock()
defer s.mu.RUnlock()
r, ok := s.reserves[symbol]
if !ok {
return nil, ErrNotFound
}
return r, nil
}
// Pairs returns available trading pairs.
func (s *ZooChainSource) Pairs() map[string]ZooPair {
return s.tokens
}