Skip to content

Commit

Permalink
twap record upgrade handler (#5363)
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic authored May 31, 2023
1 parent 20650cf commit fe30995
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
10 changes: 10 additions & 0 deletions app/upgrades/v16/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ func CreateUpgradeHandler(
return nil, err
}

clPoolTwapRecords, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, clPoolId)
if err != nil {
return nil, err
}

for _, twapRecord := range clPoolTwapRecords {
twapRecord.LastErrorTime = time.Time{}
keepers.TwapKeeper.StoreNewRecord(ctx, twapRecord)
}

updateTokenFactoryParams(ctx, keepers.TokenFactoryKeeper)

// Transfers out all the dev fees in kvstore to dev account during upgrade
Expand Down
8 changes: 0 additions & 8 deletions x/twap/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ type (
GeometricTwapStrategy = geometric
)

func (k Keeper) StoreNewRecord(ctx sdk.Context, record types.TwapRecord) {
k.storeNewRecord(ctx, record)
}

func (k Keeper) GetMostRecentRecordStoreRepresentation(ctx sdk.Context, poolId uint64, asset0Denom string, asset1Denom string) (types.TwapRecord, error) {
return k.getMostRecentRecordStoreRepresentation(ctx, poolId, asset0Denom, asset1Denom)
}

func (k Keeper) GetAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) ([]types.TwapRecord, error) {
return k.getAllMostRecentRecordsForPool(ctx, poolId)
}

func (k Keeper) GetRecordAtOrBeforeTime(ctx sdk.Context, poolId uint64, time time.Time, asset0Denom string, asset1Denom string) (types.TwapRecord, error) {
return k.getRecordAtOrBeforeTime(ctx, poolId, time, asset0Denom, asset1Denom)
}
Expand Down
2 changes: 1 addition & 1 deletion x/twap/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
})

for _, twap := range genState.Twaps {
k.storeNewRecord(ctx, twap)
k.StoreNewRecord(ctx, twap)
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/twap/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (k Keeper) afterCreatePool(ctx sdk.Context, poolId uint64) error {
// that there is a swap against this pool in this same block.
// furthermore, this protects against an edge case where a pool is created
// during EndBlock, after twapkeeper's endblock.
k.storeNewRecord(ctx, record)
k.StoreNewRecord(ctx, record)
}
k.trackChangedPool(ctx, poolId)
return err
Expand Down Expand Up @@ -124,7 +124,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) {
// number of denoms in the pool.
func (k Keeper) updateRecords(ctx sdk.Context, poolId uint64) error {
// Will only err if pool doesn't have most recent entry set
records, err := k.getAllMostRecentRecordsForPool(ctx, poolId)
records, err := k.GetAllMostRecentRecordsForPool(ctx, poolId)
if err != nil {
return err
}
Expand All @@ -148,7 +148,7 @@ func (k Keeper) updateRecords(ctx sdk.Context, poolId uint64) error {
if err != nil {
return err
}
k.storeNewRecord(ctx, newRecord)
k.StoreNewRecord(ctx, newRecord)
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions x/twap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ func (k Keeper) getMostRecentRecordStoreRepresentation(ctx sdk.Context, poolId u
return twap, err
}

// getAllMostRecentRecordsForPool returns all most recent twap records
// GetAllMostRecentRecordsForPool returns all most recent twap records
// (in state representation) for the provided pool id.
func (k Keeper) getAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) ([]types.TwapRecord, error) {
func (k Keeper) GetAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) ([]types.TwapRecord, error) {
store := ctx.KVStore(k.storeKey)
return types.GetAllMostRecentTwapsForPool(store, poolId)
}
Expand All @@ -162,8 +162,8 @@ func (k Keeper) getAllHistoricalPoolIndexedTWAPs(ctx sdk.Context) ([]types.TwapR
return osmoutils.GatherValuesFromStorePrefix(ctx.KVStore(k.storeKey), []byte(types.HistoricalTWAPPoolIndexPrefix), types.ParseTwapFromBz)
}

// storeNewRecord stores a record, in both the most recent record store and historical stores.
func (k Keeper) storeNewRecord(ctx sdk.Context, twap types.TwapRecord) {
// StoreNewRecord stores a record, in both the most recent record store and historical stores.
func (k Keeper) StoreNewRecord(ctx sdk.Context, twap types.TwapRecord) {
store := ctx.KVStore(k.storeKey)
key := types.FormatMostRecentTWAPKey(twap.PoolId, twap.Asset0Denom, twap.Asset1Denom)
osmoutils.MustSet(store, key, &twap)
Expand Down
6 changes: 3 additions & 3 deletions x/twap/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (t TwapRecord) validate() error {
// if there was an error in this record, one of the spot prices should be 0.
// else, the spot prices must be positive for both spot prices.
if t.LastErrorTime.Equal(t.Time) {
isP0LastSpotPriseZero := t.P0LastSpotPrice.IsNil() || t.P0LastSpotPrice.IsZero()
isP1LastSpotPriseZero := t.P1LastSpotPrice.IsNil() || t.P1LastSpotPrice.IsZero()
isP0LastSpotPriceZero := t.P0LastSpotPrice.IsNil() || t.P0LastSpotPrice.IsZero()
isP1LastSpotPriceZero := t.P1LastSpotPrice.IsNil() || t.P1LastSpotPrice.IsZero()

if !isP0LastSpotPriseZero && !isP1LastSpotPriseZero {
if !isP0LastSpotPriceZero && !isP1LastSpotPriceZero {
return fmt.Errorf("one of twap record p0 and p1 last spot price must be zero due to having an error, was (%s, %s)", t.P0LastSpotPrice, t.P1LastSpotPrice)
}
} else {
Expand Down

0 comments on commit fe30995

Please sign in to comment.