Documentation ¶
Index ¶
- Constants
- Variables
- func AccumDiffDivDuration(accumDiff osmomath.Dec, timeDeltaMs int64) osmomath.Dec
- func CanonicalTimeMs(twapTime time.Time) int64
- func FormatHistoricalPoolIndexTWAPKey(poolId uint64, denom1, denom2 string, accumulatorWriteTime time.Time) []byte
- func FormatHistoricalPoolIndexTWAPKeyFromStrTime(poolId uint64, denom1, denom2 string, accumulatorWriteTimeString string) []byte
- func FormatHistoricalPoolIndexTimePrefix(poolId uint64, denom1, denom2 string) []byte
- func FormatHistoricalPoolIndexTimeSuffix(poolId uint64, denom1, denom2 string, accumulatorWriteTime time.Time) []byte
- func FormatHistoricalTimeIndexTWAPKey(accumulatorWriteTime time.Time, poolId uint64, denom1, denom2 string) []byte
- func FormatKeyPoolTwapRecords(poolId uint64) []byte
- func FormatMostRecentTWAPKey(poolId uint64, denom1, denom2 string) []byte
- func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error)
- func ParamKeyTable() paramtypes.KeyTable
- func ParseFieldsFromHistoricalTimeKey(bz []byte) (string, uint64, string, string, error)
- func SpotPriceMulDuration(sp osmomath.Dec, timeDeltaMs int64) osmomath.Dec
- type DenomPair
- type EndTimeInFutureError
- type GenesisState
- func (*GenesisState) Descriptor() ([]byte, []int)
- func (m *GenesisState) GetParams() Params
- func (m *GenesisState) GetTwaps() []TwapRecord
- func (m *GenesisState) Marshal() (dAtA []byte, err error)
- func (m *GenesisState) MarshalTo(dAtA []byte) (int, error)
- func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*GenesisState) ProtoMessage()
- func (m *GenesisState) Reset()
- func (m *GenesisState) Size() (n int)
- func (m *GenesisState) String() string
- func (m *GenesisState) Unmarshal(dAtA []byte) error
- func (g *GenesisState) Validate() error
- func (m *GenesisState) XXX_DiscardUnknown()
- func (m *GenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *GenesisState) XXX_Merge(src proto.Message)
- func (m *GenesisState) XXX_Size() int
- func (m *GenesisState) XXX_Unmarshal(b []byte) error
- type InvalidRecordCountError
- type InvalidUpdateRecordError
- type KeySeparatorLengthError
- type Params
- func (*Params) Descriptor() ([]byte, []int)
- func (m *Params) GetPruneEpochIdentifier() string
- func (m *Params) GetRecordHistoryKeepPeriod() time.Duration
- func (m *Params) Marshal() (dAtA []byte, err error)
- func (m *Params) MarshalTo(dAtA []byte) (int, error)
- func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs
- func (*Params) ProtoMessage()
- func (m *Params) Reset()
- func (m *Params) Size() (n int)
- func (m *Params) String() string
- func (m *Params) Unmarshal(dAtA []byte) error
- func (p Params) Validate() error
- func (m *Params) XXX_DiscardUnknown()
- func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Params) XXX_Merge(src proto.Message)
- func (m *Params) XXX_Size() int
- func (m *Params) XXX_Unmarshal(b []byte) error
- type PoolManagerInterface
- type PruningState
- func (*PruningState) Descriptor() ([]byte, []int)
- func (m *PruningState) GetIsPruning() bool
- func (m *PruningState) GetLastKeptTime() time.Time
- func (m *PruningState) GetLastKeySeen() []byte
- func (m *PruningState) Marshal() (dAtA []byte, err error)
- func (m *PruningState) MarshalTo(dAtA []byte) (int, error)
- func (m *PruningState) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*PruningState) ProtoMessage()
- func (m *PruningState) Reset()
- func (m *PruningState) Size() (n int)
- func (m *PruningState) String() string
- func (m *PruningState) Unmarshal(dAtA []byte) error
- func (m *PruningState) XXX_DiscardUnknown()
- func (m *PruningState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *PruningState) XXX_Merge(src proto.Message)
- func (m *PruningState) XXX_Size() int
- func (m *PruningState) XXX_Unmarshal(b []byte) error
- type StartTimeAfterEndTimeError
- type TimeStringKeyFormatError
- type TwapRecord
- func (*TwapRecord) Descriptor() ([]byte, []int)
- func (m *TwapRecord) GetAsset0Denom() string
- func (m *TwapRecord) GetAsset1Denom() string
- func (m *TwapRecord) GetHeight() int64
- func (m *TwapRecord) GetLastErrorTime() time.Time
- func (m *TwapRecord) GetPoolId() uint64
- func (m *TwapRecord) GetTime() time.Time
- func (m *TwapRecord) Marshal() (dAtA []byte, err error)
- func (m *TwapRecord) MarshalTo(dAtA []byte) (int, error)
- func (m *TwapRecord) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*TwapRecord) ProtoMessage()
- func (m *TwapRecord) Reset()
- func (m *TwapRecord) Size() (n int)
- func (m *TwapRecord) String() string
- func (m *TwapRecord) Unmarshal(dAtA []byte) error
- func (m *TwapRecord) XXX_DiscardUnknown()
- func (m *TwapRecord) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *TwapRecord) XXX_Merge(src proto.Message)
- func (m *TwapRecord) XXX_Size() int
- func (m *TwapRecord) XXX_Unmarshal(b []byte) error
- type UnexpectedSeparatorError
Constants ¶
const ( ModuleName = "twap" StoreKey = ModuleName TransientStoreKey = "transient_" + ModuleName // this is silly we have to do this RouterKey = ModuleName QuerierRoute = ModuleName // Contract: Coin denoms cannot contain this character KeySeparator = "|" )
Variables ¶
var ( ErrInvalidLengthGenesis = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowGenesis = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupGenesis = fmt.Errorf("proto: unexpected end of group") )
var ( PruningStateKey = []byte{0x01} // format is time | pool id | denom1 | denom2 // made for efficiently deleting records by time in pruning HistoricalTWAPTimeIndexPrefix = historicalTWAPTimeIndexNoSeparator + KeySeparator // format is pool id | denom1 | denom2 | time // made for efficiently getting records given (pool id, denom1, denom2) and time bounds HistoricalTWAPPoolIndexPrefix = historicalTWAPPoolIndexNoSeparator + KeySeparator )
var ( KeyPruneEpochIdentifier = []byte("PruneEpochIdentifier") KeyRecordHistoryKeepPeriod = []byte("RecordHistoryKeepPeriod") )
Parameter store keys.
var ( ErrInvalidLengthTwapRecord = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowTwapRecord = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupTwapRecord = fmt.Errorf("proto: unexpected end of group") )
var ( MaxSpotPrice = osmomath.NewDec(2).Power(128).Sub(osmomath.OneDec()) MaxSpotPriceBigDec = osmomath.BigDecFromDec(MaxSpotPrice) )
Functions ¶
func AccumDiffDivDuration ¶
AccumDiffDivDuration returns the osmomath.Decated difference dividosmomath.Deche the time delta, that is the spot price between the current and last TWAP record.
func CanonicalTimeMs ¶
CanonicalTimeMs returns the canonical time in milliseconds used for twap math computations in UTC. Removes any monotonic clock reading prior to conversion to ms. In twap, we assume all calculations are done in milliseconds. Therefore, this conversion is necessary to make sure that there are no rounding errors.
func FormatHistoricalTimeIndexTWAPKey ¶
func FormatHistoricalTimeIndexTWAPKey(accumulatorWriteTime time.Time, poolId uint64, denom1, denom2 string) []byte
TODO: Replace historical management with ORM, we currently accept 2x write amplification right now.
func FormatMostRecentTWAPKey ¶
func LexicographicalOrderDenoms ¶
LexicographicalOrderDenoms takes two denoms and returns them to be in lexicographically ascending order. In other words, the first returned denom string will be the lexicographically smaller of the two denoms. If the denoms are equal, an error will be returned.
func ParseFieldsFromHistoricalTimeKey ¶
returns timeString, poolIdString, denom1, denom2, error nolint: revive
func SpotPriceMulDuration ¶
SpotPriceMulDuration returns the spot price multiplied by the time delta, that is the spot price between the current and last TWAP record. A single second accounts for 1_000_000_000 when converted to int64.
Types ¶
type DenomPair ¶
DenomPair contains pair of assetA and assetB denoms which belong to a pool.
func GetAllUniqueDenomPairs ¶
GetAllUniqueDenomPairs returns all unique pairs of denoms, where for every pair (X, Y), X < Y. The pair (X,Y) should only appear once in the list. Denoms are lexicographically sorted. Panics if finds duplicate pairs.
NOTE: Sorts the input denoms slice.
type EndTimeInFutureError ¶
func (EndTimeInFutureError) Error ¶
func (e EndTimeInFutureError) Error() string
type GenesisState ¶
type GenesisState struct { // twaps is the collection of all twap records. Twaps []TwapRecord `protobuf:"bytes,1,rep,name=twaps,proto3" json:"twaps"` // params is the container of twap parameters. Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` }
GenesisState defines the twap module's genesis state.
func DefaultGenesis ¶
func DefaultGenesis() *GenesisState
DefaultGenesis returns the default twap genesis state.
func NewGenesisState ¶
func NewGenesisState(params Params, twapRecords []TwapRecord) *GenesisState
NewGenesisState returns genesis state with the given parameters and twap records.
func (*GenesisState) Descriptor ¶
func (*GenesisState) Descriptor() ([]byte, []int)
func (*GenesisState) GetParams ¶
func (m *GenesisState) GetParams() Params
func (*GenesisState) GetTwaps ¶
func (m *GenesisState) GetTwaps() []TwapRecord
func (*GenesisState) Marshal ¶
func (m *GenesisState) Marshal() (dAtA []byte, err error)
func (*GenesisState) MarshalToSizedBuffer ¶
func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error)
func (*GenesisState) ProtoMessage ¶
func (*GenesisState) ProtoMessage()
func (*GenesisState) Reset ¶
func (m *GenesisState) Reset()
func (*GenesisState) Size ¶
func (m *GenesisState) Size() (n int)
func (*GenesisState) String ¶
func (m *GenesisState) String() string
func (*GenesisState) Unmarshal ¶
func (m *GenesisState) Unmarshal(dAtA []byte) error
func (*GenesisState) Validate ¶
func (g *GenesisState) Validate() error
Validate validates the genesis state. Returns nil on success, error otherwise.
func (*GenesisState) XXX_DiscardUnknown ¶
func (m *GenesisState) XXX_DiscardUnknown()
func (*GenesisState) XXX_Marshal ¶
func (m *GenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
func (*GenesisState) XXX_Merge ¶
func (m *GenesisState) XXX_Merge(src proto.Message)
func (*GenesisState) XXX_Size ¶
func (m *GenesisState) XXX_Size() int
func (*GenesisState) XXX_Unmarshal ¶
func (m *GenesisState) XXX_Unmarshal(b []byte) error
type InvalidRecordCountError ¶
func (InvalidRecordCountError) Error ¶
func (e InvalidRecordCountError) Error() string
type InvalidUpdateRecordError ¶
type InvalidUpdateRecordError struct { RecordBlockHeight int64 RecordTime time.Time ActualBlockHeight int64 ActualTime time.Time }
func (InvalidUpdateRecordError) Error ¶
func (e InvalidUpdateRecordError) Error() string
type KeySeparatorLengthError ¶
func (KeySeparatorLengthError) Error ¶
func (e KeySeparatorLengthError) Error() string
type Params ¶
type Params struct { PruneEpochIdentifier string `protobuf:"bytes,1,opt,name=prune_epoch_identifier,json=pruneEpochIdentifier,proto3" json:"prune_epoch_identifier,omitempty"` RecordHistoryKeepPeriod time.Duration `` /* 170-byte string literal not displayed */ }
Params holds parameters for the twap module
func (*Params) Descriptor ¶
func (*Params) GetPruneEpochIdentifier ¶
func (*Params) GetRecordHistoryKeepPeriod ¶
func (*Params) MarshalToSizedBuffer ¶
func (*Params) ParamSetPairs ¶
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs
Implements params.ParamSet.
func (*Params) ProtoMessage ¶
func (*Params) ProtoMessage()
func (*Params) XXX_DiscardUnknown ¶
func (m *Params) XXX_DiscardUnknown()
func (*Params) XXX_Marshal ¶
func (*Params) XXX_Unmarshal ¶
type PoolManagerInterface ¶
type PoolManagerInterface interface { RouteGetPoolDenoms(ctx sdk.Context, poolId uint64) (denoms []string, err error) // CalculateSpotPrice returns the spot price of the quote asset in terms of the base asset, // using the specified pool. // E.g. if pool 1 traded 2 atom for 3 osmo, the quote asset was atom, and the base asset was osmo, // this would return 1.5. (Meaning that 1 atom costs 1.5 osmo) RouteCalculateSpotPrice( ctx sdk.Context, poolID uint64, quoteAssetDenom string, baseAssetDenom string, ) (price osmomath.BigDec, err error) }
AmmInterface is the functionality needed from a given pool ID, in order to maintain records and serve TWAPs.
type PruningState ¶
type PruningState struct { // is_pruning is true if the pruning process is ongoing. // This tells the module to continue pruning the TWAP records // at the EndBlock. IsPruning bool `protobuf:"varint,1,opt,name=is_pruning,json=isPruning,proto3" json:"is_pruning,omitempty"` // last_kept_time is the time of the last kept TWAP record. // This is used to determine all TWAP records that are older than // last_kept_time and should be pruned. LastKeptTime time.Time `protobuf:"bytes,2,opt,name=last_kept_time,json=lastKeptTime,proto3,stdtime" json:"last_kept_time" yaml:"last_kept_time"` // last_key_seen is the last key of the TWAP records that were pruned // before reaching the block's prune limit LastKeySeen []byte `protobuf:"bytes,3,opt,name=last_key_seen,json=lastKeySeen,proto3" json:"last_key_seen,omitempty"` }
PruningState allows us to spread out the pruning of TWAP records over time, instead of pruning all at once at the end of the epoch.
func (*PruningState) Descriptor ¶
func (*PruningState) Descriptor() ([]byte, []int)
func (*PruningState) GetIsPruning ¶
func (m *PruningState) GetIsPruning() bool
func (*PruningState) GetLastKeptTime ¶
func (m *PruningState) GetLastKeptTime() time.Time
func (*PruningState) GetLastKeySeen ¶
func (m *PruningState) GetLastKeySeen() []byte
func (*PruningState) Marshal ¶
func (m *PruningState) Marshal() (dAtA []byte, err error)
func (*PruningState) MarshalToSizedBuffer ¶
func (m *PruningState) MarshalToSizedBuffer(dAtA []byte) (int, error)
func (*PruningState) ProtoMessage ¶
func (*PruningState) ProtoMessage()
func (*PruningState) Reset ¶
func (m *PruningState) Reset()
func (*PruningState) Size ¶
func (m *PruningState) Size() (n int)
func (*PruningState) String ¶
func (m *PruningState) String() string
func (*PruningState) Unmarshal ¶
func (m *PruningState) Unmarshal(dAtA []byte) error
func (*PruningState) XXX_DiscardUnknown ¶
func (m *PruningState) XXX_DiscardUnknown()
func (*PruningState) XXX_Marshal ¶
func (m *PruningState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
func (*PruningState) XXX_Merge ¶
func (m *PruningState) XXX_Merge(src proto.Message)
func (*PruningState) XXX_Size ¶
func (m *PruningState) XXX_Size() int
func (*PruningState) XXX_Unmarshal ¶
func (m *PruningState) XXX_Unmarshal(b []byte) error
type StartTimeAfterEndTimeError ¶
func (StartTimeAfterEndTimeError) Error ¶
func (e StartTimeAfterEndTimeError) Error() string
type TimeStringKeyFormatError ¶
func (TimeStringKeyFormatError) Error ¶
func (e TimeStringKeyFormatError) Error() string
type TwapRecord ¶
type TwapRecord struct { PoolId uint64 `protobuf:"varint,1,opt,name=pool_id,json=poolId,proto3" json:"pool_id,omitempty"` // Lexicographically smaller denom of the pair Asset0Denom string `protobuf:"bytes,2,opt,name=asset0_denom,json=asset0Denom,proto3" json:"asset0_denom,omitempty"` // Lexicographically larger denom of the pair Asset1Denom string `protobuf:"bytes,3,opt,name=asset1_denom,json=asset1Denom,proto3" json:"asset1_denom,omitempty"` // height this record corresponds to, for debugging purposes Height int64 `protobuf:"varint,4,opt,name=height,proto3" json:"record_height" yaml:"record_height"` // This field should only exist until we have a global registry in the state // machine, mapping prior block heights within {TIME RANGE} to times. Time time.Time `protobuf:"bytes,5,opt,name=time,proto3,stdtime" json:"time" yaml:"record_time"` // We store the last spot prices in the struct, so that we can interpolate // accumulator values for times between when accumulator records are stored. P0LastSpotPrice cosmossdk_io_math.LegacyDec `` /* 139-byte string literal not displayed */ P1LastSpotPrice cosmossdk_io_math.LegacyDec `` /* 139-byte string literal not displayed */ P0ArithmeticTwapAccumulator cosmossdk_io_math.LegacyDec `` /* 175-byte string literal not displayed */ P1ArithmeticTwapAccumulator cosmossdk_io_math.LegacyDec `` /* 175-byte string literal not displayed */ GeometricTwapAccumulator cosmossdk_io_math.LegacyDec `` /* 165-byte string literal not displayed */ // This field contains the time in which the last spot price error occurred. // It is used to alert the caller if they are getting a potentially erroneous // TWAP, due to an unforeseen underlying error. LastErrorTime time.Time `protobuf:"bytes,11,opt,name=last_error_time,json=lastErrorTime,proto3,stdtime" json:"last_error_time" yaml:"last_error_time"` }
A TWAP record should be indexed in state by pool_id, (asset pair), timestamp The asset pair assets should be lexicographically sorted. Technically (pool_id, asset_0_denom, asset_1_denom, height) do not need to appear in the struct however we view this as the wrong performance tradeoff given SDK today. Would rather we optimize for readability and correctness, than an optimal state storage format. The system bottleneck is elsewhere for now.
func GetAllMostRecentTwapsForPool ¶
func GetAllMostRecentTwapsForPool(store sdk.KVStore, poolId uint64) ([]TwapRecord, error)
GetAllMostRecentTwapsForPool returns all of the most recent twap records for a pool id. if the pool id doesn't exist, then this returns a blank list.
func ParseTwapFromBz ¶
func ParseTwapFromBz(bz []byte) (twap TwapRecord, err error)
func (*TwapRecord) Descriptor ¶
func (*TwapRecord) Descriptor() ([]byte, []int)
func (*TwapRecord) GetAsset0Denom ¶
func (m *TwapRecord) GetAsset0Denom() string
func (*TwapRecord) GetAsset1Denom ¶
func (m *TwapRecord) GetAsset1Denom() string
func (*TwapRecord) GetHeight ¶
func (m *TwapRecord) GetHeight() int64
func (*TwapRecord) GetLastErrorTime ¶
func (m *TwapRecord) GetLastErrorTime() time.Time
func (*TwapRecord) GetPoolId ¶
func (m *TwapRecord) GetPoolId() uint64
func (*TwapRecord) GetTime ¶
func (m *TwapRecord) GetTime() time.Time
func (*TwapRecord) Marshal ¶
func (m *TwapRecord) Marshal() (dAtA []byte, err error)
func (*TwapRecord) MarshalToSizedBuffer ¶
func (m *TwapRecord) MarshalToSizedBuffer(dAtA []byte) (int, error)
func (*TwapRecord) ProtoMessage ¶
func (*TwapRecord) ProtoMessage()
func (*TwapRecord) Reset ¶
func (m *TwapRecord) Reset()
func (*TwapRecord) Size ¶
func (m *TwapRecord) Size() (n int)
func (*TwapRecord) String ¶
func (m *TwapRecord) String() string
func (*TwapRecord) Unmarshal ¶
func (m *TwapRecord) Unmarshal(dAtA []byte) error
func (*TwapRecord) XXX_DiscardUnknown ¶
func (m *TwapRecord) XXX_DiscardUnknown()
func (*TwapRecord) XXX_Marshal ¶
func (m *TwapRecord) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
func (*TwapRecord) XXX_Merge ¶
func (m *TwapRecord) XXX_Merge(src proto.Message)
func (*TwapRecord) XXX_Size ¶
func (m *TwapRecord) XXX_Size() int
func (*TwapRecord) XXX_Unmarshal ¶
func (m *TwapRecord) XXX_Unmarshal(b []byte) error
type UnexpectedSeparatorError ¶
func (UnexpectedSeparatorError) Error ¶
func (e UnexpectedSeparatorError) Error() string