mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
dexd is now the ONLY command in the repo. One binary, two run modes:
dexd run as a luxd rpcchainvm PLUGIN. luxd execs the plugin from
dexd plugin its PluginDir with NO positional args (engine address arrives
via the runtime.EngineAddressKey env var) and probes the
version with `dexd version`. No-subcommand == plugin is
REQUIRED to preserve that contract — byte-for-byte the old
cmd/dchain behavior (rpc.Serve over the plugin transport).
dexd run run the STANDALONE D-Chain venue: open the on-disk zapdb,
dexd standalone serve the frozen dex_* surface over ZAP, and drive the
consensus sealer loop (WaitForEvent->BuildBlock->Verify->
Accept). -addr/-db flags preserved from the old cmd/dvenue.
Relocated logic (engine code NOT duplicated — pkg/dchain + pkg/lx untouched):
cmd/dchain/{main,runner} -> internal/plugin (plugin.Run)
cmd/dvenue/{main,engine_*} -> internal/standalone (standalone.Run + engineName)
cmd/dexd/main.go = thin run-mode dispatcher
Removed cmd dirs (12): dchain, dvenue, demo, dex-api-server, dex-server,
dexfillproof, dexseed, fix-gateway, gateway, monitor, persistent-server,
price-feed. Removed committed build artifacts: gateway, dex-server-linux.
Build/ship ONLY dexd:
- Dockerfile: canonical CPU image (CGO=0, ghcr.io/luxfi/dex) running
`dexd run`; no GPU/MLX required.
- Dockerfile.dvenue: GPU operator bundle repointed cmd/dvenue -> cmd/dexd.
- Deleted Dockerfile.{gateway,devnet,minimal,dvenue-cpu} and
docker/{Dockerfile,Dockerfile.cuda,backend/Dockerfile,compose-simple.yml}.
- Makefile rewritten to build/run/ship dexd only.
- docker.yml: dropped the dex-gateway image; dex-venue-image.yml: dvenue->dexd.
- Deleted legacy scripts that built removed/long-gone cmds; fixed deploy.sh +
dev-setup.sh; .gitignore ignores root build binaries.
Also included (coupled, environment-applied, settled): the in-progress
clob -> dex/orderbook terminology refactor across pkg/* + sdk/* + tests/*
(identifier + file renames, e.g. RegisterCLOB->RegisterDEX,
source_clob.go->source_orderbook.go). The relocated standalone venue calls the
renamed API, so it lands in the same consistent snapshot.
Verified (GOWORK=off CGO_ENABLED=0): `go build ./cmd/dexd`, `go build ./...`,
`go vet ./...` all exit 0; `go test ./pkg/dchain` (renamed RegisterDEX socket
path) green. GPU/cgo path not compiled locally (no CUDA); its rename mirrors
the verified nocgo twins and preserves the C ABI symbol names.
201 lines
6.1 KiB
Go
201 lines
6.1 KiB
Go
// Package trading provides a unified HFT trading SDK with multi-venue support.
|
|
package trading
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
// VenueCapabilities describes what a venue supports.
|
|
type VenueCapabilities struct {
|
|
LimitOrders bool
|
|
MarketOrders bool
|
|
StopOrders bool
|
|
PostOnly bool
|
|
CancelOrders bool
|
|
BatchOrders bool
|
|
Streaming bool
|
|
Orderbook bool
|
|
Trades bool
|
|
AmmSwap bool
|
|
AddLiquidity bool
|
|
RemoveLiquidity bool
|
|
LpPositions bool
|
|
MaxBatchSize int
|
|
SupportedPairs map[string]struct{}
|
|
}
|
|
|
|
// OrderBookCapabilities returns capabilities for a DEX/orderbook venue.
|
|
func OrderBookCapabilities() VenueCapabilities {
|
|
return VenueCapabilities{
|
|
LimitOrders: true,
|
|
MarketOrders: true,
|
|
StopOrders: true,
|
|
PostOnly: true,
|
|
CancelOrders: true,
|
|
BatchOrders: true,
|
|
Streaming: true,
|
|
Orderbook: true,
|
|
Trades: true,
|
|
MaxBatchSize: 10,
|
|
SupportedPairs: make(map[string]struct{}),
|
|
}
|
|
}
|
|
|
|
// AMMCapabilities returns capabilities for an AMM venue.
|
|
func AMMCapabilities() VenueCapabilities {
|
|
return VenueCapabilities{
|
|
MarketOrders: true,
|
|
Streaming: true,
|
|
Trades: true,
|
|
AmmSwap: true,
|
|
AddLiquidity: true,
|
|
RemoveLiquidity: true,
|
|
LpPositions: true,
|
|
MaxBatchSize: 1,
|
|
SupportedPairs: make(map[string]struct{}),
|
|
}
|
|
}
|
|
|
|
// SupportsPair checks if a pair is supported.
|
|
func (c VenueCapabilities) SupportsPair(symbol string) bool {
|
|
if len(c.SupportedPairs) == 0 {
|
|
return true // No restrictions
|
|
}
|
|
_, ok := c.SupportedPairs[symbol]
|
|
return ok
|
|
}
|
|
|
|
// VenueAdapter is the interface all venue adapters must implement.
|
|
type VenueAdapter interface {
|
|
// Identity
|
|
Name() string
|
|
VenueType() VenueType
|
|
Capabilities() VenueCapabilities
|
|
IsConnected() bool
|
|
LatencyMs() *int64
|
|
|
|
// Info returns venue information.
|
|
Info() VenueInfo
|
|
|
|
// Connection
|
|
Connect(ctx context.Context) error
|
|
Disconnect(ctx context.Context) error
|
|
|
|
// Market data
|
|
GetMarkets(ctx context.Context) ([]MarketInfo, error)
|
|
GetTicker(ctx context.Context, symbol string) (Ticker, error)
|
|
GetOrderbook(ctx context.Context, symbol string, depth int) (*Orderbook, error)
|
|
GetTrades(ctx context.Context, symbol string, limit int) ([]Trade, error)
|
|
|
|
// Account
|
|
GetBalances(ctx context.Context) ([]Balance, error)
|
|
GetBalance(ctx context.Context, asset string) (Balance, error)
|
|
GetOpenOrders(ctx context.Context, symbol string) ([]Order, error)
|
|
|
|
// Orders
|
|
PlaceOrder(ctx context.Context, request OrderRequest) (Order, error)
|
|
CancelOrder(ctx context.Context, orderID, symbol string) (Order, error)
|
|
CancelAllOrders(ctx context.Context, symbol string) ([]Order, error)
|
|
|
|
// AMM (optional - return ErrNotSupported if not implemented)
|
|
GetSwapQuote(ctx context.Context, baseToken, quoteToken string, amount decimal.Decimal, isBuy bool) (SwapQuote, error)
|
|
ExecuteSwap(ctx context.Context, baseToken, quoteToken string, amount decimal.Decimal, isBuy bool, slippage decimal.Decimal) (Trade, error)
|
|
GetPoolInfo(ctx context.Context, baseToken, quoteToken string) (PoolInfo, error)
|
|
AddLiquidity(ctx context.Context, baseToken, quoteToken string, baseAmount, quoteAmount, slippage decimal.Decimal) (LiquidityResult, error)
|
|
RemoveLiquidity(ctx context.Context, poolAddress string, liquidityAmount, slippage decimal.Decimal) (LiquidityResult, error)
|
|
GetLpPositions(ctx context.Context) ([]LpPosition, error)
|
|
}
|
|
|
|
// ErrNotSupported is returned when an operation is not supported.
|
|
var ErrNotSupported = NewTradingError("NOT_SUPPORTED", "operation not supported by this venue")
|
|
|
|
// BaseAdapter provides common functionality for adapters.
|
|
type BaseAdapter struct {
|
|
name string
|
|
venueType VenueType
|
|
capabilities VenueCapabilities
|
|
connected bool
|
|
latency *int64
|
|
makerFee decimal.Decimal
|
|
takerFee decimal.Decimal
|
|
}
|
|
|
|
// Name returns the venue name.
|
|
func (a *BaseAdapter) Name() string {
|
|
return a.name
|
|
}
|
|
|
|
// VenueType returns the venue type.
|
|
func (a *BaseAdapter) VenueType() VenueType {
|
|
return a.venueType
|
|
}
|
|
|
|
// Capabilities returns venue capabilities.
|
|
func (a *BaseAdapter) Capabilities() VenueCapabilities {
|
|
return a.capabilities
|
|
}
|
|
|
|
// IsConnected returns connection status.
|
|
func (a *BaseAdapter) IsConnected() bool {
|
|
return a.connected
|
|
}
|
|
|
|
// LatencyMs returns connection latency.
|
|
func (a *BaseAdapter) LatencyMs() *int64 {
|
|
return a.latency
|
|
}
|
|
|
|
// SetLatency updates the latency measurement.
|
|
func (a *BaseAdapter) SetLatency(ms int64) {
|
|
a.latency = &ms
|
|
}
|
|
|
|
// SetConnected updates connection status.
|
|
func (a *BaseAdapter) SetConnected(connected bool) {
|
|
a.connected = connected
|
|
}
|
|
|
|
// Info returns venue information.
|
|
func (a *BaseAdapter) Info() VenueInfo {
|
|
pairs := make([]string, 0, len(a.capabilities.SupportedPairs))
|
|
for p := range a.capabilities.SupportedPairs {
|
|
pairs = append(pairs, p)
|
|
}
|
|
return VenueInfo{
|
|
Name: a.name,
|
|
VenueType: a.venueType,
|
|
Connected: a.connected,
|
|
LatencyMs: a.latency,
|
|
SupportedPairs: pairs,
|
|
MakerFee: a.makerFee,
|
|
TakerFee: a.takerFee,
|
|
}
|
|
}
|
|
|
|
// Default AMM implementations that return ErrNotSupported
|
|
func (a *BaseAdapter) GetSwapQuote(ctx context.Context, baseToken, quoteToken string, amount decimal.Decimal, isBuy bool) (SwapQuote, error) {
|
|
return SwapQuote{}, ErrNotSupported
|
|
}
|
|
|
|
func (a *BaseAdapter) ExecuteSwap(ctx context.Context, baseToken, quoteToken string, amount decimal.Decimal, isBuy bool, slippage decimal.Decimal) (Trade, error) {
|
|
return Trade{}, ErrNotSupported
|
|
}
|
|
|
|
func (a *BaseAdapter) GetPoolInfo(ctx context.Context, baseToken, quoteToken string) (PoolInfo, error) {
|
|
return PoolInfo{}, ErrNotSupported
|
|
}
|
|
|
|
func (a *BaseAdapter) AddLiquidity(ctx context.Context, baseToken, quoteToken string, baseAmount, quoteAmount, slippage decimal.Decimal) (LiquidityResult, error) {
|
|
return LiquidityResult{}, ErrNotSupported
|
|
}
|
|
|
|
func (a *BaseAdapter) RemoveLiquidity(ctx context.Context, poolAddress string, liquidityAmount, slippage decimal.Decimal) (LiquidityResult, error) {
|
|
return LiquidityResult{}, ErrNotSupported
|
|
}
|
|
|
|
func (a *BaseAdapter) GetLpPositions(ctx context.Context) ([]LpPosition, error) {
|
|
return nil, ErrNotSupported
|
|
}
|