Documentation
¶
Overview ¶
Package oracle provides price oracle implementations for the DEX VM.
Index ¶
- Variables
- type PricePoint
- type TWAP
- func (t *TWAP) Clear()
- func (t *TWAP) GetLastPrice() (*big.Int, error)
- func (t *TWAP) GetPrice() (*big.Int, error)
- func (t *TWAP) GetPriceAt(at time.Time) (*big.Int, error)
- func (t *TWAP) GetVolatility() (uint64, error)
- func (t *TWAP) Market() string
- func (t *TWAP) ObservationCount() int
- func (t *TWAP) Record(price *big.Int, timestamp time.Time)
- func (t *TWAP) RecordNow(price *big.Int)
- func (t *TWAP) Window() time.Duration
- type TWAPOracle
Constants ¶
This section is empty.
Variables ¶
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 ¶
NewDefaultTWAP creates a TWAP oracle with the default 30-minute window.
func (*TWAP) GetLastPrice ¶
GetLastPrice returns the most recent observed price.
func (*TWAP) GetPrice ¶
GetPrice returns the time-weighted average price over the configured window. This is the primary method for getting manipulation-resistant prices.
func (*TWAP) GetPriceAt ¶
GetPriceAt returns the TWAP calculated at a specific point in time.
func (*TWAP) GetVolatility ¶
GetVolatility returns a measure of price volatility over the window. Returns the ratio of (max - min) / average as a percentage.
func (*TWAP) ObservationCount ¶
ObservationCount returns the number of price observations.
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 ¶
RecordPrice records a price for a market.