Skip to content

Commit

Permalink
feat: helper function to emit events (#1777)
Browse files Browse the repository at this point in the history
Co-authored-by: Sai Kumar <17549398+gsk967@users.noreply.github.com>
  • Loading branch information
robert-zaremba and gsk967 authored Feb 3, 2023
1 parent 9a20198 commit b7eb055
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 64 deletions.
14 changes: 14 additions & 0 deletions util/sdkutil/events.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 3 additions & 1 deletion x/leverage/keeper/interest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
57 changes: 27 additions & 30 deletions x/leverage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -440,7 +437,7 @@ func (s msgServer) Liquidate(
Repaid: repaid,
Collateral: liquidated,
Reward: reward,
}, err
}, nil
}

// GovUpdateRegistry updates existing tokens with new settings
Expand Down
5 changes: 2 additions & 3 deletions x/leverage/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 3 additions & 8 deletions x/leverage/keeper/reserves.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 3 additions & 2 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
})
}
Expand Down
3 changes: 1 addition & 2 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
9 changes: 5 additions & 4 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
16 changes: 7 additions & 9 deletions x/uibc/quota/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
})
}

0 comments on commit b7eb055

Please sign in to comment.