Documentation
¶
Overview ¶
Package dexvm implements a high-performance decentralized exchange VM for the Lux blockchain network.
The DEX VM provides:
- Central Limit Order Book (CLOB) trading with nanosecond price updates
- Automated Market Maker (AMM) liquidity pools
- Cross-chain atomic swaps via Warp messaging
- 200ms block times for high-frequency trading
- LX-First arbitrage strategy support
Architecture:
- Uses the Lux consensus engine for finality
- Integrates with Warp 1.5 for cross-chain messaging
- Supports both spot and perpetual trading
- Designed for institutional-grade performance
Index ¶
- Variables
- type Block
- func (b *Block) Accept(ctx context.Context) error
- func (b *Block) Bytes() []byte
- func (b *Block) Height() uint64
- func (b *Block) ID() ids.ID
- func (b *Block) Parent() ids.ID
- func (b *Block) ParentID() ids.ID
- func (b *Block) Reject(ctx context.Context) error
- func (b *Block) Status() uint8
- func (b *Block) Timestamp() time.Time
- func (b *Block) Verify(ctx context.Context) error
- type BlockResult
- type ChainVM
- func (vm *ChainVM) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error
- func (vm *ChainVM) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, ...) error
- func (vm *ChainVM) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, ...) error
- func (vm *ChainVM) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error
- func (vm *ChainVM) BuildBlock(ctx context.Context) (block.Block, error)
- func (vm *ChainVM) Connected(ctx context.Context, nodeID ids.NodeID, v interface{}) error
- func (vm *ChainVM) CreateHandlers(ctx context.Context) (map[string]http.Handler, error)
- func (vm *ChainVM) Disconnected(ctx context.Context, nodeID ids.NodeID) error
- func (vm *ChainVM) GetBlock(ctx context.Context, blkID ids.ID) (block.Block, error)
- func (vm *ChainVM) GetBlockIDAtHeight(ctx context.Context, height uint64) (ids.ID, error)
- func (vm *ChainVM) GetInnerVM() *VM
- func (vm *ChainVM) GetLiquidityManager() interface{}
- func (vm *ChainVM) GetOrderbook(symbol string) (*orderbook.Orderbook, error)
- func (vm *ChainVM) GetPerpetualsEngine() interface{}
- func (vm *ChainVM) HealthCheck(ctx context.Context) (interface{}, error)
- func (vm *ChainVM) Initialize(ctx context.Context, consensusCtx interface{}, dbManager interface{}, ...) error
- func (vm *ChainVM) LastAccepted(ctx context.Context) (ids.ID, error)
- func (vm *ChainVM) NewHTTPHandler(ctx context.Context) (interface{}, error)
- func (vm *ChainVM) ParseBlock(ctx context.Context, data []byte) (block.Block, error)
- func (vm *ChainVM) SetPreference(ctx context.Context, blkID ids.ID) error
- func (vm *ChainVM) SetState(ctx context.Context, state uint32) error
- func (vm *ChainVM) Shutdown(ctx context.Context) error
- func (vm *ChainVM) SubmitTx(tx []byte) error
- func (vm *ChainVM) Version(ctx context.Context) (string, error)
- func (vm *ChainVM) WaitForEvent(ctx context.Context) (interface{}, error)
- type Codec
- type CodecRequest
- type Factory
- type Status
- type VM
- func (vm *VM) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error
- func (vm *VM) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, ...) error
- func (vm *VM) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, ...) error
- func (vm *VM) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error
- func (vm *VM) Connected(ctx context.Context, nodeID ids.NodeID, v *version.Application) error
- func (vm *VM) CreateHandlers(ctx context.Context) (map[string]http.Handler, error)
- func (vm *VM) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, ...) error
- func (vm *VM) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32, ...) error
- func (vm *VM) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error
- func (vm *VM) Disconnected(ctx context.Context, nodeID ids.NodeID) error
- func (vm *VM) GetADLEngine() api.ADLEngine
- func (vm *VM) GetBlockHeight() uint64
- func (vm *VM) GetCommitmentStore() api.CommitmentStore
- func (vm *VM) GetLastBlockTime() time.Time
- func (vm *VM) GetLiquidityManager() *liquidity.Manager
- func (vm *VM) GetOrCreateOrderbook(symbol string) *orderbook.Orderbook
- func (vm *VM) GetOrderbook(symbol string) (*orderbook.Orderbook, error)
- func (vm *VM) GetPerpetualsEngine() api.PerpetualsEngine
- func (vm *VM) HealthCheck(ctx context.Context) (interface{}, error)
- func (vm *VM) Initialize(ctx context.Context, consensusCtx interface{}, dbManager interface{}, ...) error
- func (vm *VM) IsBootstrapped() bool
- func (vm *VM) ProcessBlock(ctx context.Context, blockHeight uint64, blockTime time.Time, txs [][]byte) (*BlockResult, error)
- func (vm *VM) SetState(ctx context.Context, stateNum uint32) error
- func (vm *VM) Shutdown(ctx context.Context) error
- func (vm *VM) Version(ctx context.Context) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ( // VMID is the unique identifier for the DEX VM VMID = [32]byte{'d', 'e', 'x', 'v', 'm'} )
Functions ¶
This section is empty.
Types ¶
type Block ¶ added in v1.22.76
type Block struct {
// contains filtered or unexported fields
}
Block represents a DEX VM block that wraps the functional ProcessBlock results. It implements the block.Block interface required for the ChainVM.
type BlockResult ¶
type BlockResult struct {
// BlockHeight is the height of the processed block
BlockHeight uint64
// Timestamp is when this block was processed
Timestamp time.Time
// MatchedTrades from order matching in this block
MatchedTrades []orderbook.Trade
// FundingPayments processed in this block (if any)
FundingPayments []*perpetuals.FundingPayment
// Liquidations executed in this block (if any)
Liquidations []*perpetuals.LiquidationEvent
// StateRoot is the merkle root of state after this block
StateRoot ids.ID
}
BlockResult represents the deterministic result of processing a block. All state changes are captured here for verifiability.
type ChainVM ¶ added in v1.22.76
type ChainVM struct {
// contains filtered or unexported fields
}
ChainVM wraps the functional DEX VM to implement the block.ChainVM interface required for running as an L2 subnet plugin.
func NewChainVM ¶ added in v1.22.76
NewChainVM creates a new ChainVM that wraps a functional DEX VM
func (*ChainVM) AppRequest ¶ added in v1.22.76
func (vm *ChainVM) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error
AppRequest implements the VM interface
func (*ChainVM) AppRequestFailed ¶ added in v1.22.76
func (vm *ChainVM) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, appErr *consensuscore.AppError) error
AppRequestFailed implements the VM interface
func (*ChainVM) AppResponse ¶ added in v1.22.76
func (vm *ChainVM) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error
AppResponse implements the VM interface
func (*ChainVM) BuildBlock ¶ added in v1.22.76
BuildBlock implements the block.ChainVM interface. It builds a new block from pending transactions.
func (*ChainVM) CreateHandlers ¶ added in v1.22.76
CreateHandlers implements the interface expected by chain manager for HTTP registration
func (*ChainVM) Disconnected ¶ added in v1.22.76
Disconnected implements the VM interface
func (*ChainVM) GetBlock ¶ added in v1.22.76
GetBlock implements the block.ChainVM interface. It returns a block by its ID.
func (*ChainVM) GetBlockIDAtHeight ¶ added in v1.22.76
GetBlockIDAtHeight returns the block ID at the given height
func (*ChainVM) GetInnerVM ¶ added in v1.22.76
GetInnerVM returns the inner functional VM for direct access
func (*ChainVM) GetLiquidityManager ¶ added in v1.22.76
func (vm *ChainVM) GetLiquidityManager() interface{}
GetLiquidityManager returns the liquidity manager
func (*ChainVM) GetOrderbook ¶ added in v1.22.76
GetOrderbook returns an orderbook by symbol
func (*ChainVM) GetPerpetualsEngine ¶ added in v1.22.76
func (vm *ChainVM) GetPerpetualsEngine() interface{}
GetPerpetualsEngine returns the perpetuals engine
func (*ChainVM) HealthCheck ¶ added in v1.22.76
HealthCheck implements the VM interface
func (*ChainVM) Initialize ¶ added in v1.22.76
func (vm *ChainVM) Initialize( ctx context.Context, consensusCtx interface{}, dbManager interface{}, genesisBytes []byte, upgradeBytes []byte, configBytes []byte, msgChan interface{}, fxs []interface{}, appSender interface{}, ) error
Initialize implements the VM interface
func (*ChainVM) LastAccepted ¶ added in v1.22.76
LastAccepted implements the block.ChainVM interface. It returns the ID of the last accepted block.
func (*ChainVM) NewHTTPHandler ¶ added in v1.22.76
NewHTTPHandler implements the block.ChainVM interface
func (*ChainVM) ParseBlock ¶ added in v1.22.76
ParseBlock implements the block.ChainVM interface. It parses a block from bytes.
func (*ChainVM) SetPreference ¶ added in v1.22.76
SetPreference implements the block.ChainVM interface. It sets the preferred block for building new blocks.
type Codec ¶
type Codec struct{}
Codec implements gorilla/rpc codec interface.
func (*Codec) NewRequest ¶
func (c *Codec) NewRequest(*http.Request) rpc.CodecRequest
type CodecRequest ¶
type CodecRequest struct{}
CodecRequest implements rpc.CodecRequest
func (*CodecRequest) Method ¶
func (r *CodecRequest) Method() (string, error)
func (*CodecRequest) ReadRequest ¶
func (r *CodecRequest) ReadRequest(interface{}) error
func (*CodecRequest) WriteError ¶
func (r *CodecRequest) WriteError(http.ResponseWriter, int, error)
func (*CodecRequest) WriteResponse ¶
func (r *CodecRequest) WriteResponse(http.ResponseWriter, interface{})
type VM ¶
VM implements the DEX Virtual Machine using a pure functional architecture. All state transitions happen deterministically within block processing:
- Central Limit Order Book (CLOB) trading
- Automated Market Maker (AMM) liquidity pools
- Cross-chain atomic swaps via Warp messaging
- 1ms block times for ultra-low latency HFT support
DESIGN: No background goroutines. All operations are block-driven and deterministic. This ensures:
- Every node produces identical state from identical inputs
- No race conditions or non-deterministic behavior
- Easy to test and verify
- Replay-safe for auditing
func NewVMForTest ¶
NewVMForTest creates a new VM instance for testing purposes. This allows external test packages to create VM instances without needing to access internal fields directly.
func (*VM) AppGossip ¶
AppGossip implements consensuscore.VM interface. It handles gossiped messages from peers.
func (*VM) AppRequest ¶
func (vm *VM) AppRequest( ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte, ) error
AppRequest implements consensuscore.VM interface. It handles direct requests from peers.
func (*VM) AppRequestFailed ¶
func (vm *VM) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32, appErr *consensuscore.AppError) error
AppRequestFailed implements consensuscore.VM interface.
func (*VM) AppResponse ¶
func (vm *VM) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error
AppResponse implements consensuscore.VM interface.
func (*VM) CreateHandlers ¶
CreateHandlers implements consensuscore.VM interface. It creates HTTP handlers for the DEX API.
func (*VM) CrossChainAppRequest ¶
func (vm *VM) CrossChainAppRequest( ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte, ) error
CrossChainAppRequest implements consensuscore.VM interface.
func (*VM) CrossChainAppRequestFailed ¶
func (vm *VM) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32, appErr *consensuscore.AppError) error
CrossChainAppRequestFailed implements consensuscore.VM interface.
func (*VM) CrossChainAppResponse ¶
func (vm *VM) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error
CrossChainAppResponse implements consensuscore.VM interface.
func (*VM) Disconnected ¶
Disconnected implements consensuscore.VM interface.
func (*VM) GetADLEngine ¶ added in v1.22.76
GetADLEngine returns the auto-deleveraging engine.
func (*VM) GetBlockHeight ¶
GetBlockHeight returns the current block height.
func (*VM) GetCommitmentStore ¶ added in v1.22.76
func (vm *VM) GetCommitmentStore() api.CommitmentStore
GetCommitmentStore returns the MEV protection commitment store.
func (*VM) GetLastBlockTime ¶
GetLastBlockTime returns the timestamp of the last processed block.
func (*VM) GetLiquidityManager ¶
GetLiquidityManager returns the liquidity pool manager.
func (*VM) GetOrCreateOrderbook ¶
GetOrCreateOrderbook returns or creates an orderbook for a symbol.
func (*VM) GetOrderbook ¶
GetOrderbook returns the orderbook for a symbol.
func (*VM) GetPerpetualsEngine ¶
func (vm *VM) GetPerpetualsEngine() api.PerpetualsEngine
GetPerpetualsEngine returns the perpetual futures engine.
func (*VM) HealthCheck ¶
HealthCheck implements consensuscore.VM interface.
func (*VM) Initialize ¶
func (vm *VM) Initialize( ctx context.Context, consensusCtx interface{}, dbManager interface{}, genesisBytes []byte, upgradeBytes []byte, configBytes []byte, msgChan interface{}, fxs []interface{}, appSender interface{}, ) error
Initialize implements consensuscore.VM interface. It sets up the VM with the provided context, database, and genesis data.
func (*VM) IsBootstrapped ¶
IsBootstrapped returns true if the VM is fully bootstrapped.
func (*VM) ProcessBlock ¶
func (vm *VM) ProcessBlock(ctx context.Context, blockHeight uint64, blockTime time.Time, txs [][]byte) (*BlockResult, error)
ProcessBlock is the core function that processes all DEX operations deterministically. This is called by the consensus engine for each new block. All state changes happen here in a deterministic, reproducible manner.
Operations performed per block:
- Order matching for all orderbooks
- Funding rate processing (every 8 hours)
- Liquidation checks
- State commitment
func (*VM) SetState ¶
SetState implements consensuscore.VM interface. It transitions the VM between bootstrapping and normal operation states. NOTE: No background goroutines are started - all operations are block-driven.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api provides RPC and REST API handlers for the DEX VM.
|
Package api provides RPC and REST API handlers for the DEX VM. |
|
Package block implements block structure for the DEX VM.
|
Package block implements block structure for the DEX VM. |
|
Package config defines configuration types for the DEX VM.
|
Package config defines configuration types for the DEX VM. |
|
Package lending provides a DeFi lending protocol for the DEX VM.
|
Package lending provides a DeFi lending protocol for the DEX VM. |
|
Package liquidity implements AMM liquidity pools for the DEX VM.
|
Package liquidity implements AMM liquidity pools for the DEX VM. |
|
Package math provides safe arithmetic operations for the DEX VM.
|
Package math provides safe arithmetic operations for the DEX VM. |
|
Package mev implements MEV protection via commit-reveal scheme.
|
Package mev implements MEV protection via commit-reveal scheme. |
|
Package network provides peer-to-peer networking and Warp messaging for the DEX VM.
|
Package network provides peer-to-peer networking and Warp messaging for the DEX VM. |
|
Package oracle provides price oracle implementations for the DEX VM.
|
Package oracle provides price oracle implementations for the DEX VM. |
|
Package orderbook implements a high-performance order book for the DEX VM.
|
Package orderbook implements a high-performance order book for the DEX VM. |
|
Package perpetuals provides perpetual futures trading functionality for the DEX VM.
|
Package perpetuals provides perpetual futures trading functionality for the DEX VM. |
|
Package state manages persistent state for the DEX VM.
|
Package state manages persistent state for the DEX VM. |
|
Package txs defines transaction types for the DEX VM.
|
Package txs defines transaction types for the DEX VM. |