Skip to content

Commit

Permalink
[accum][CL Incentives]: Track total shares for accumulators (#4204)
Browse files Browse the repository at this point in the history
* implement total share tracking for accumulators

* use seed in testing and rename test helper
  • Loading branch information
AlpinYukseloglu authored Feb 2, 2023
1 parent 9286922 commit 826832a
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 53 deletions.
45 changes: 38 additions & 7 deletions osmoutils/accum/accum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type AccumulatorObject struct {

// Accumulator's current value (pulled from AccumulatorContent)
value sdk.DecCoins

// Accumulator's total shares across all positions
totalShares sdk.Dec
}

// Makes a new accumulator at store/accum/{accumName}
Expand All @@ -33,11 +36,12 @@ func MakeAccumulator(accumStore store.KVStore, accumName string) error {
// New accumulator values start out at zero
// TODO: consider whether this should be a parameter instead of always zero
initAccumValue := sdk.NewDecCoins()
initTotalShares := sdk.ZeroDec()

newAccum := AccumulatorObject{accumStore, accumName, initAccumValue}
newAccum := AccumulatorObject{accumStore, accumName, initAccumValue, initTotalShares}

// Stores accumulator in state
setAccumulator(newAccum, initAccumValue)
setAccumulator(newAccum, initAccumValue, initTotalShares)

return nil
}
Expand All @@ -53,13 +57,13 @@ func GetAccumulator(accumStore store.KVStore, accumName string) (AccumulatorObje
return AccumulatorObject{}, AccumDoesNotExistError{AccumName: accumName}
}

accum := AccumulatorObject{accumStore, accumName, accumContent.AccumValue}
accum := AccumulatorObject{accumStore, accumName, accumContent.AccumValue, accumContent.TotalShares}

return accum, nil
}

func setAccumulator(accum AccumulatorObject, amt sdk.DecCoins) {
newAccum := AccumulatorContent{amt}
func setAccumulator(accum AccumulatorObject, value sdk.DecCoins, shares sdk.Dec) {
newAccum := AccumulatorContent{value, shares}
osmoutils.MustSet(accum.store, formatAccumPrefixKey(accum.name), &newAccum)
}

Expand All @@ -68,7 +72,7 @@ func setAccumulator(accum AccumulatorObject, amt sdk.DecCoins) {
// the given amount. Persists to store. Mutates the receiver.
func (accum *AccumulatorObject) AddToAccumulator(amt sdk.DecCoins) {
accum.value = accum.value.Add(amt...)
setAccumulator(*accum, accum.value)
setAccumulator(*accum, accum.value, accum.totalShares)
}

// NewPosition creates a new position for the given name, with the given number of share units.
Expand Down Expand Up @@ -96,6 +100,14 @@ func (accum AccumulatorObject) NewPositionCustomAcc(name string, numShareUnits s
}

initOrUpdatePosition(accum, customAccumulatorValue, name, numShareUnits, sdk.NewDecCoins(), options)

// Update total shares in accum (re-fetch accum from state to ensure it's up to date)
accum, err := GetAccumulator(accum.store, accum.name)
if err != nil {
return err
}
setAccumulator(accum, accum.value, accum.totalShares.Add(numShareUnits))

return nil
}

Expand Down Expand Up @@ -135,7 +147,7 @@ func (accum AccumulatorObject) AddToPosition(name string, newShares sdk.Dec) err
// - other internal or database error occurs.
func (accum AccumulatorObject) AddToPositionCustomAcc(name string, newShares sdk.Dec, customAccumulatorValue sdk.DecCoins) error {
if !newShares.IsPositive() {
return errors.New("Attempted to add a non-zero and non-negative number of shares to a position")
return errors.New("Attempted to add zero or negative number of shares to a position")
}

// Get addr's current position
Expand All @@ -159,6 +171,13 @@ func (accum AccumulatorObject) AddToPositionCustomAcc(name string, newShares sdk
// into UnclaimedRewards. Starting accumulator value is moved up to accum'scurrent value
initOrUpdatePosition(accum, customAccumulatorValue, name, oldNumShares.Add(newShares), unclaimedRewards, position.Options)

// Update total shares in accum (re-fetch accum from state to ensure it's up to date)
accum, err = GetAccumulator(accum.store, accum.name)
if err != nil {
return err
}
setAccumulator(accum, accum.value, accum.totalShares.Add(newShares))

return nil
}

Expand Down Expand Up @@ -207,6 +226,13 @@ func (accum AccumulatorObject) RemoveFromPositionCustomAcc(name string, numShare
// Update user's position with new number of shares
initOrUpdatePosition(accum, customAccumulatorValue, name, oldNumShares.Sub(numSharesToRemove), unclaimedRewards, position.Options)

// Update total shares in accum (re-fetch accum from state to ensure it's up to date)
accum, err = GetAccumulator(accum.store, accum.name)
if err != nil {
return err
}
setAccumulator(accum, accum.value, accum.totalShares.Sub(numSharesToRemove))

return nil
}

Expand Down Expand Up @@ -323,3 +349,8 @@ func (accum AccumulatorObject) ClaimRewards(positionName string) (sdk.Coins, err

return truncatedRewards, nil
}

// GetTotalShares returns the total number of shares in the accumulator
func (accum AccumulatorObject) GetTotalShares() sdk.Dec {
return accum.totalShares
}
102 changes: 75 additions & 27 deletions osmoutils/accum/accum.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 826832a

Please sign in to comment.