Documentation ¶
Overview ¶
package accumulator allows one to define an accumulator to accommodate constant-rate (linear) distribution mechanisms with constant runtime and linear memory
Index ¶
- Constants
- Variables
- func FormatPositionPrefixKey(accumName, name string) []byte
- func GetTotalRewards(accum *AccumulatorObject, position Record) sdk.DecCoins
- func MakeAccumulator(accumStore store.KVStore, accumName string) error
- func MakeAccumulatorWithValueAndShare(accumStore store.KVStore, accumName string, accumValue sdk.DecCoins, ...) error
- func OverwriteAccumulatorUnsafe(accumStore store.KVStore, accumName string, accumValue sdk.DecCoins, ...) error
- type AccumDoesNotExistError
- type AccumulatorContent
- func (*AccumulatorContent) Descriptor() ([]byte, []int)
- func (m *AccumulatorContent) GetAccumValue() github_com_cosmos_cosmos_sdk_types.DecCoins
- func (m *AccumulatorContent) Marshal() (dAtA []byte, err error)
- func (m *AccumulatorContent) MarshalTo(dAtA []byte) (int, error)
- func (m *AccumulatorContent) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*AccumulatorContent) ProtoMessage()
- func (m *AccumulatorContent) Reset()
- func (m *AccumulatorContent) Size() (n int)
- func (m *AccumulatorContent) String() string
- func (m *AccumulatorContent) Unmarshal(dAtA []byte) error
- func (m *AccumulatorContent) XXX_DiscardUnknown()
- func (m *AccumulatorContent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *AccumulatorContent) XXX_Merge(src proto.Message)
- func (m *AccumulatorContent) XXX_Size() int
- func (m *AccumulatorContent) XXX_Unmarshal(b []byte) error
- type AccumulatorObject
- func (accum *AccumulatorObject) AddToAccumulator(amt sdk.DecCoins)
- func (accum *AccumulatorObject) AddToPosition(name string, newShares osmomath.Dec) error
- func (accum *AccumulatorObject) AddToPositionIntervalAccumulation(name string, newShares osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins) error
- func (accum *AccumulatorObject) AddToUnclaimedRewards(positionName string, rewardsToAddTotal sdk.DecCoins) error
- func (accum *AccumulatorObject) ClaimRewards(positionName string) (sdk.Coins, sdk.DecCoins, error)
- func (accum *AccumulatorObject) DeletePosition(positionName string) (sdk.DecCoins, error)
- func (accum AccumulatorObject) GetName() string
- func (accum AccumulatorObject) GetPosition(name string) (Record, error)
- func (accum *AccumulatorObject) GetPositionSize(name string) (osmomath.Dec, error)
- func (accum AccumulatorObject) GetTotalShares() osmomath.Dec
- func (accum AccumulatorObject) GetValue() sdk.DecCoins
- func (accum AccumulatorObject) HasPosition(name string) bool
- func (accum AccumulatorObject) MustGetPosition(name string) Record
- func (accum *AccumulatorObject) NewPosition(name string, numShareUnits osmomath.Dec, options *Options) error
- func (accum *AccumulatorObject) NewPositionIntervalAccumulation(name string, numShareUnits osmomath.Dec, ...) error
- func (accum *AccumulatorObject) RemoveFromPosition(name string, numSharesToRemove osmomath.Dec) error
- func (accum *AccumulatorObject) RemoveFromPositionIntervalAccumulation(name string, numSharesToRemove osmomath.Dec, ...) error
- func (accum *AccumulatorObject) SetPositionIntervalAccumulation(name string, intervalAccumulationPerShare sdk.DecCoins) error
- func (accum *AccumulatorObject) UpdatePosition(name string, numShares osmomath.Dec) error
- func (accum *AccumulatorObject) UpdatePositionIntervalAccumulation(name string, numShares osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins) error
- type NegativeAccDifferenceError
- type NegativeIntervalAccumulationPerShareError
- type NegativeRewardsAdditionError
- type NoPositionError
- type Options
- func (*Options) Descriptor() ([]byte, []int)
- func (m *Options) Marshal() (dAtA []byte, err error)
- func (m *Options) MarshalTo(dAtA []byte) (int, error)
- func (m *Options) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*Options) ProtoMessage()
- func (m *Options) Reset()
- func (m *Options) Size() (n int)
- func (m *Options) String() string
- func (m *Options) Unmarshal(dAtA []byte) error
- func (m *Options) XXX_DiscardUnknown()
- func (m *Options) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Options) XXX_Merge(src proto.Message)
- func (m *Options) XXX_Size() int
- func (m *Options) XXX_Unmarshal(b []byte) error
- type Record
- func (*Record) Descriptor() ([]byte, []int)
- func (m *Record) GetAccumValuePerShare() github_com_cosmos_cosmos_sdk_types.DecCoins
- func (m *Record) GetOptions() *Options
- func (m *Record) GetUnclaimedRewardsTotal() github_com_cosmos_cosmos_sdk_types.DecCoins
- func (m *Record) Marshal() (dAtA []byte, err error)
- func (m *Record) MarshalTo(dAtA []byte) (int, error)
- func (m *Record) MarshalToSizedBuffer(dAtA []byte) (int, error)
- func (*Record) ProtoMessage()
- func (m *Record) Reset()
- func (m *Record) Size() (n int)
- func (m *Record) String() string
- func (m *Record) Unmarshal(dAtA []byte) error
- func (m *Record) XXX_DiscardUnknown()
- func (m *Record) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *Record) XXX_Merge(src proto.Message)
- func (m *Record) XXX_Size() int
- func (m *Record) XXX_Unmarshal(b []byte) error
Constants ¶
const (
KeySeparator = "||" // needs to be different from other modules.
)
Variables ¶
var ( ErrInvalidLengthAccum = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowAccum = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupAccum = fmt.Errorf("proto: unexpected end of group") )
var (
)Functions ¶
func FormatPositionPrefixKey ¶ added in v0.0.5
FormatPositionPrefixKey returns the key prefix used specifically for position values in the KVStore. Returns "accum||pos||{accumName}||{name}" as bytes. We use a different key separator, namely `||`, to separate the accumulator name and the position name. This is because we require that accumName does not contain this as a substring.
func GetTotalRewards ¶ added in v0.0.5
func GetTotalRewards(accum *AccumulatorObject, position Record) sdk.DecCoins
Gets total unclaimed rewards, including existing and newly accrued unclaimed rewards
func MakeAccumulator ¶
Makes a new accumulator at store/accum/{accumName} Returns error if: * accumName already exists * there's some overlapping keys * Accumulator name contains "||"
func MakeAccumulatorWithValueAndShare ¶ added in v0.0.5
func MakeAccumulatorWithValueAndShare(accumStore store.KVStore, accumName string, accumValue sdk.DecCoins, totalShares osmomath.Dec) error
Makes a new accumulator at store/accum/{accumName} Returns error if: * accumName already exists * there's some overlapping keys * Accumulator name contains "||"
func OverwriteAccumulatorUnsafe ¶ added in v0.0.9
func OverwriteAccumulatorUnsafe(accumStore store.KVStore, accumName string, accumValue sdk.DecCoins, totalShares osmomath.Dec) error
OverwriteAccumulatorUnsafe overwrites the accumulator with the given name in accumStore. Use with caution as this method is only meant for use in migrations.
Types ¶
type AccumDoesNotExistError ¶
type AccumDoesNotExistError struct {
AccumName string
}
func (AccumDoesNotExistError) Error ¶
func (e AccumDoesNotExistError) Error() string
type AccumulatorContent ¶
type AccumulatorContent struct { AccumValue github_com_cosmos_cosmos_sdk_types.DecCoins `` /* 138-byte string literal not displayed */ }
AccumulatorContent is the state-entry for the global accumulator. It contains the name of the global accumulator and the total value of shares belonging to it from all positions.
func (*AccumulatorContent) Descriptor ¶
func (*AccumulatorContent) Descriptor() ([]byte, []int)
func (*AccumulatorContent) GetAccumValue ¶
func (m *AccumulatorContent) GetAccumValue() github_com_cosmos_cosmos_sdk_types.DecCoins
func (*AccumulatorContent) Marshal ¶
func (m *AccumulatorContent) Marshal() (dAtA []byte, err error)
func (*AccumulatorContent) MarshalTo ¶
func (m *AccumulatorContent) MarshalTo(dAtA []byte) (int, error)
func (*AccumulatorContent) MarshalToSizedBuffer ¶
func (m *AccumulatorContent) MarshalToSizedBuffer(dAtA []byte) (int, error)
func (*AccumulatorContent) ProtoMessage ¶
func (*AccumulatorContent) ProtoMessage()
func (*AccumulatorContent) Reset ¶
func (m *AccumulatorContent) Reset()
func (*AccumulatorContent) Size ¶
func (m *AccumulatorContent) Size() (n int)
func (*AccumulatorContent) String ¶
func (m *AccumulatorContent) String() string
func (*AccumulatorContent) Unmarshal ¶
func (m *AccumulatorContent) Unmarshal(dAtA []byte) error
func (*AccumulatorContent) XXX_DiscardUnknown ¶
func (m *AccumulatorContent) XXX_DiscardUnknown()
func (*AccumulatorContent) XXX_Marshal ¶
func (m *AccumulatorContent) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
func (*AccumulatorContent) XXX_Merge ¶
func (m *AccumulatorContent) XXX_Merge(src proto.Message)
func (*AccumulatorContent) XXX_Size ¶
func (m *AccumulatorContent) XXX_Size() int
func (*AccumulatorContent) XXX_Unmarshal ¶
func (m *AccumulatorContent) XXX_Unmarshal(b []byte) error
type AccumulatorObject ¶
type AccumulatorObject struct {
// contains filtered or unexported fields
}
We keep this object as a way to interface with the methods, even though only the Accumulator inside is stored in state
func GetAccumulator ¶
func GetAccumulator(accumStore store.KVStore, accumName string) (*AccumulatorObject, error)
Gets the current value of the accumulator corresponding to accumName in accumStore
func (*AccumulatorObject) AddToAccumulator ¶
func (accum *AccumulatorObject) AddToAccumulator(amt sdk.DecCoins)
AddToAccumulator updates the accumulator's value by amt. It does so by increasing the value of the accumulator by the given amount. Persists to store. Mutates the receiver.
func (*AccumulatorObject) AddToPosition ¶
func (accum *AccumulatorObject) AddToPosition(name string, newShares osmomath.Dec) error
AddToPosition adds newShares of shares to an existing position with the given name. This is functionally equivalent to claiming rewards, closing down the position, and opening a fresh one with the new number of shares. We can represent this behavior by claiming rewards and moving up the accumulator start value to its current value.
An alternative approach is to simply generate an additional position every time an address adds to its position. We do not pursue this path because we want to ensure that withdrawal and claiming functions remain constant time and do not scale with the number of times a user has added to their position.
Returns nil on success. Returns error when: - newShares are negative or zero. - there is no existing position at the given address - other internal or database error occurs.
func (*AccumulatorObject) AddToPositionIntervalAccumulation ¶ added in v0.0.5
func (accum *AccumulatorObject) AddToPositionIntervalAccumulation(name string, newShares osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins) error
AddToPositionIntervalAccumulation adds newShares of shares to an existing position with the given name. This is functionally equivalent to claiming rewards, closing down the position, and opening a fresh one with the new number of shares. The accumulator of the new position is set to given intervalAccumulationPerShare. intervalAccumulationPerShare DecCoin values must be non-negative. They must also be a superset of the old accumulator value associated with the position. Providing intervalAccumulationPerShare is useful for when the accumulation happens at a sub-range of the full accumulator rewards range. For example, a concentrated liquidity narrow range position.
An alternative approach is to simply generate an additional position every time an address adds to its position. We do not pursue this path because we want to ensure that withdrawal and claiming functions remain constant time and do not scale with the number of times a user has added to their position.
Returns nil on success. Returns error when: - newShares are negative or zero. - there is no existing position at the given address - other internal or database error occurs.
func (*AccumulatorObject) AddToUnclaimedRewards ¶ added in v0.0.5
func (accum *AccumulatorObject) AddToUnclaimedRewards(positionName string, rewardsToAddTotal sdk.DecCoins) error
AddToUnclaimedRewards adds the given amount of rewards to the unclaimed rewards for the given position. Returns error if no position exists for the given position name. Returns error if any database errors occur or if neggative rewards are provided.
func (*AccumulatorObject) ClaimRewards ¶
ClaimRewards claims the rewards for the given address, and returns the amount of rewards claimed. Upon claiming the rewards, the position at the current address is reset to have no unclaimed rewards. The position's accumulator is also set to the current accumulator value. The position state is removed if the position shares is equal to zero.
Returns error if - no position exists for the given address - any database errors occur.
func (*AccumulatorObject) DeletePosition ¶ added in v0.0.5
func (accum *AccumulatorObject) DeletePosition(positionName string) (sdk.DecCoins, error)
DeletePosition claims rewards and deletes the position from the accumulator state. Prior to deletion, claims rewards and returns them. Decrements total accumulator share counter by the number of shares in the position tracker. Returns error if: - fails to fetch a position - fails to claim rewards - fails to retrieve total accumulator shares
func (AccumulatorObject) GetName ¶ added in v0.0.5
func (accum AccumulatorObject) GetName() string
GetValue returns the current value of the accumulator.
func (AccumulatorObject) GetPosition ¶ added in v0.0.5
func (accum AccumulatorObject) GetPosition(name string) (Record, error)
GetPosition returns the position associated with the given address. If the position does not exist, returns an error.
func (*AccumulatorObject) GetPositionSize ¶
func (accum *AccumulatorObject) GetPositionSize(name string) (osmomath.Dec, error)
GetPositionSize returns the number of shares the position with the given name has in the accumulator. Returns error if position does not exist or if fails to retrieve position from state.
func (AccumulatorObject) GetTotalShares ¶
func (accum AccumulatorObject) GetTotalShares() osmomath.Dec
GetTotalShares returns the total number of shares in the accumulator
func (AccumulatorObject) GetValue ¶
func (accum AccumulatorObject) GetValue() sdk.DecCoins
GetValue returns the current value of the accumulator.
func (AccumulatorObject) HasPosition ¶
func (accum AccumulatorObject) HasPosition(name string) bool
HasPosition returns true if a position with the given name exists, false otherwise.
func (AccumulatorObject) MustGetPosition ¶ added in v0.0.5
func (accum AccumulatorObject) MustGetPosition(name string) Record
MustGetPosition returns the position associated with the given address. No errors in position retrieval are allowed.
func (*AccumulatorObject) NewPosition ¶
func (accum *AccumulatorObject) NewPosition(name string, numShareUnits osmomath.Dec, options *Options) error
NewPosition creates a new position for the given name, with the given number of share units. The name can be an owner's address, or any other unique identifier for a position. It takes a snapshot of the current accumulator value, and sets the position's initial value to that. The position is initialized with empty unclaimed rewards If there is an existing position for the given address, it is overwritten.
func (*AccumulatorObject) NewPositionIntervalAccumulation ¶ added in v0.0.5
func (accum *AccumulatorObject) NewPositionIntervalAccumulation(name string, numShareUnits osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins, options *Options) error
NewPositionIntervalAccumulation creates a new position for the given name, with the given number of share units. The name can be an owner's address, or any other unique identifier for a position. It sets the position's accumulator to the given value of intervalAccumulationPerShare. This is useful for when the accumulation happens at a sub-range of the full accumulator rewards range. For example, a concentrated liquidity narrow range position. intervalAccumulationPerShare DecCoin values are allowed to be negative. The position is initialized with empty unclaimed rewards If there is an existing position for the given address, it is overwritten.
func (*AccumulatorObject) RemoveFromPosition ¶
func (accum *AccumulatorObject) RemoveFromPosition(name string, numSharesToRemove osmomath.Dec) error
RemovePosition removes the specified number of shares from a position. Specifically, it claims the unclaimed and newly accrued rewards and returns them alongside the redeemed shares. Then, it overwrites the position record with the updated number of shares. Since it accrues rewards, it also moves up the position's accumulator value to the current accum val.
func (*AccumulatorObject) RemoveFromPositionIntervalAccumulation ¶ added in v0.0.5
func (accum *AccumulatorObject) RemoveFromPositionIntervalAccumulation(name string, numSharesToRemove osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins) error
RemovePositionIntervalAccumulation removes the specified number of shares from a position. Specifically, it claims the unclaimed and newly accrued rewards and returns them alongside the redeemed shares. Then, it overwrites the position record with the updated number of shares. Since it accrues rewards, it also resets the position's accumulator value to the given intervalAccumulationPerShare. Providing intervalAccumulationPerShare is useful for when the accumulation happens at a sub-range of the full accumulator rewards range. For example, a concentrated liquidity narrow range position. All intervalAccumulationPerShare DecCoin values must be non-negative. They must also be a superset of the old accumulator value associated with the position.
func (*AccumulatorObject) SetPositionIntervalAccumulation ¶ added in v0.0.5
func (accum *AccumulatorObject) SetPositionIntervalAccumulation(name string, intervalAccumulationPerShare sdk.DecCoins) error
SetPositionIntervalAccumulation sets the position's accumulator to the given value. This is useful for when the accumulation happens at a sub-range of the full accumulator rewards range. For example, a concentrated liquidity narrow range position. This method does not update shares or attempt to claim rewards. The new accumulator value must be greater than or equal to the old accumulator value. Returns nil on success, error otherwise.
func (*AccumulatorObject) UpdatePosition ¶
func (accum *AccumulatorObject) UpdatePosition(name string, numShares osmomath.Dec) error
UpdatePosition updates the position with the given name by adding or removing the given number of shares. If numShares is positive, it is equivalent to calling AddToPosition. If numShares is negative, it is equivalent to calling RemoveFromPosition. Also, it moves up the position's accumulator value to the current accum value. Fails with error if numShares is zero. Returns nil on success.
func (*AccumulatorObject) UpdatePositionIntervalAccumulation ¶ added in v0.0.5
func (accum *AccumulatorObject) UpdatePositionIntervalAccumulation(name string, numShares osmomath.Dec, intervalAccumulationPerShare sdk.DecCoins) error
UpdatePositionIntervalAccumulation updates the position with the given name by adding or removing the given number of shares. If numShares is positive, it is equivalent to calling AddToPositionIntervalAccumulation. If numShares is negative, it is equivalent to calling RemoveFromPositionIntervalAccumulation. Fails with error if numShares is zero. Returns nil on success. It also resets the position's accumulator value to the given intervalAccumulationPerShare. Providing intervalAccumulationPerShare is useful for when the accumulation happens at a sub-range of the full accumulator rewards range. For example, a concentrated liquidity narrow range position. All intervalAccumulationPerShare DecCoin value must be non-negative. They must also be a superset of the old accumulator value associated with the position.
type NegativeAccDifferenceError ¶
func (NegativeAccDifferenceError) Error ¶
func (e NegativeAccDifferenceError) Error() string
type NegativeIntervalAccumulationPerShareError ¶ added in v0.0.5
type NegativeIntervalAccumulationPerShareError struct {
}func (NegativeIntervalAccumulationPerShareError) Error ¶ added in v0.0.5
func (e NegativeIntervalAccumulationPerShareError) Error() string
type NegativeRewardsAdditionError ¶ added in v0.0.5
func (NegativeRewardsAdditionError) Error ¶ added in v0.0.5
func (e NegativeRewardsAdditionError) Error() string
type NoPositionError ¶
type NoPositionError struct {
Name string
}
func (NoPositionError) Error ¶
func (e NoPositionError) Error() string
type Options ¶
type Options struct { }
func (*Options) Descriptor ¶
func (*Options) MarshalToSizedBuffer ¶
func (*Options) ProtoMessage ¶
func (*Options) ProtoMessage()
func (*Options) XXX_DiscardUnknown ¶
func (m *Options) XXX_DiscardUnknown()
func (*Options) XXX_Marshal ¶
func (*Options) XXX_Unmarshal ¶
type Record ¶
type Record struct { // with this record. NumShares cosmossdk_io_math.LegacyDec `protobuf:"bytes,1,opt,name=num_shares,json=numShares,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"num_shares"` // accumulator value that allows to infer how much a position is entitled to // per share that it owns. // // In the default case with no intervals, this value equals to the global // accumulator value at the time of the position creation, the last update or // reward claim. // // In the interval case such as concentrated liquidity, this value equals to // the global growth of rewards inside the interval during one of: the time of // the position creation, the last update or reward claim. Note, that // immediately prior to claiming or updating rewards, this value must be // updated to "the growth inside at the time of last update + the growth // outside at the time of the current block". This is so that the claiming // logic can subtract this updated value from the global accumulator value to // get the growth inside the interval from the time of last update up until // the current block time. AccumValuePerShare github_com_cosmos_cosmos_sdk_types.DecCoins `` /* 166-byte string literal not displayed */ // unclaimed_rewards_total is the total amount of unclaimed rewards that the // position is entitled to. This value is updated whenever shares are added or // removed from an existing position. We also expose API for manually updating // this value for some custom use cases such as merging pre-existing positions // into a single one. UnclaimedRewardsTotal github_com_cosmos_cosmos_sdk_types.DecCoins `` /* 173-byte string literal not displayed */ Options *Options `protobuf:"bytes,4,opt,name=options,proto3" json:"options,omitempty"` }
Record corresponds to an individual position value belonging to the global accumulator.
func GetPosition ¶ added in v0.0.5
func GetPosition(accum *AccumulatorObject, name string) (Record, error)
Gets addr's current position from store
func (*Record) Descriptor ¶
func (*Record) GetAccumValuePerShare ¶ added in v0.0.5
func (m *Record) GetAccumValuePerShare() github_com_cosmos_cosmos_sdk_types.DecCoins
func (*Record) GetOptions ¶
func (*Record) GetUnclaimedRewardsTotal ¶ added in v0.0.5
func (m *Record) GetUnclaimedRewardsTotal() github_com_cosmos_cosmos_sdk_types.DecCoins
func (*Record) MarshalToSizedBuffer ¶
func (*Record) ProtoMessage ¶
func (*Record) ProtoMessage()
func (*Record) XXX_DiscardUnknown ¶
func (m *Record) XXX_DiscardUnknown()