mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
Default build pulls zero gRPC code; rebuild with `-tags=grpc` to opt
into gRPC.
Decomplected via a `grpcBackend` interface in `sdk/go/client` and a
`grpcSubClient` interface in `client/go`. The default build wires a
no-op backend (`*_nogrpc.go`); the `grpc` build wires the real
protobuf-backed client (`*_grpc.go`). Public methods stay the same in
both builds — the gRPC fast path is preferred when wired, else
JSON-RPC / WebSocket.
Files gated:
- pkg/grpc/pb/lxdex_grpc.pb.go (//go:build grpc)
- pkg/proto/engine/proto/lx_engine_grpc.pb.go (//go:build grpc)
Files refactored:
- sdk/go/client.go: //go:build grpc with stub in client_nogrpc.go
- sdk/go/client/client.go: gRPC removed from method bodies; backed by
the grpcBackend interface (grpc_backend{,_grpc,_nogrpc}.go)
- client/go/main.go: GrpcClient + ConnectGrpc moved to grpc.go
(//go:build grpc); nogrpc.go stubs ConnectGrpc with a clear error
Verification:
- go build ./... (default) clean across sdk/go, client/go, top-level
- go build -tags=grpc ./... clean across all three modules
- go test ./... and go test -tags=grpc ./... green
- go list -deps shows 0 google.golang.org/grpc packages in default
builds of sdk/go/client and client/go; 62 packages with -tags=grpc
READMEs in sdk/go and client/go document the build-tag opt-in.
134 lines
3.4 KiB
Go
134 lines
3.4 KiB
Go
//go:build !grpc
|
|
|
|
// Package lxsdk is the gRPC-backed Go SDK for the LX trading platform.
|
|
//
|
|
// In the default build (no `grpc` build tag), gRPC support is excluded
|
|
// so consumers compile zero gRPC code. NewClient and NewHighFrequencyClient
|
|
// return an explanatory error. Rebuild with `-tags=grpc` to get the
|
|
// real client.
|
|
//
|
|
// External trading clients that do not need the binary gRPC transport
|
|
// should use the JSON-RPC + WebSocket SDK at
|
|
// github.com/luxfi/dex/sdk/go/client instead.
|
|
package lxsdk
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// ErrGRPCNotBuilt is returned when the SDK is used without the `grpc`
|
|
// build tag. Rebuild with `go build -tags=grpc` to enable.
|
|
var ErrGRPCNotBuilt = errors.New("lxsdk: gRPC client requires the `grpc` build tag (rebuild with -tags=grpc)")
|
|
|
|
// Side represents order side.
|
|
type Side int
|
|
|
|
const (
|
|
Buy Side = iota
|
|
Sell
|
|
)
|
|
|
|
// OrderType represents order type.
|
|
type OrderType int
|
|
|
|
const (
|
|
Market OrderType = iota
|
|
Limit
|
|
Stop
|
|
StopLimit
|
|
)
|
|
|
|
// Order represents a trading order.
|
|
type Order struct {
|
|
ID string
|
|
Symbol string
|
|
Price float64
|
|
Quantity float64
|
|
Side Side
|
|
Type OrderType
|
|
Timestamp time.Time
|
|
}
|
|
|
|
// Trade represents an executed trade.
|
|
type Trade struct {
|
|
ID string
|
|
OrderID string
|
|
Symbol string
|
|
Price float64
|
|
Quantity float64
|
|
Side Side
|
|
Timestamp time.Time
|
|
}
|
|
|
|
// Client is the no-op placeholder for the gRPC client. All operations
|
|
// return ErrGRPCNotBuilt.
|
|
type Client struct {
|
|
address string
|
|
|
|
// Match the gRPC build's exported observability surface so consumers
|
|
// compile against both builds.
|
|
ordersSent atomic.Int64
|
|
tradesRecv atomic.Int64
|
|
latencyNanos atomic.Int64
|
|
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewClient returns an error in the default build. Rebuild with `-tags=grpc`.
|
|
func NewClient(address string) (*Client, error) {
|
|
return nil, ErrGRPCNotBuilt
|
|
}
|
|
|
|
// EnableFIX is a no-op stub.
|
|
func (c *Client) EnableFIX(senderID, targetID string) {}
|
|
|
|
// SendOrder returns ErrGRPCNotBuilt.
|
|
func (c *Client) SendOrder(symbol string, price, quantity float64, side Side) (*Order, error) {
|
|
return nil, ErrGRPCNotBuilt
|
|
}
|
|
|
|
// SendBulkOrders returns ErrGRPCNotBuilt.
|
|
func (c *Client) SendBulkOrders(orders []*Order) error { return ErrGRPCNotBuilt }
|
|
|
|
// CancelOrder returns ErrGRPCNotBuilt.
|
|
func (c *Client) CancelOrder(orderID string) error { return ErrGRPCNotBuilt }
|
|
|
|
// Subscribe returns ErrGRPCNotBuilt.
|
|
func (c *Client) Subscribe(symbols []string, callback func(*Trade)) error { return ErrGRPCNotBuilt }
|
|
|
|
// GetOrderBook returns ErrGRPCNotBuilt.
|
|
func (c *Client) GetOrderBook(symbol string) (bids, asks []Order, err error) {
|
|
return nil, nil, ErrGRPCNotBuilt
|
|
}
|
|
|
|
// GetMetrics returns zeroed metrics.
|
|
func (c *Client) GetMetrics() map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"orders_sent": c.ordersSent.Load(),
|
|
"trades_received": c.tradesRecv.Load(),
|
|
"latency_ns": c.latencyNanos.Load(),
|
|
"latency_ms": float64(c.latencyNanos.Load()) / 1e6,
|
|
}
|
|
}
|
|
|
|
// GetThroughput returns 0.
|
|
func (c *Client) GetThroughput() float64 { return 0 }
|
|
|
|
// Close is a no-op.
|
|
func (c *Client) Close() error { return nil }
|
|
|
|
// HighFrequencyClient placeholder.
|
|
type HighFrequencyClient struct{ *Client }
|
|
|
|
// NewHighFrequencyClient returns ErrGRPCNotBuilt.
|
|
func NewHighFrequencyClient(address string) (*HighFrequencyClient, error) {
|
|
return nil, ErrGRPCNotBuilt
|
|
}
|
|
|
|
// SendOrderAsync is a no-op.
|
|
func (hft *HighFrequencyClient) SendOrderAsync(symbol string, price, quantity float64, side Side) {
|
|
}
|