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

refactor(oracle)!: use typed events #1477

Merged
merged 8 commits into from
Jul 3, 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 @@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### State Machine Breaking

* [#1473](https://github.com/NibiruChain/nibiru/pull/1473) - refactor(perp)!: rename `OpenPosition` to `MarketOrder`
* [#1477](https://github.com/NibiruChain/nibiru/pull/1477) - refactor(oracle)!: Move away from deprecated events to typed events in x/oracle

### Dependencies

Expand Down
39 changes: 38 additions & 1 deletion proto/nibiru/oracle/v1/event.proto
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
syntax = "proto3";

Check failure on line 1 in proto/nibiru/oracle/v1/event.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present message "OraclePriceUpdate" was deleted from file.

package nibiru.oracle.v1;

import "nibiru/oracle/v1/oracle.proto";
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 {
message EventPriceUpdate {
string pair = 1;
string price = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
int64 timestamp_ms = 3;
}

// Emitted when a valoper delegates oracle voting rights to a feeder address.
message EventDelegateFeederConsent {
// Validator is the Bech32 address that is delegating voting rights.
string validator = 1;

// Feeder is the delegate or representative that will be able to send
// vote and prevote transaction messages.
string feeder = 2;
}

// Emitted by MsgAggregateExchangeVote when an aggregate vote is added to state
message EventAggregateVote {
// Validator is the Bech32 address to which the vote will be credited.
string validator = 1;

// Feeder is the delegate or representative that will send vote and prevote
// transaction messages on behalf of the voting validator.
string feeder = 2;

repeated ExchangeRateTuple prices = 3 [
(gogoproto.castrepeated) = "ExchangeRateTuples",
(gogoproto.nullable) = false
];
}

// Emitted by MsgAggregateExchangePrevote when an aggregate prevote is added
// to state
message EventAggregatePrevote {
// Validator is the Bech32 address to which the vote will be credited.
string validator = 1;

// Feeder is the delegate or representative that will send vote and prevote
// transaction messages on behalf of the voting validator.
string feeder = 2;
}
2 changes: 1 addition & 1 deletion x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (k Keeper) SetPrice(ctx sdk.Context, pair asset.Pair, price sdk.Dec) {
Price: price,
TimestampMs: timestampMs,
})
if err := ctx.EventManager().EmitTypedEvent(&types.OraclePriceUpdate{
if err := ctx.EventManager().EmitTypedEvent(&types.EventPriceUpdate{
Pair: pair.String(),
Price: price,
TimestampMs: timestampMs,
Expand Down
68 changes: 29 additions & 39 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,16 @@ func (ms msgServer) AggregateExchangeRatePrevote(

ms.Keeper.Prevotes.Insert(ctx, valAddr, types.NewAggregateExchangeRatePrevote(voteHash, valAddr, uint64(ctx.BlockHeight())))

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAggregatePrevote,
sdk.NewAttribute(types.AttributeKeyVoter, msg.Validator),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Feeder),
),
err = ctx.EventManager().EmitTypedEvent(&types.EventAggregatePrevote{
Validator: msg.Validator,
Feeder: msg.Feeder,
})

return &types.MsgAggregateExchangeRatePrevoteResponse{}, nil
return &types.MsgAggregateExchangeRatePrevoteResponse{}, err
}

func (ms msgServer) AggregateExchangeRateVote(goCtx context.Context, msg *types.MsgAggregateExchangeRateVote) (*types.MsgAggregateExchangeRateVoteResponse, error) {
func (ms msgServer) AggregateExchangeRateVote(
goCtx context.Context, msg *types.MsgAggregateExchangeRateVote,
) (msgResp *types.MsgAggregateExchangeRateVoteResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)

valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
Expand Down Expand Up @@ -118,30 +112,33 @@ func (ms msgServer) AggregateExchangeRateVote(goCtx context.Context, msg *types.
// Verify an exchange rate with aggregate prevote hash
hash := types.GetAggregateVoteHash(msg.Salt, msg.ExchangeRates, valAddr)
if aggregatePrevote.Hash != hash.String() {
return nil, sdkerrors.Wrapf(types.ErrVerificationFailed, "must be given %s not %s", aggregatePrevote.Hash, hash)
return nil, sdkerrors.Wrapf(
types.ErrVerificationFailed, "must be given %s not %s", aggregatePrevote.Hash, hash,
)
}

// Move aggregate prevote to aggregate vote with given exchange rates
ms.Keeper.Votes.Insert(ctx, valAddr, types.NewAggregateExchangeRateVote(exchangeRateTuples, valAddr))
ms.Keeper.Votes.Insert(
ctx, valAddr, types.NewAggregateExchangeRateVote(exchangeRateTuples, valAddr),
)
_ = ms.Keeper.Prevotes.Delete(ctx, valAddr)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAggregateVote,
sdk.NewAttribute(types.AttributeKeyVoter, msg.Validator),
sdk.NewAttribute(types.AttributeKeyExchangeRates, msg.ExchangeRates),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Feeder),
),
priceTuples, err := types.NewExchangeRateTuplesFromString(msg.ExchangeRates)
if err != nil {
return
}
err = ctx.EventManager().EmitTypedEvent(&types.EventAggregateVote{
Validator: msg.Validator,
Feeder: msg.Feeder,
Prices: priceTuples,
})

return &types.MsgAggregateExchangeRateVoteResponse{}, nil
return &types.MsgAggregateExchangeRateVoteResponse{}, err
}

func (ms msgServer) DelegateFeedConsent(goCtx context.Context, msg *types.MsgDelegateFeedConsent) (*types.MsgDelegateFeedConsentResponse, error) {
func (ms msgServer) DelegateFeedConsent(
goCtx context.Context, msg *types.MsgDelegateFeedConsent,
) (*types.MsgDelegateFeedConsentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

operatorAddr, err := sdk.ValAddressFromBech32(msg.Operator)
Expand All @@ -163,17 +160,10 @@ func (ms msgServer) DelegateFeedConsent(goCtx context.Context, msg *types.MsgDel
// Set the delegation
ms.Keeper.FeederDelegations.Insert(ctx, operatorAddr, delegateAddr)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeFeedDelegate,
sdk.NewAttribute(types.AttributeKeyFeeder, msg.Delegate),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Operator),
),
err = ctx.EventManager().EmitTypedEvent(&types.EventDelegateFeederConsent{
Feeder: msg.Delegate,
Validator: msg.Operator,
})

return &types.MsgDelegateFeedConsentResponse{}, nil
return &types.MsgDelegateFeedConsentResponse{}, err
}
2 changes: 1 addition & 1 deletion x/oracle/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestQueryExchangeRateTwap(t *testing.T) {
testutilevents.RequireContainsTypedEvent(
t,
input.Ctx,
&types.OraclePriceUpdate{
&types.EventPriceUpdate{
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD).String(),
Price: rate,
TimestampMs: input.Ctx.BlockTime().UnixMilli()},
Expand Down
11 changes: 5 additions & 6 deletions x/oracle/keeper/update_exchange_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ func (k Keeper) countVotesAndUpdateExchangeRates(

k.SetPrice(ctx, pair, exchangeRate)

ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeExchangeRateUpdate,
sdk.NewAttribute(types.AttributeKeyPair, pair.String()),
sdk.NewAttribute(types.AttributeKeyExchangeRate, exchangeRate.String()),
),
)
_ = ctx.EventManager().EmitTypedEvent(&types.EventPriceUpdate{
Pair: pairStr,
Price: exchangeRate,
TimestampMs: ctx.BlockTime().UnixMilli(),
})
}
}

Expand Down
Loading
Loading