Skip to content

Commit

Permalink
[1658]: Create emitEvent and emitEvents that just logs any errors ins…
Browse files Browse the repository at this point in the history
…tead of returning them.
  • Loading branch information
SpicyLemon committed Oct 3, 2023
1 parent 5f885b8 commit d10eeec
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 30 deletions.
9 changes: 6 additions & 3 deletions x/exchange/keeper/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (k Keeper) FillBids(ctx sdk.Context, msg *exchange.MsgFillBidsRequest) erro
events[i] = exchange.NewEventOrderFilled(order.OrderId)
}

return ctx.EventManager().EmitTypedEvents(events...)
k.emitEvents(ctx, events)
return nil
}

// FillAsks settles one or more ask orders for a buyer.
Expand Down Expand Up @@ -281,7 +282,8 @@ func (k Keeper) FillAsks(ctx sdk.Context, msg *exchange.MsgFillAsksRequest) erro
events[i] = exchange.NewEventOrderFilled(order.OrderId)
}

return ctx.EventManager().EmitTypedEvents(events...)
k.emitEvents(ctx, events)
return nil
}

// SettleOrders attempts to settle all the provided orders.
Expand Down Expand Up @@ -411,5 +413,6 @@ func (k Keeper) SettleOrders(ctx sdk.Context, marketID uint32, askOrderIDs, bidO
))
}

return ctx.EventManager().EmitTypedEvents(events...)
k.emitEvents(ctx, events)
return nil
}
5 changes: 1 addition & 4 deletions x/exchange/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ func (k QueryServer) QueryValidateManageFees(goCtx context.Context, req *exchang
buyerRatios, msg.AddFeeBuyerSettlementRatios, msg.RemoveFeeBuyerSettlementRatios)...)
}

if err := k.UpdateFees(ctx, msg); err != nil {
// The only error this might be would be about event emission.
errs = append(errs, err)
}
k.UpdateFees(ctx, msg)
if err := k.ValidateMarket(ctx, msg.MarketId); err != nil {
errs = append(errs, err)
}
Expand Down
20 changes: 20 additions & 0 deletions x/exchange/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

"github.com/gogo/protobuf/proto"

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -53,6 +55,24 @@ func (k Keeper) logErrorf(ctx sdk.Context, msg string, args ...interface{}) {
ctx.Logger().Error(fmt.Sprintf(msg, args...), "module", "x/"+exchange.ModuleName)
}

// emitEvent emits the provided event and writes any error to the error log.
// See Also emitEvents.
func (k Keeper) emitEvent(ctx sdk.Context, event proto.Message) {
err := ctx.EventManager().EmitTypedEvent(event)
if err != nil {
k.logErrorf(ctx, "error emitting event %#v: %v", event, err)
}
}

// emitEvents emits the provided events and writes any error to the error log.
// See Also emitEvent.
func (k Keeper) emitEvents(ctx sdk.Context, events []proto.Message) {
err := ctx.EventManager().EmitTypedEvents(events...)
if err != nil {
k.logErrorf(ctx, "error emitting events %#v: %v", events, err)
}
}

// wrongAuthErr returns the error to use when a message's authority isn't what's required.
func (k Keeper) wrongAuthErr(badAuthority string) error {
return govtypes.ErrInvalidSigner.Wrapf("expected %s got %s", k.GetAuthority(), badAuthority)
Expand Down
24 changes: 16 additions & 8 deletions x/exchange/keeper/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,16 @@ func (k Keeper) ValidateBuyerSettlementFee(ctx sdk.Context, marketID uint32, pri
}

// UpdateFees updates all the fees as provided in the MsgGovManageFeesRequest.
func (k Keeper) UpdateFees(ctx sdk.Context, msg *exchange.MsgGovManageFeesRequest) error {
func (k Keeper) UpdateFees(ctx sdk.Context, msg *exchange.MsgGovManageFeesRequest) {
store := k.getStore(ctx)
updateCreateAskFlatFees(store, msg.MarketId, msg.RemoveFeeCreateAskFlat, msg.AddFeeCreateAskFlat)
updateCreateBidFlatFees(store, msg.MarketId, msg.RemoveFeeCreateBidFlat, msg.AddFeeCreateBidFlat)
updateSellerSettlementFlatFees(store, msg.MarketId, msg.RemoveFeeSellerSettlementFlat, msg.AddFeeSellerSettlementFlat)
updateSellerSettlementRatios(store, msg.MarketId, msg.RemoveFeeSellerSettlementRatios, msg.AddFeeSellerSettlementRatios)
updateBuyerSettlementFlatFees(store, msg.MarketId, msg.RemoveFeeBuyerSettlementFlat, msg.AddFeeBuyerSettlementFlat)
updateBuyerSettlementRatios(store, msg.MarketId, msg.RemoveFeeBuyerSettlementRatios, msg.AddFeeBuyerSettlementRatios)
return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketFeesUpdated(msg.MarketId))

k.emitEvent(ctx, exchange.NewEventMarketFeesUpdated(msg.MarketId))
}

// isMarketActive returns true if the provided market is accepting orders.
Expand Down Expand Up @@ -721,7 +722,8 @@ func (k Keeper) UpdateMarketActive(ctx sdk.Context, marketID uint32, active bool
return fmt.Errorf("market %d already has accepting-orders %t", marketID, active)
}
setMarketActive(store, marketID, active)
return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketActiveUpdated(marketID, updatedBy, active))
k.emitEvent(ctx, exchange.NewEventMarketActiveUpdated(marketID, updatedBy, active))
return nil
}

// IsUserSettlementAllowed gets whether user-settlement is allowed for a market.
Expand All @@ -738,7 +740,8 @@ func (k Keeper) UpdateUserSettlementAllowed(ctx sdk.Context, marketID uint32, al
return fmt.Errorf("market %d already has allow-user-settlement %t", marketID, allow)
}
setUserSettlementAllowed(store, marketID, allow)
return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketUserSettleUpdated(marketID, updatedBy, allow))
k.emitEvent(ctx, exchange.NewEventMarketUserSettleUpdated(marketID, updatedBy, allow))
return nil
}

// storeHasPermission returns true if there is an entry in the store for the given market, address, and permissions.
Expand Down Expand Up @@ -918,7 +921,8 @@ func (k Keeper) UpdatePermissions(ctx sdk.Context, msg *exchange.MsgMarketManage
return errors.Join(errs...)
}

return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketPermissionsUpdated(marketID, admin))
k.emitEvent(ctx, exchange.NewEventMarketPermissionsUpdated(marketID, admin))
return nil
}

// reqAttrKeyMaker is a function that returns a key for required attributes.
Expand Down Expand Up @@ -1085,7 +1089,8 @@ func (k Keeper) UpdateReqAttrs(ctx sdk.Context, msg *exchange.MsgMarketManageReq
return errors.Join(errs...)
}

return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketReqAttrUpdated(marketID, admin))
k.emitEvent(ctx, exchange.NewEventMarketReqAttrUpdated(marketID, admin))
return nil
}

// getMarketAccountByAddr gets a market's account given its address.
Expand Down Expand Up @@ -1135,8 +1140,9 @@ func (k Keeper) UpdateMarketDetails(ctx sdk.Context, marketID uint32, marketDeta

marketAcc.MarketDetails = marketDetails
k.accountKeeper.SetAccount(ctx, marketAcc)
k.emitEvent(ctx, exchange.NewEventMarketDetailsUpdated(marketID, updatedBy))

return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketDetailsUpdated(marketID, updatedBy))
return nil
}

// storeMarket writes all the market fields to the state store (except MarketDetails which are in the account).
Expand Down Expand Up @@ -1215,6 +1221,7 @@ func (k Keeper) CreateMarket(ctx sdk.Context, market exchange.Market) (uint32, e
k.accountKeeper.SetAccount(ctx, marketAcc)

storeMarket(store, market)
k.emitEvent(ctx, exchange.NewEventMarketCreated(market.MarketId))

return market.MarketId, nil
}
Expand Down Expand Up @@ -1278,7 +1285,8 @@ func (k Keeper) WithdrawMarketFunds(ctx sdk.Context, marketID uint32, toAddr sdk
if err != nil {
return fmt.Errorf("failed to withdraw %s from market %d: %w", amount, marketID, err)
}
return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketWithdraw(marketID, amount, toAddr, withdrawnBy))
k.emitEvent(ctx, exchange.NewEventMarketWithdraw(marketID, amount, toAddr, withdrawnBy))
return nil
}

// ValidateMarket checks the setup of the provided market, making sure there aren't any possibly problematic settings.
Expand Down
16 changes: 4 additions & 12 deletions x/exchange/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,11 @@ func (k MsgServer) GovCreateMarket(goCtx context.Context, msg *exchange.MsgGovCr
}

ctx := sdk.UnwrapSDKContext(goCtx)
marketID, err := k.CreateMarket(ctx, msg.Market)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())
}
err = ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketCreated(marketID))
_, err := k.CreateMarket(ctx, msg.Market)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())
}

return &exchange.MsgGovCreateMarketResponse{}, nil
}

Expand All @@ -198,10 +195,7 @@ func (k MsgServer) GovManageFees(goCtx context.Context, msg *exchange.MsgGovMana
}

ctx := sdk.UnwrapSDKContext(goCtx)
err := k.UpdateFees(ctx, msg)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())
}
k.UpdateFees(ctx, msg)

return &exchange.MsgGovManageFeesResponse{}, nil
}
Expand All @@ -214,9 +208,7 @@ func (k MsgServer) GovUpdateParams(goCtx context.Context, msg *exchange.MsgGovUp

ctx := sdk.UnwrapSDKContext(goCtx)
k.SetParams(ctx, &msg.Params)
k.emitEvent(ctx, exchange.NewEventParamsUpdated())

if err := ctx.EventManager().EmitTypedEvent(exchange.NewEventParamsUpdated()); err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error())
}
return &exchange.MsgGovUpdateParamsResponse{}, nil
}
9 changes: 6 additions & 3 deletions x/exchange/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ func (k Keeper) CreateAskOrder(ctx sdk.Context, askOrder exchange.AskOrder, crea
return 0, err
}

return orderID, ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCreated(order))
k.emitEvent(ctx, exchange.NewEventOrderCreated(order))
return orderID, nil
}

// CreateBidOrder creates a bid order, collects the creation fee, and places all needed holds.
Expand Down Expand Up @@ -642,7 +643,8 @@ func (k Keeper) CreateBidOrder(ctx sdk.Context, bidOrder exchange.BidOrder, crea
return 0, err
}

return orderID, ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCreated(order))
k.emitEvent(ctx, exchange.NewEventOrderCreated(order))
return orderID, nil
}

// CancelOrder releases an order's held funds and deletes it.
Expand All @@ -669,8 +671,9 @@ func (k Keeper) CancelOrder(ctx sdk.Context, orderID uint64, signer string) erro
}

deleteAndDeIndexOrder(k.getStore(ctx), *order)
k.emitEvent(ctx, exchange.NewEventOrderCancelled(orderID, signerAddr))

return ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCancelled(orderID, signerAddr))
return nil
}

// IterateOrders iterates over all orders. An error is returned if there was a problem
Expand Down

0 comments on commit d10eeec

Please sign in to comment.