types

package
v11.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
var (
	ErrInvalidLengthGenesis        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowGenesis          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupGenesis = fmt.Errorf("proto: unexpected end of group")
)
View Source
var (

	// keySeparatorPlusOne is used for creating prefixes for the key end in iterators
	// when we want to get all of the keys in a prefix. Since it is one byte larger
	// than the original key separator and the end prefix is exclusive, it is valid
	// for getting all values under the original key separator.
	HistoricalTWAPTimeIndexPrefix = historicalTWAPTimeIndexNoSeparator + KeySeparator
	HistoricalTWAPPoolIndexPrefix = historicalTWAPPoolIndexNoSeparator + KeySeparator
)
View Source
var (
	KeyPruneEpochIdentifier    = []byte("PruneEpochIdentifier")
	KeyRecordHistoryKeepPeriod = []byte("RecordHistoryKeepPeriod")
)

Parameter store keys.

View Source
var (
	ErrInvalidLengthTwapRecord        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowTwapRecord          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupTwapRecord = fmt.Errorf("proto: unexpected end of group")
)

Functions

func AccumDiffDivDuration

func AccumDiffDivDuration(accumDiff sdk.Dec, timeDelta time.Duration) sdk.Dec

func FormatHistoricalPoolIndexTWAPKey

func FormatHistoricalPoolIndexTWAPKey(poolId uint64, accumulatorWriteTime time.Time, denom1, denom2 string) []byte

func FormatHistoricalPoolIndexTimePrefix

func FormatHistoricalPoolIndexTimePrefix(poolId uint64, accumulatorWriteTime time.Time) []byte

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 FormatMostRecentTWAPKey(poolId uint64, denom1, denom2 string) []byte

func GetAllUniqueDenomPairs

func GetAllUniqueDenomPairs(denoms []string) ([]string, []string)

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

NOTE: Sorts the input denoms slice.

func LexicographicalOrderDenoms

func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error)

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 MustGetSpotPrice

func MustGetSpotPrice(k AmmInterface, ctx sdk.Context, poolId uint64, baseAssetDenom string, quoteAssetDenom string) sdk.Dec

mustGetSpotPrice returns the spot price for the given pool id, and denom0 in terms of denom1. Panics if the pool state is misconfigured, which will halt any tx that interacts with this.

func ParamKeyTable

func ParamKeyTable() paramtypes.KeyTable

ParamTable for twap module.

func ParseTimeFromHistoricalPoolIndexKey

func ParseTimeFromHistoricalPoolIndexKey(key []byte) (time.Time, error)

func ParseTimeFromHistoricalTimeIndexKey

func ParseTimeFromHistoricalTimeIndexKey(key []byte) time.Time

func SpotPriceTimesDuration

func SpotPriceTimesDuration(sp sdk.Dec, timeDelta time.Duration) sdk.Dec

SpotPriceTimesDuration multiplies the spot price with the given delta time. 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 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)
	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 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) MarshalTo

func (m *GenesisState) MarshalTo(dAtA []byte) (int, 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 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 DefaultParams

func DefaultParams() Params

default twap module parameters.

func NewParams

func NewParams(pruneEpochIdentifier string, recordHistoryKeepPeriod time.Duration) Params

func (*Params) Descriptor

func (*Params) Descriptor() ([]byte, []int)

func (*Params) GetPruneEpochIdentifier

func (m *Params) GetPruneEpochIdentifier() string

func (*Params) GetRecordHistoryKeepPeriod

func (m *Params) GetRecordHistoryKeepPeriod() time.Duration

func (*Params) Marshal

func (m *Params) Marshal() (dAtA []byte, err error)

func (*Params) MarshalTo

func (m *Params) MarshalTo(dAtA []byte) (int, error)

func (*Params) MarshalToSizedBuffer

func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Params) ParamSetPairs

func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs

Implements params.ParamSet.

func (*Params) ProtoMessage

func (*Params) ProtoMessage()

func (*Params) Reset

func (m *Params) Reset()

func (*Params) Size

func (m *Params) Size() (n int)

func (*Params) String

func (m *Params) String() string

func (*Params) Unmarshal

func (m *Params) Unmarshal(dAtA []byte) error

func (Params) Validate

func (p Params) Validate() error

validate params.

func (*Params) XXX_DiscardUnknown

func (m *Params) XXX_DiscardUnknown()

func (*Params) XXX_Marshal

func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Params) XXX_Merge

func (m *Params) XXX_Merge(src proto.Message)

func (*Params) XXX_Size

func (m *Params) XXX_Size() int

func (*Params) XXX_Unmarshal

func (m *Params) XXX_Unmarshal(b []byte) error

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 */
}

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) 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) MarshalTo

func (m *TwapRecord) MarshalTo(dAtA []byte) (int, 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

Jump to

Keyboard shortcuts

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