-
Notifications
You must be signed in to change notification settings - Fork 193
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
feat(oracle): Add new DatedExchangeRate query with block timestamp and block height #2097
Changes from 6 commits
9cf67f2
3597fc7
8fe206e
79f8772
b052144
80520ec
1e46699
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 |
---|---|---|
|
@@ -187,6 +187,16 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, pair asset.Pair) (price sdk.Dec | |
return | ||
} | ||
|
||
func (k Keeper) GetDatedExchangeRate(ctx sdk.Context, pair asset.Pair) (price sdk.Dec, blockTimeMs int64, BlockHeight uint64, err error) { | ||
exchangeRate, err := k.ExchangeRates.Get(ctx, pair) | ||
if err != nil { | ||
return | ||
} | ||
Comment on lines
+191
to
+194
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. 🛠️ Refactor suggestion Enhance error handling with specific error types. The current error handling doesn't provide specific information about why the exchange rate retrieval failed. Consider wrapping the error with additional context. exchangeRate, err := k.ExchangeRates.Get(ctx, pair)
if err != nil {
- return
+ return sdk.Dec{}, 0, 0, sdkerrors.Wrapf(types.ErrNoValidPrice, "failed to get exchange rate for pair %s: %v", pair, err)
}
|
||
time := ctx.WithBlockHeight(int64(exchangeRate.CreatedBlock)).BlockTime() | ||
Unique-Divine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return exchangeRate.ExchangeRate, time.UnixMilli(), exchangeRate.CreatedBlock, nil | ||
} | ||
|
||
// SetPrice sets the price for a pair as well as the price snapshot. | ||
func (k Keeper) SetPrice(ctx sdk.Context, pair asset.Pair, price sdk.Dec) { | ||
k.ExchangeRates.Insert(ctx, pair, types.DatedPrice{ExchangeRate: price, CreatedBlock: uint64(ctx.BlockHeight())}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,111 @@ func TestQueryExchangeRateTwap(t *testing.T) { | |
require.Equal(t, math.LegacyMustNewDecFromStr("1700"), res.ExchangeRate) | ||
} | ||
|
||
func TestQueryDatedExchangeRate(t *testing.T) { | ||
input := CreateTestFixture(t) | ||
querier := NewQuerier(input.OracleKeeper) | ||
|
||
// Set initial block time and height | ||
initialTime := input.Ctx.BlockTime() | ||
initialHeight := input.Ctx.BlockHeight() | ||
|
||
// Pair 1: BTC/NUSD | ||
pairBTC := asset.Registry.Pair(denoms.BTC, denoms.NUSD) | ||
rateBTC1 := math.LegacyNewDec(1700) | ||
rateBTC2 := math.LegacyNewDec(1800) | ||
|
||
// Pair 2: ETH/NUSD | ||
pairETH := asset.Registry.Pair(denoms.ETH, denoms.NUSD) | ||
rateETH1 := math.LegacyNewDec(100) | ||
rateETH2 := math.LegacyNewDec(110) | ||
|
||
Comment on lines
+121
to
+138
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. 🛠️ Refactor suggestion Consider using table-driven tests for better coverage. While the test setup is well-structured, consider converting it to a table-driven test to cover more edge cases such as:
|
||
// --- Set first price for BTC/NUSD --- | ||
input.OracleKeeper.SetPrice(input.Ctx, pairBTC, rateBTC1) | ||
testutilevents.RequireContainsTypedEvent( | ||
t, | ||
input.Ctx, | ||
&types.EventPriceUpdate{ | ||
Pair: pairBTC.String(), | ||
Price: rateBTC1, | ||
TimestampMs: input.Ctx.BlockTime().UnixMilli(), | ||
}, | ||
) | ||
|
||
// Advance time and block height | ||
input.Ctx = input.Ctx.WithBlockTime(initialTime.Add(1 * time.Second)). | ||
WithBlockHeight(initialHeight + 1) | ||
|
||
// --- Set first price for ETH/NUSD --- | ||
input.OracleKeeper.SetPrice(input.Ctx, pairETH, rateETH1) | ||
testutilevents.RequireContainsTypedEvent( | ||
t, | ||
input.Ctx, | ||
&types.EventPriceUpdate{ | ||
Pair: pairETH.String(), | ||
Price: rateETH1, | ||
TimestampMs: input.Ctx.BlockTime().UnixMilli(), | ||
}, | ||
) | ||
|
||
// Advance time and block height | ||
input.Ctx = input.Ctx.WithBlockTime(initialTime.Add(2 * time.Second)). | ||
WithBlockHeight(initialHeight + 2) | ||
|
||
// --- Set second price for BTC/NUSD --- | ||
input.OracleKeeper.SetPrice(input.Ctx, pairBTC, rateBTC2) | ||
testutilevents.RequireContainsTypedEvent( | ||
t, | ||
input.Ctx, | ||
&types.EventPriceUpdate{ | ||
Pair: pairBTC.String(), | ||
Price: rateBTC2, | ||
TimestampMs: input.Ctx.BlockTime().UnixMilli(), | ||
}, | ||
) | ||
|
||
// Advance time and block height | ||
input.Ctx = input.Ctx.WithBlockTime(initialTime.Add(3 * time.Second)). | ||
WithBlockHeight(initialHeight + 3) | ||
|
||
// --- Set second price for ETH/NUSD --- | ||
input.OracleKeeper.SetPrice(input.Ctx, pairETH, rateETH2) | ||
testutilevents.RequireContainsTypedEvent( | ||
t, | ||
input.Ctx, | ||
&types.EventPriceUpdate{ | ||
Pair: pairETH.String(), | ||
Price: rateETH2, | ||
TimestampMs: input.Ctx.BlockTime().UnixMilli(), | ||
}, | ||
) | ||
Comment on lines
+166
to
+197
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. 🛠️ Refactor suggestion Extract helper functions to reduce code duplication. Consider creating helper functions to reduce repetition: + func setPriceAndVerifyEvent(
+ t *testing.T,
+ ctx sdk.Context,
+ k Keeper,
+ pair asset.Pair,
+ price math.LegacyDec,
+ ) {
+ k.SetPrice(ctx, pair, price)
+ testutilevents.RequireContainsTypedEvent(
+ t,
+ ctx,
+ &types.EventPriceUpdate{
+ Pair: pair.String(),
+ Price: price,
+ TimestampMs: ctx.BlockTime().UnixMilli(),
+ BlockHeight: ctx.BlockHeight(),
+ },
+ )
+ }
|
||
|
||
// Wrap context for querying | ||
ctx := sdk.WrapSDKContext(input.Ctx) | ||
|
||
// --- Query latest snapshot for BTC/NUSD --- | ||
resBTC, err := querier.DatedExchangeRate( | ||
ctx, | ||
&types.QueryExchangeRateRequest{Pair: pairBTC}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, rateBTC2, resBTC.Price) | ||
require.Equal(t, input.Ctx.BlockTime().UnixMilli(), resBTC.BlockTimestampMs) | ||
|
||
// --- Query latest snapshot for ETH/NUSD --- | ||
resETH, err := querier.DatedExchangeRate( | ||
ctx, | ||
&types.QueryExchangeRateRequest{Pair: pairETH}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, rateETH2, resETH.Price) | ||
require.Equal(t, input.Ctx.BlockTime().UnixMilli(), resETH.BlockTimestampMs) | ||
|
||
// --- Query a pair with no snapshots (should return an error) --- | ||
pairATOM := asset.Registry.Pair(denoms.ATOM, denoms.NUSD) | ||
_, err = querier.DatedExchangeRate(ctx, &types.QueryExchangeRateRequest{Pair: pairATOM}) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestCalcTwap(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
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.
🛠️ Refactor suggestion
Improve test maintainability with explicit value calculations.
The test assertions would be clearer with explicit calculations of expected values. This would make the relationship between inputs and expected outputs more obvious.
📝 Committable suggestion