dexvm

package
v1.22.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2025 License: BSD-3-Clause Imports: 23 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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 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 Codec

type Codec struct{}

Codec implements gorilla/rpc codec interface.

func NewCodec

func NewCodec() *Codec

NewCodec creates a new JSON codec for RPC.

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 Factory

type Factory struct {
	config.Config
}

Factory creates new DEX VM instances.

func (*Factory) New

func (f *Factory) New(logger log.Logger) (interface{}, error)

New implements vms.Factory interface. It creates a new DEX VM instance with the factory's configuration.

type VM

type VM struct {
	config.Config
	// contains filtered or unexported fields
}

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

func NewVMForTest(cfg config.Config, logger log.Logger) *VM

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

func (vm *VM) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error

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) Connected

func (vm *VM) Connected(ctx context.Context, nodeID ids.NodeID, v *version.Application) error

Connected implements consensuscore.VM interface.

func (*VM) CreateHandlers

func (vm *VM) CreateHandlers(ctx context.Context) (map[string]http.Handler, error)

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

func (vm *VM) Disconnected(ctx context.Context, nodeID ids.NodeID) error

Disconnected implements consensuscore.VM interface.

func (*VM) GetBlockHeight

func (vm *VM) GetBlockHeight() uint64

GetBlockHeight returns the current block height.

func (*VM) GetLastBlockTime

func (vm *VM) GetLastBlockTime() time.Time

GetLastBlockTime returns the timestamp of the last processed block.

func (*VM) GetLiquidityManager

func (vm *VM) GetLiquidityManager() *liquidity.Manager

GetLiquidityManager returns the liquidity pool manager.

func (*VM) GetOrCreateOrderbook

func (vm *VM) GetOrCreateOrderbook(symbol string) *orderbook.Orderbook

GetOrCreateOrderbook returns or creates an orderbook for a symbol.

func (*VM) GetOrderbook

func (vm *VM) GetOrderbook(symbol string) (*orderbook.Orderbook, error)

GetOrderbook returns the orderbook for a symbol.

func (*VM) GetPerpetualsEngine

func (vm *VM) GetPerpetualsEngine() *perpetuals.Engine

GetPerpetualsEngine returns the perpetual futures engine.

func (*VM) HealthCheck

func (vm *VM) HealthCheck(ctx context.Context) (interface{}, error)

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

func (vm *VM) IsBootstrapped() bool

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:

  1. Order matching for all orderbooks
  2. Funding rate processing (every 8 hours)
  3. Liquidation checks
  4. State commitment

func (*VM) SetState

func (vm *VM) SetState(ctx context.Context, stateNum uint32) error

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.

func (*VM) Shutdown

func (vm *VM) Shutdown(ctx context.Context) error

Shutdown implements consensuscore.VM interface. It gracefully shuts down the VM. NOTE: No background tasks to wait for in functional mode.

func (*VM) Version

func (vm *VM) Version(ctx context.Context) (string, error)

Version implements consensuscore.VM interface.

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 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 orderbook implements a high-performance order book for the DEX VM.
Package orderbook implements a high-performance order book 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL