-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove using an iterator from updating TWAP records #7266
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,23 @@ func (k Keeper) GetAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) ( | |
return types.GetAllMostRecentTwapsForPool(store, poolId) | ||
} | ||
|
||
// GetAllMostRecentRecordsForPool returns all most recent twap records | ||
// (in state representation) for the provided pool id. | ||
func (k Keeper) GetAllMostRecentRecordsForPoolWithDenoms(ctx sdk.Context, poolId uint64, denoms []string) ([]types.TwapRecord, error) { | ||
store := ctx.KVStore(k.storeKey) | ||
// if length != 2, use iterator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 🌮 |
||
if len(denoms) != 2 { | ||
return types.GetAllMostRecentTwapsForPool(store, poolId) | ||
} | ||
// else, directly fetch the key. | ||
asset0Denom, asset1Denom, err := types.LexicographicalOrderDenoms(denoms[0], denoms[1]) | ||
if err != nil { | ||
return []types.TwapRecord{}, err | ||
} | ||
record, err := types.GetMostRecentTwapForPool(store, poolId, asset0Denom, asset1Denom) | ||
return []types.TwapRecord{record}, err | ||
} | ||
|
||
// getAllHistoricalTimeIndexedTWAPs returns all historical TWAPs indexed by time. | ||
func (k Keeper) GetAllHistoricalTimeIndexedTWAPs(ctx sdk.Context) ([]types.TwapRecord, error) { | ||
return osmoutils.GatherValuesFromStorePrefix(ctx.KVStore(k.storeKey), []byte(types.HistoricalTWAPTimeIndexPrefix), types.ParseTwapFromBz) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,12 @@ func GetAllMostRecentTwapsForPool(store sdk.KVStore, poolId uint64) ([]TwapRecor | |
return osmoutils.GatherValuesFromStore(store, []byte(startPrefix), []byte(endPrefix), ParseTwapFromBz) | ||
} | ||
|
||
func GetMostRecentTwapForPool(store sdk.KVStore, poolId uint64, denom1, denom2 string) (TwapRecord, error) { | ||
key := FormatMostRecentTWAPKey(poolId, denom1, denom2) | ||
bz := store.Get(key) | ||
return ParseTwapFromBz(bz) | ||
} | ||
Comment on lines
+95
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: would it be better to have this within keeper since it touches store? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having this here makes sense given prev code structure (e.g GetAllMostRecentTwapsForPool also lives in types/keys.go) |
||
|
||
func ParseTwapFromBz(bz []byte) (twap TwapRecord, err error) { | ||
if len(bz) == 0 { | ||
return TwapRecord{}, errors.New("twap not found") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have unit test for this method?