Skip to content
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: add typed event for oracle post pices #1244

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1223](https://github.com/NibiruChain/nibiru/pull/1223) - chore(deps): bump github.com/golang/protobuf from 1.5.2 to 1.5.3
* [#1205](https://github.com/NibiruChain/nibiru/pull/1205) - test: first testing framework skeleton and example
* [#1228](https://github.com/NibiruChain/nibiru/pull/1228) - feat: update github.com/CosmWasm/wasmd 0.29.2
* [#1244](https://github.com/NibiruChain/nibiru/pull/1244) - feat: add typed event for oracle post price
* [#1237](https://github.com/NibiruChain/nibiru/pull/1237) - feat: reduce gas on openposition

### Bug Fixes
Expand Down
18 changes: 18 additions & 0 deletions proto/oracle/v1beta1/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package nibiru.oracle.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";

option go_package = "github.com/NibiruChain/nibiru/x/oracle/types";

// Emitted when a price is posted
message OraclePriceUpdate {
string pair = 1;
string price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 timestamp_ms = 3;
}
2 changes: 1 addition & 1 deletion x/epochs/types/state.pb.go

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

10 changes: 9 additions & 1 deletion x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,17 @@ func (k Keeper) SetPrice(ctx sdk.Context, pair asset.Pair, price sdk.Dec) {
k.ExchangeRates.Insert(ctx, pair, price)

key := collections.Join(pair, ctx.BlockTime())
timestampMs := ctx.BlockTime().UnixMilli()
k.PriceSnapshots.Insert(ctx, key, types.PriceSnapshot{
Pair: pair,
Price: price,
TimestampMs: ctx.BlockTime().UnixMilli(),
TimestampMs: timestampMs,
})
if err := ctx.EventManager().EmitTypedEvent(&types.OraclePriceUpdate{
Pair: pair.String(),
Price: price,
TimestampMs: timestampMs,
}); err != nil {
ctx.Logger().Error("failed to emit OraclePriceUpdate", "pair", pair, "error", err)
}
}
10 changes: 10 additions & 0 deletions x/oracle/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

testutilevents "github.com/NibiruChain/nibiru/x/common/testutil"

"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/oracle/types"
Expand Down Expand Up @@ -90,6 +92,14 @@ func TestQueryExchangeRateTwap(t *testing.T) {

rate := sdk.NewDec(1700)
input.OracleKeeper.SetPrice(input.Ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), rate)
testutilevents.RequireContainsTypedEvent(
t,
input.Ctx,
&types.OraclePriceUpdate{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
Price: rate,
TimestampMs: input.Ctx.BlockTime().UnixMilli()},
)

_, err := querier.ExchangeRateTwap(ctx, &types.QueryExchangeRateRequest{Pair: asset.Registry.Pair(denoms.ETH, denoms.NUSD)})
require.Error(t, err)
Expand Down
Loading