oracle

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: 4 Imported by: 0

Documentation

Overview

Package oracle provides price oracle implementations for the DEX VM.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoObservations indicates no price observations are available.
	ErrNoObservations = errors.New("no price observations available")

	// ErrInsufficientHistory indicates not enough history for TWAP calculation.
	ErrInsufficientHistory = errors.New("insufficient price history for TWAP")

	// ErrInvalidWindow indicates an invalid TWAP window duration.
	ErrInvalidWindow = errors.New("TWAP window must be positive")

	// DefaultTWAPWindow is the default TWAP calculation window.
	DefaultTWAPWindow = 30 * time.Minute

	// MinTWAPWindow is the minimum allowed TWAP window.
	MinTWAPWindow = 5 * time.Minute

	// MaxObservations is the maximum number of observations to keep.
	MaxObservations = 1000

	// PrecisionFactor for price calculations (1e18).
	PrecisionFactor = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
)

Functions

This section is empty.

Types

type PricePoint

type PricePoint struct {
	Price     *big.Int  // Price scaled by PrecisionFactor
	Timestamp time.Time // When the price was observed
}

PricePoint represents a single price observation at a specific time.

type TWAP

type TWAP struct {
	// contains filtered or unexported fields
}

TWAP implements a time-weighted average price oracle. It maintains a rolling window of price observations and calculates the time-weighted average to resist price manipulation.

func NewDefaultTWAP

func NewDefaultTWAP(market string) *TWAP

NewDefaultTWAP creates a TWAP oracle with the default 30-minute window.

func NewTWAP

func NewTWAP(market string, window time.Duration) (*TWAP, error)

NewTWAP creates a new TWAP oracle with the specified window duration.

func (*TWAP) Clear

func (t *TWAP) Clear()

Clear removes all observations.

func (*TWAP) GetLastPrice

func (t *TWAP) GetLastPrice() (*big.Int, error)

GetLastPrice returns the most recent observed price.

func (*TWAP) GetPrice

func (t *TWAP) GetPrice() (*big.Int, error)

GetPrice returns the time-weighted average price over the configured window. This is the primary method for getting manipulation-resistant prices.

func (*TWAP) GetPriceAt

func (t *TWAP) GetPriceAt(at time.Time) (*big.Int, error)

GetPriceAt returns the TWAP calculated at a specific point in time.

func (*TWAP) GetVolatility

func (t *TWAP) GetVolatility() (uint64, error)

GetVolatility returns a measure of price volatility over the window. Returns the ratio of (max - min) / average as a percentage.

func (*TWAP) Market

func (t *TWAP) Market() string

Market returns the market symbol.

func (*TWAP) ObservationCount

func (t *TWAP) ObservationCount() int

ObservationCount returns the number of price observations.

func (*TWAP) Record

func (t *TWAP) Record(price *big.Int, timestamp time.Time)

Record adds a new price observation.

func (*TWAP) RecordNow

func (t *TWAP) RecordNow(price *big.Int)

RecordNow adds a new price observation with the current time.

func (*TWAP) Window

func (t *TWAP) Window() time.Duration

Window returns the TWAP window duration.

type TWAPOracle

type TWAPOracle struct {
	// contains filtered or unexported fields
}

TWAPOracle manages TWAP oracles for multiple markets.

func NewTWAPOracle

func NewTWAPOracle(window time.Duration) *TWAPOracle

NewTWAPOracle creates a new multi-market TWAP oracle.

func (*TWAPOracle) GetLastPrice

func (o *TWAPOracle) GetLastPrice(market string) (*big.Int, error)

GetLastPrice returns the last observed price for a market.

func (*TWAPOracle) GetOrCreate

func (o *TWAPOracle) GetOrCreate(market string) *TWAP

GetOrCreate returns the TWAP oracle for a market, creating one if needed.

func (*TWAPOracle) GetPrice

func (o *TWAPOracle) GetPrice(market string) (*big.Int, error)

GetPrice returns the TWAP for a market.

func (*TWAPOracle) RecordPrice

func (o *TWAPOracle) RecordPrice(market string, price *big.Int, timestamp time.Time)

RecordPrice records a price for a market.

Jump to

Keyboard shortcuts

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