Files
dex/pkg/lx/liquidity.go
zeekay 0f6a1b3b86 feat(trading): add cross-chain routing, bridge venues, and arbitrage detection
- CrossChainRouter with BFS path discovery across chain graph
- 4 bridge venues: Warp (AWM), Teleport, CCTP, LuxBridge
- Triangular + cross-chain arbitrage scanner
- HTTP handlers: cross-chain quote, bridges list, arb scan
- Gateway: venue router, order management, LP, multihop, Uniswap compat
- V4 LXRouter calldata builder with encode/decode
- All test tokens use generic LUSD (no white-label refs)
- 72 trading tests + gateway tests passing
2026-03-29 14:55:08 -07:00

42 lines
1.2 KiB
Go

// Copyright (C) 2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package lx
import (
"errors"
"math/big"
)
// uint128Max is 2^128 - 1.
var uint128Max = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 128), big.NewInt(1))
// ErrLiquidityUnderflow is returned when removing more liquidity than available.
var ErrLiquidityUnderflow = errors.New("liquidity underflow")
// ErrLiquidityOverflow is returned when the result exceeds uint128.
var ErrLiquidityOverflow = errors.New("liquidity overflow")
// AddDelta adds a signed liquidity delta to unsigned liquidity.
// x is uint128 (current liquidity, non-negative).
// y is int128 (delta, can be negative for removal).
//
// Matches Uniswap V4 LiquidityMath.addDelta:
// - if y < 0: result = x - |y|, must not underflow (result < 0)
// - if y >= 0: result = x + y, must not overflow uint128
func AddDelta(x, y *big.Int) (*big.Int, error) {
result := new(big.Int).Add(x, y)
if y.Sign() < 0 {
// Removal: result must be non-negative.
if result.Sign() < 0 {
return nil, ErrLiquidityUnderflow
}
} else {
// Addition: result must fit in uint128.
if result.Cmp(uint128Max) > 0 {
return nil, ErrLiquidityOverflow
}
}
return result, nil
}