Documentation ¶
Index ¶
- Constants
- Variables
- func AccumDiffDivDuration(accumDiff sdk.Dec, timeDelta time.Duration) sdk.Dec
- func FormatHistoricalPoolIndexTWAPKey(poolId uint64, denom1, denom2 string, accumulatorWriteTime time.Time) []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 FormatMostRecentTWAPKey(poolId uint64, denom1, denom2 string) []byte
- func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error)
- func ParamKeyTable() paramtypes.KeyTable
- func SpotPriceMulDuration(sp sdk.Dec, timeDelta time.Duration) sdk.Dec
- type AmmInterface
- 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 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 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 ( // 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 = sdk.NewDec(2).Power(128).Sub(sdk.OneDec())
Functions ¶
func AccumDiffDivDuration ¶
AccumDiffDivDuration returns the accumulated difference divided by the the time delta, that is the spot price between the current and last TWAP record.
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 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 AmmInterface ¶
type AmmInterface interface { GetPoolDenoms(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 moki, the quote asset was atom, and the base asset was moki, // this would return 1.5. (Meaning that 1 atom costs 1.5 moki) CalculateSpotPrice( ctx sdk.Context, poolID uint64, baseAssetDenom string, quoteAssetDenom string, ) (price sdk.Dec, err error) }
AmmInterface is the functionality needed from a given pool ID, in order to maintain records and serve TWAPs.
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 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 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 github_com_cosmos_cosmos_sdk_types.Dec `` /* 150-byte string literal not displayed */ P1LastSpotPrice github_com_cosmos_cosmos_sdk_types.Dec `` /* 150-byte string literal not displayed */ P0ArithmeticTwapAccumulator github_com_cosmos_cosmos_sdk_types.Dec `` /* 186-byte string literal not displayed */ P1ArithmeticTwapAccumulator github_com_cosmos_cosmos_sdk_types.Dec `` /* 186-byte string literal not displayed */ // This field contains the time in which the last spot price error occured. // 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