dexvm

package
v1.22.77 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: BSD-3-Clause Imports: 28 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 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.

func (*Block) Accept added in v1.22.76

func (b *Block) Accept(ctx context.Context) error

Accept marks the block as accepted

func (*Block) Bytes added in v1.22.76

func (b *Block) Bytes() []byte

Bytes returns the serialized block

func (*Block) Height added in v1.22.76

func (b *Block) Height() uint64

Height returns the block height

func (*Block) ID added in v1.22.76

func (b *Block) ID() ids.ID

ID returns the unique identifier for this block

func (*Block) Parent added in v1.22.76

func (b *Block) Parent() ids.ID

Parent returns the parent block's ID (alias for ParentID)

func (*Block) ParentID added in v1.22.76

func (b *Block) ParentID() ids.ID

ParentID returns the parent block's ID

func (*Block) Reject added in v1.22.76

func (b *Block) Reject(ctx context.Context) error

Reject marks the block as rejected

func (*Block) Status added in v1.22.76

func (b *Block) Status() uint8

Status returns the block's status as uint8

func (*Block) Timestamp added in v1.22.76

func (b *Block) Timestamp() time.Time

Timestamp returns the block timestamp

func (*Block) Verify added in v1.22.76

func (b *Block) Verify(ctx context.Context) error

Verify verifies the block is valid by processing it deterministically

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

func NewChainVM(logger log.Logger) *ChainVM

NewChainVM creates a new ChainVM that wraps a functional DEX VM

func (*ChainVM) AppGossip added in v1.22.76

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

AppGossip implements the VM interface

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

func (vm *ChainVM) BuildBlock(ctx context.Context) (block.Block, error)

BuildBlock implements the block.ChainVM interface. It builds a new block from pending transactions.

func (*ChainVM) Connected added in v1.22.76

func (vm *ChainVM) Connected(ctx context.Context, nodeID ids.NodeID, v interface{}) error

Connected implements the block.ChainVM interface

func (*ChainVM) CreateHandlers added in v1.22.76

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

CreateHandlers implements the interface expected by chain manager for HTTP registration

func (*ChainVM) Disconnected added in v1.22.76

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

Disconnected implements the VM interface

func (*ChainVM) GetBlock added in v1.22.76

func (vm *ChainVM) GetBlock(ctx context.Context, blkID ids.ID) (block.Block, error)

GetBlock implements the block.ChainVM interface. It returns a block by its ID.

func (*ChainVM) GetBlockIDAtHeight added in v1.22.76

func (vm *ChainVM) GetBlockIDAtHeight(ctx context.Context, height uint64) (ids.ID, error)

GetBlockIDAtHeight returns the block ID at the given height

func (*ChainVM) GetInnerVM added in v1.22.76

func (vm *ChainVM) GetInnerVM() *VM

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

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

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

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

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

func (vm *ChainVM) LastAccepted(ctx context.Context) (ids.ID, error)

LastAccepted implements the block.ChainVM interface. It returns the ID of the last accepted block.

func (*ChainVM) NewHTTPHandler added in v1.22.76

func (vm *ChainVM) NewHTTPHandler(ctx context.Context) (interface{}, error)

NewHTTPHandler implements the block.ChainVM interface

func (*ChainVM) ParseBlock added in v1.22.76

func (vm *ChainVM) ParseBlock(ctx context.Context, data []byte) (block.Block, error)

ParseBlock implements the block.ChainVM interface. It parses a block from bytes.

func (*ChainVM) SetPreference added in v1.22.76

func (vm *ChainVM) SetPreference(ctx context.Context, blkID ids.ID) error

SetPreference implements the block.ChainVM interface. It sets the preferred block for building new blocks.

func (*ChainVM) SetState added in v1.22.76

func (vm *ChainVM) SetState(ctx context.Context, state uint32) error

SetState implements the VM interface

func (*ChainVM) Shutdown added in v1.22.76

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

Shutdown implements the VM interface

func (*ChainVM) SubmitTx added in v1.22.76

func (vm *ChainVM) SubmitTx(tx []byte) error

SubmitTx adds a transaction to the pending pool

func (*ChainVM) Version added in v1.22.76

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

Version implements the VM interface

func (*ChainVM) WaitForEvent added in v1.22.76

func (vm *ChainVM) WaitForEvent(ctx context.Context) (interface{}, error)

WaitForEvent implements the block.ChainVM interface. It blocks until an event occurs that should trigger block building.

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 Status added in v1.22.76

type Status uint8

Status represents block status

const (
	StatusUnknown Status = iota
	StatusProcessing
	StatusAccepted
	StatusRejected
)

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) GetADLEngine added in v1.22.76

func (vm *VM) GetADLEngine() api.ADLEngine

GetADLEngine returns the auto-deleveraging engine.

func (*VM) GetBlockHeight

func (vm *VM) GetBlockHeight() uint64

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

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() api.PerpetualsEngine

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 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.

Jump to

Keyboard shortcuts

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