diff --git a/util/sdkutil/events.go b/util/sdkutil/events.go new file mode 100644 index 0000000000..b1d31c1cdf --- /dev/null +++ b/util/sdkutil/events.go @@ -0,0 +1,14 @@ +package sdkutil + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/gogo/protobuf/proto" +) + +// Emit proto event and log on error +func Emit(ctx *sdk.Context, event proto.Message) { + err := ctx.EventManager().EmitTypedEvent(event) + if err != nil { + ctx.Logger().Error("emit event error", "err", err) + } +} diff --git a/x/leverage/keeper/interest.go b/x/leverage/keeper/interest.go index 5ded48e950..841f1494b8 100644 --- a/x/leverage/keeper/interest.go +++ b/x/leverage/keeper/interest.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util/sdkutil" "github.com/umee-network/umee/v4/x/leverage/types" ) @@ -172,10 +173,11 @@ func (k Keeper) AccrueAllInterest(ctx sdk.Context) error { "interest", totalInterest.String(), "reserved", newReserves.String(), ) - return ctx.EventManager().EmitTypedEvent(&types.EventInterestAccrual{ + sdkutil.Emit(&ctx, &types.EventInterestAccrual{ BlockHeight: uint64(ctx.BlockHeight()), Timestamp: uint64(currentTime), TotalInterest: totalInterest, Reserved: newReserves, }) + return nil } diff --git a/x/leverage/keeper/msg_server.go b/x/leverage/keeper/msg_server.go index 4cffc4cd8f..3f86a136c7 100644 --- a/x/leverage/keeper/msg_server.go +++ b/x/leverage/keeper/msg_server.go @@ -47,14 +47,14 @@ func (s msgServer) Supply( "supplied", msg.Asset.String(), "received", received.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventSupply{ + sdkutil.Emit(&ctx, &types.EventSupply{ Supplier: msg.Supplier, Asset: msg.Asset, Utoken: received, }) return &types.MsgSupplyResponse{ Received: received, - }, err + }, nil } func (s msgServer) Withdraw( @@ -83,10 +83,10 @@ func (s msgServer) Withdraw( return nil, err } - err = s.logWithdrawal(ctx, msg.Supplier, msg.Asset, received, "supplied assets withdrawn") + s.logWithdrawal(ctx, msg.Supplier, msg.Asset, received, "supplied assets withdrawn") return &types.MsgWithdrawResponse{ Received: received, - }, err + }, nil } func (s msgServer) MaxWithdraw( @@ -132,26 +132,25 @@ func (s msgServer) MaxWithdraw( return nil, err } - err = s.logWithdrawal(ctx, msg.Supplier, uToken, received, "maximum supplied assets withdrawn") + s.logWithdrawal(ctx, msg.Supplier, uToken, received, "maximum supplied assets withdrawn") return &types.MsgMaxWithdrawResponse{ Withdrawn: uToken, Received: received, - }, err + }, nil } -func (s msgServer) logWithdrawal(ctx sdk.Context, supplier string, redeemed, received sdk.Coin, desc string) error { +func (s msgServer) logWithdrawal(ctx sdk.Context, supplier string, redeemed, received sdk.Coin, desc string) { s.keeper.Logger(ctx).Debug( desc, "supplier", supplier, "redeemed", redeemed.String(), "received", received.String(), ) - err := ctx.EventManager().EmitTypedEvent(&types.EventWithdraw{ + sdkutil.Emit(&ctx, &types.EventWithdraw{ Supplier: supplier, Utoken: redeemed, Asset: received, }) - return err } func (s msgServer) Collateralize( @@ -181,11 +180,11 @@ func (s msgServer) Collateralize( "borrower", msg.Borrower, "amount", msg.Asset.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventCollaterize{ + sdkutil.Emit(&ctx, &types.EventCollaterize{ Borrower: msg.Borrower, Utoken: msg.Asset, }) - return &types.MsgCollateralizeResponse{}, err + return &types.MsgCollateralizeResponse{}, nil } func (s msgServer) SupplyCollateral( @@ -226,25 +225,23 @@ func (s msgServer) SupplyCollateral( "supplied", msg.Asset.String(), "received", uToken.String(), ) - if err = ctx.EventManager().EmitTypedEvent(&types.EventSupply{ - Supplier: msg.Supplier, - Asset: msg.Asset, - Utoken: uToken, - }); err != nil { - return nil, err - } s.keeper.Logger(ctx).Debug( "collateral added", "borrower", msg.Supplier, "amount", uToken.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventCollaterize{ + sdkutil.Emit(&ctx, &types.EventSupply{ + Supplier: msg.Supplier, + Asset: msg.Asset, + Utoken: uToken, + }) + sdkutil.Emit(&ctx, &types.EventCollaterize{ Borrower: msg.Supplier, Utoken: uToken, }) return &types.MsgSupplyCollateralResponse{ Collateralized: uToken, - }, err + }, nil } func (s msgServer) Decollateralize( @@ -272,11 +269,11 @@ func (s msgServer) Decollateralize( "borrower", msg.Borrower, "amount", msg.Asset.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventDecollaterize{ + sdkutil.Emit(&ctx, &types.EventDecollaterize{ Borrower: msg.Borrower, Utoken: msg.Asset, }) - return &types.MsgDecollateralizeResponse{}, err + return &types.MsgDecollateralizeResponse{}, nil } func (s msgServer) Borrow( @@ -314,11 +311,11 @@ func (s msgServer) Borrow( "borrower", msg.Borrower, "amount", msg.Asset.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventBorrow{ + sdkutil.Emit(&ctx, &types.EventBorrow{ Borrower: msg.Borrower, Asset: msg.Asset, }) - return &types.MsgBorrowResponse{}, err + return &types.MsgBorrowResponse{}, nil } func (s msgServer) MaxBorrow( @@ -365,13 +362,13 @@ func (s msgServer) MaxBorrow( "borrower", msg.Borrower, "amount", maxBorrow.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventBorrow{ + sdkutil.Emit(&ctx, &types.EventBorrow{ Borrower: msg.Borrower, Asset: maxBorrow, }) return &types.MsgMaxBorrowResponse{ Borrowed: maxBorrow, - }, err + }, nil } func (s msgServer) Repay( @@ -394,13 +391,13 @@ func (s msgServer) Repay( "attempted", msg.Asset.String(), "repaid", repaid.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventRepay{ + sdkutil.Emit(&ctx, &types.EventRepay{ Borrower: msg.Borrower, Repaid: repaid, }) return &types.MsgRepayResponse{ Repaid: repaid, - }, err + }, nil } func (s msgServer) Liquidate( @@ -431,7 +428,7 @@ func (s msgServer) Liquidate( "liquidated", liquidated.String(), "reward", reward.String(), ) - err = ctx.EventManager().EmitTypedEvent(&types.EventLiquidate{ + sdkutil.Emit(&ctx, &types.EventLiquidate{ Liquidator: msg.Liquidator, Borrower: msg.Borrower, Liquidated: liquidated, @@ -440,7 +437,7 @@ func (s msgServer) Liquidate( Repaid: repaid, Collateral: liquidated, Reward: reward, - }, err + }, nil } // GovUpdateRegistry updates existing tokens with new settings diff --git a/x/leverage/keeper/oracle.go b/x/leverage/keeper/oracle.go index f30f24352d..97dbe3bef6 100644 --- a/x/leverage/keeper/oracle.go +++ b/x/leverage/keeper/oracle.go @@ -4,6 +4,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/umee-network/umee/v4/util/sdkutil" oracletypes "github.com/umee-network/umee/v4/x/oracle/types" "github.com/umee-network/umee/v4/x/leverage/types" @@ -192,9 +193,7 @@ func (k Keeper) FundOracle(ctx sdk.Context, requested sdk.Coins) error { "funded oracle", "amount", rewards, ) - if err := ctx.EventManager().EmitTypedEvent(&types.EventFundOracle{Assets: rewards}); err != nil { - return err - } + sdkutil.Emit(&ctx, &types.EventFundOracle{Assets: rewards}) // Send rewards if !rewards.IsZero() { diff --git a/x/leverage/keeper/reserves.go b/x/leverage/keeper/reserves.go index a8bccf018a..caa4916dcf 100644 --- a/x/leverage/keeper/reserves.go +++ b/x/leverage/keeper/reserves.go @@ -3,6 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/util/sdkutil" "github.com/umee-network/umee/v4/x/leverage/types" ) @@ -88,12 +89,9 @@ func (k Keeper) RepayBadDebt(ctx sdk.Context, borrowerAddr sdk.AccAddress, denom "borrower", borrower, "asset", asset, ) - err := ctx.EventManager().EmitTypedEvent(&types.EventRepayBadDebt{ + sdkutil.Emit(&ctx, &types.EventRepayBadDebt{ Borrower: borrower, Asset: asset, }) - if err != nil { - return false, err - } } newModuleBalance := k.ModuleBalance(ctx, denom) @@ -107,13 +105,10 @@ func (k Keeper) RepayBadDebt(ctx sdk.Context, borrowerAddr sdk.AccAddress, denom "module balance", newModuleBalance, "reserves", newReserved, ) - err := ctx.EventManager().EmitTypedEvent(&types.EventReservesExhausted{ + sdkutil.Emit(&ctx, &types.EventReservesExhausted{ Borrower: borrower, OutstandingDebt: newBorrowed, ModuleBalance: newModuleBalance, Reserves: newReserved, }) - if err != nil { - return false, err - } } // True is returned on full repayment diff --git a/x/oracle/abci.go b/x/oracle/abci.go index 8caef23ef9..ca7c101521 100644 --- a/x/oracle/abci.go +++ b/x/oracle/abci.go @@ -64,11 +64,7 @@ func CalcPrices(ctx sdk.Context, params types.Params, k keeper.Keeper) error { return err } - // Set the exchange rate, emit ABCI event - if err = k.SetExchangeRateWithEvent(ctx, denom, exchangeRate); err != nil { - return err - } - + k.SetExchangeRateWithEvent(ctx, denom, exchangeRate) if k.IsPeriodLastBlock(ctx, params.HistoricStampPeriod) { k.AddHistoricPrice(ctx, denom, exchangeRate) } diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index 575a952b25..d5a37247ea 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -13,6 +13,7 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/tendermint/tendermint/libs/log" + "github.com/umee-network/umee/v4/util/sdkutil" "github.com/umee-network/umee/v4/x/oracle/types" ) @@ -124,9 +125,9 @@ func (k Keeper) SetExchangeRate(ctx sdk.Context, denom string, exchangeRate sdk. // SetExchangeRateWithEvent sets an consensus // exchange rate to the store with ABCI event -func (k Keeper) SetExchangeRateWithEvent(ctx sdk.Context, denom string, exchangeRate sdk.Dec) error { +func (k Keeper) SetExchangeRateWithEvent(ctx sdk.Context, denom string, exchangeRate sdk.Dec) { k.SetExchangeRate(ctx, denom, exchangeRate) - return ctx.EventManager().EmitTypedEvent(&types.EventSetFxRate{ + sdkutil.Emit(&ctx, &types.EventSetFxRate{ Denom: denom, Rate: exchangeRate, }) } diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index be836cc990..ba7dc2da80 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -206,8 +206,7 @@ func (s *IntegrationTestSuite) TestAggregateExchangeRateVoteError() { func (s *IntegrationTestSuite) TestSetExchangeRateWithEvent() { app, ctx := s.app, s.ctx - err := app.OracleKeeper.SetExchangeRateWithEvent(ctx, displayDenom, sdk.OneDec()) - s.Require().NoError(err) + app.OracleKeeper.SetExchangeRateWithEvent(ctx, displayDenom, sdk.OneDec()) rate, err := app.OracleKeeper.GetExchangeRate(ctx, displayDenom) s.Require().NoError(err) s.Require().Equal(rate, sdk.OneDec()) diff --git a/x/oracle/keeper/msg_server.go b/x/oracle/keeper/msg_server.go index 26f47992b5..0e5cd2ba6d 100644 --- a/x/oracle/keeper/msg_server.go +++ b/x/oracle/keeper/msg_server.go @@ -7,6 +7,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/umee-network/umee/v4/util/sdkutil" "github.com/umee-network/umee/v4/x/oracle/types" ) @@ -133,9 +134,9 @@ func (ms msgServer) DelegateFeedConsent( } ms.SetFeederDelegation(ctx, operatorAddr, delegateAddr) - err = ctx.EventManager().EmitTypedEvent(&types.EventDelegateFeedConsent{ - Operator: msg.Operator, Delegate: msg.Delegate, - }) - return &types.MsgDelegateFeedConsentResponse{}, err + sdkutil.Emit(&ctx, &types.EventDelegateFeedConsent{ + Operator: msg.Operator, Delegate: msg.Delegate}) + + return &types.MsgDelegateFeedConsentResponse{}, nil } diff --git a/x/uibc/quota/ibc_module.go b/x/uibc/quota/ibc_module.go index a7c5b565fd..170da86583 100644 --- a/x/uibc/quota/ibc_module.go +++ b/x/uibc/quota/ibc_module.go @@ -11,6 +11,7 @@ import ( channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types" + "github.com/umee-network/umee/v4/util/sdkutil" "github.com/umee-network/umee/v4/x/uibc" "github.com/umee-network/umee/v4/x/uibc/quota/keeper" ) @@ -40,7 +41,7 @@ func (im IBCMiddleware) OnAcknowledgementPacket(ctx sdk.Context, packet channelt } if _, ok := ack.Response.(*channeltypes.Acknowledgement_Error); ok { err := im.RevertQuotaUpdate(ctx, packet.Data) - emitOnRevertQuota(ctx, "acknowledgement", packet.Data, err) + emitOnRevertQuota(&ctx, "acknowledgement", packet.Data, err) } return im.IBCModule.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) @@ -49,7 +50,7 @@ func (im IBCMiddleware) OnAcknowledgementPacket(ctx sdk.Context, packet channelt // OnTimeoutPacket implements types.Middleware func (im IBCMiddleware) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error { err := im.RevertQuotaUpdate(ctx, packet.Data) - emitOnRevertQuota(ctx, "timeout", packet.Data, err) + emitOnRevertQuota(&ctx, "timeout", packet.Data, err) return im.IBCModule.OnTimeoutPacket(ctx, packet, relayer) } @@ -88,17 +89,14 @@ func ValidateReceiverAddress(packet channeltypes.Packet) error { // emitOnRevertQuota emits events related to quota update revert. // packetData is ICS 20 packet data bytes. -func emitOnRevertQuota(ctx sdk.Context, failureType string, packetData []byte, err error) { +func emitOnRevertQuota(ctx *sdk.Context, failureType string, packetData []byte, err error) { if err == nil { return } - logger := ctx.Logger() - logger.Error("revert quota update error", "err", err) - if err = ctx.EventManager().EmitTypedEvent(&uibc.EventBadRevert{ + ctx.Logger().Error("revert quota update error", "err", err) + sdkutil.Emit(ctx, &uibc.EventBadRevert{ Module: uibc.ModuleName, FailureType: failureType, Packet: string(packetData), - }); err != nil { - logger.Error("emit event error", "err", err) - } + }) }