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: Monitoring Events #667

Merged
merged 19 commits into from
Oct 20, 2023
43 changes: 43 additions & 0 deletions x/liquidstakeibc/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package keeper

import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -82,11 +85,27 @@ func (k *Keeper) DoDelegate(ctx sdk.Context, hc *types.HostChain) {
}

// if everything went well, update the deposit states and set the sequence id
lastEpoch := int64(0) // highest epoch among deposits, used for event emission
for _, deposit := range deposits {
deposit.IbcSequenceId = sequenceID
deposit.State = types.Deposit_DEPOSIT_DELEGATING
k.SetDeposit(ctx, deposit)

kruspy marked this conversation as resolved.
Show resolved Hide resolved
lastEpoch = deposit.Epoch
}

// emit the delegation event
encMsgs, _ := json.Marshal(&messages)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDoDelegation,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeEpoch, strconv.FormatInt(lastEpoch, 10)),
sdk.NewAttribute(types.AttributeTotalDelegatedAmount, sdk.NewCoin(hc.HostDenom, totalDepositDelegation).String()),
sdk.NewAttribute(types.AttributeICAMessages, base64.StdEncoding.EncodeToString(encMsgs)),
sdk.NewAttribute(types.AttributeIBCSequenceID, sequenceID),
),
)
}

func (k *Keeper) DoClaim(ctx sdk.Context, hc *types.HostChain) {
Expand Down Expand Up @@ -114,12 +133,15 @@ func (k *Keeper) DoClaim(ctx sdk.Context, hc *types.HostChain) {
}

var claimableCoins sdk.Coins
var eventAmount sdk.Coin // used for claim events
switch unbonding.State {
case types.Unbonding_UNBONDING_CLAIMABLE:
claimableCoins = sdk.NewCoins(sdk.NewCoin(hc.IBCDenom(), userUnbonding.UnbondAmount.Amount))
eventAmount = sdk.NewCoin(hc.HostDenom, userUnbonding.UnbondAmount.Amount)
unbonding.UnbondAmount = unbonding.UnbondAmount.Sub(userUnbonding.UnbondAmount)
case types.Unbonding_UNBONDING_FAILED:
claimableCoins = sdk.NewCoins(sdk.NewCoin(hc.MintDenom(), userUnbonding.StkAmount.Amount))
eventAmount = sdk.NewCoin(hc.MintDenom(), userUnbonding.StkAmount.Amount)
unbonding.BurnAmount = unbonding.BurnAmount.Sub(userUnbonding.StkAmount)
}

Expand Down Expand Up @@ -148,6 +170,16 @@ func (k *Keeper) DoClaim(ctx sdk.Context, hc *types.HostChain) {
}

k.DeleteUserUnbonding(ctx, userUnbonding)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeClaimedUnbondings,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeEpoch, strconv.FormatInt(epochNumber, 10)),
sdk.NewAttribute(types.AttributeClaimAmount, eventAmount.String()),
sdk.NewAttribute(types.AttributeClaimAddress, userUnbonding.Address),
),
)
}
}
}
Expand Down Expand Up @@ -306,4 +338,15 @@ func (k *Keeper) DoRedeemLSMTokens(ctx sdk.Context, hc *types.HostChain) {
"sequence-id",
sequenceID,
)

// emit the untokenize event
encMsgs, _ := json.Marshal(&messages)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRedeemTokensForShares,
sdk.NewAttribute(types.AttributeChainID, hc.ChainId),
sdk.NewAttribute(types.AttributeICAMessages, base64.StdEncoding.EncodeToString(encMsgs)),
sdk.NewAttribute(types.AttributeIBCSequenceID, sequenceID),
),
)
}
164 changes: 154 additions & 10 deletions x/liquidstakeibc/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package keeper

import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"time"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -178,6 +182,16 @@ func (k *Keeper) OnRecvIBCTransferPacket(
unbonding.IbcSequenceId = ""
unbonding.State = liquidstakeibctypes.Unbonding_UNBONDING_CLAIMABLE
k.SetUnbonding(ctx, unbonding)

// emit event for the received transfer
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeUnbondingMaturedReceived,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeEpoch, strconv.FormatInt(unbonding.EpochNumber, 10)),
sdk.NewAttribute(liquidstakeibctypes.AttributeUnbondingMaturedAmount, sdk.NewCoin(hc.HostDenom, unbonding.UnbondAmount.Amount).String()),
),
)
}
}

Expand Down Expand Up @@ -220,6 +234,14 @@ func (k *Keeper) OnRecvIBCTransferPacket(

deposit.Amount.Amount = deposit.Amount.Amount.Add(transferAmount)
k.SetDeposit(ctx, deposit)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeValidatorUnbondingMaturedReceived,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeValidatorUnbondingMaturedAmount, sdk.NewCoin(hc.HostDenom, transferAmount).String()),
),
)
}

// the transfer is part of the autocompounding process
Expand Down Expand Up @@ -283,6 +305,16 @@ func (k *Keeper) OnRecvIBCTransferPacket(
// update the deposit
deposit.Amount.Amount = deposit.Amount.Amount.Add(transferAmount.Sub(feeAmount.TruncateInt()))
k.SetDeposit(ctx, deposit)

// emit autocompound received event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventAutocompoundRewardsReceived,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeAutocompoundTransfer, sdk.NewCoin(hc.HostDenom, transferAmount).String()),
sdk.NewAttribute(liquidstakeibctypes.AttributePstakeAutocompoundFee, sdk.NewCoin(hc.HostDenom, feeAmount.TruncateInt()).String()),
),
)
}

return nil
Expand Down Expand Up @@ -353,11 +385,36 @@ func (k *Keeper) OnAcknowledgementIBCTransferPacket(
"channel",
packet.SourceChannel,
)

// emit events for the deposits received
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventStakingDepositTransferReceived,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
),
)
}

// mark tokenized LSM token delegations as received and add the IBC sequence
lsmDeposits := k.GetLSMDepositsFromIbcSequenceID(ctx, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence))
k.UpdateLSMDepositsStateAndSequence(ctx, lsmDeposits, liquidstakeibctypes.LSMDeposit_DEPOSIT_RECEIVED, "")

// emit events for the lsm deposits received
for _, lsmDeposit := range lsmDeposits {
hc, found := k.GetHostChain(ctx, lsmDeposit.ChainId)
if !found {
return fmt.Errorf("host chain with id %s is not registered", lsmDeposit.ChainId)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventLSMDepositTransferReceived,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
),
)
}
}

return nil
Expand All @@ -381,16 +438,44 @@ func (k *Keeper) OnTimeoutIBCTransferPacket(
// just take action when the transfer has been, send from the deposit module account
if data.GetSender() == authtypes.NewModuleAddress(liquidstakeibctypes.DepositModuleAccount).String() {
// revert the state of the deposits that timed out
k.RevertDepositsState(
ctx,
k.GetDepositsWithSequenceID(ctx, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
)
deposits := k.GetDepositsWithSequenceID(ctx, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence))
k.RevertDepositsState(ctx, deposits)

// emit events for the deposits that timed out
for _, deposit := range deposits {
hc, found := k.GetHostChain(ctx, deposit.ChainId)
if !found {
return fmt.Errorf("host chain with id %s is not registered", deposit.ChainId)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventStakingDepositTransferTimeout,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
),
)
}

// revert the state of the LSM deposits that timed out
k.RevertLSMDepositsState(
ctx,
k.GetLSMDepositsFromIbcSequenceID(ctx, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
)
lsmDeposits := k.GetLSMDepositsFromIbcSequenceID(ctx, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence))
k.RevertLSMDepositsState(ctx, lsmDeposits)

// emit events for the lsm deposits that timed out
for _, lsmDeposit := range lsmDeposits {
hc, found := k.GetHostChain(ctx, lsmDeposit.ChainId)
if !found {
return fmt.Errorf("host chain with id %s is not registered", lsmDeposit.ChainId)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventLSMDepositTransferTimeout,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, k.GetTransactionSequenceID(packet.SourceChannel, packet.Sequence)),
),
)
}
}

k.Logger(ctx).Info(
Expand Down Expand Up @@ -442,7 +527,7 @@ func (k *Keeper) DepositWorkflow(ctx sdk.Context, epoch int64) {

timeoutHeight := clienttypes.NewHeight(
clientState.GetLatestHeight().GetRevisionNumber(),
clientState.GetLatestHeight().GetRevisionHeight()+liquidstakeibctypes.IBCTimeoutHeightIncrement,
clientState.GetLatestHeight().GetRevisionHeight()+1,
)

msg := ibctransfertypes.NewMsgTransfer(
Expand Down Expand Up @@ -474,6 +559,16 @@ func (k *Keeper) DepositWorkflow(ctx sdk.Context, epoch int64) {
deposit.State = liquidstakeibctypes.Deposit_DEPOSIT_SENT
deposit.IbcSequenceId = k.GetTransactionSequenceID(hc.ChannelId, msgTransferResponse.Sequence)
k.SetDeposit(ctx, deposit)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeDelegationWorkflow,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeEpoch, strconv.FormatInt(deposit.Epoch, 10)),
sdk.NewAttribute(liquidstakeibctypes.AttributeTotalEpochDepositAmount, sdk.NewCoin(hc.HostDenom, deposit.Amount.Amount).String()),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, deposit.IbcSequenceId),
),
)
}
}

Expand Down Expand Up @@ -545,6 +640,20 @@ func (k *Keeper) UndelegationWorkflow(ctx sdk.Context, epoch int64) {
unbonding.IbcSequenceId = sequenceID
unbonding.State = liquidstakeibctypes.Unbonding_UNBONDING_INITIATED
k.SetUnbonding(ctx, unbonding)

// emit the unbonding event
encMsgs, _ := json.Marshal(&messages)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeUndelegationWorkflow,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeEpoch, strconv.FormatInt(epoch, 10)),
sdk.NewAttribute(liquidstakeibctypes.AttributeTotalEpochUnbondingAmount, sdk.NewCoin(hc.HostDenom, unbonding.UnbondAmount.Amount).String()),
sdk.NewAttribute(liquidstakeibctypes.AttributeTotalEpochBurnAmount, sdk.NewCoin(hc.HostDenom, unbonding.BurnAmount.Amount).String()),
sdk.NewAttribute(liquidstakeibctypes.AttributeICAMessages, base64.StdEncoding.EncodeToString(encMsgs)),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, sequenceID),
),
)
}
}

Expand Down Expand Up @@ -619,6 +728,18 @@ func (k *Keeper) ValidatorUndelegationWorkflow(ctx sdk.Context, epoch int64) {
"epoch",
epoch,
)

// emit the validator unbonding event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeValidatorUndelegationWorkflow,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeEpoch, strconv.FormatInt(epoch, 10)),
sdk.NewAttribute(liquidstakeibctypes.AttributeValidatorAddress, validatorUnbonding.ValidatorAddress),
sdk.NewAttribute(liquidstakeibctypes.AttributeValidatorUnbondingAmount, sdk.NewCoin(hc.HostDenom, validatorUnbonding.Amount.Amount).String()),
sdk.NewAttribute(liquidstakeibctypes.AttributeIBCSequenceID, sequenceID),
),
)
}
}
}
Expand Down Expand Up @@ -661,6 +782,17 @@ func (k *Keeper) RewardsWorkflow(ctx sdk.Context, epoch int64) {
)
continue
}

// emit the rewards event
encMsgs, _ := json.Marshal(&messages)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeRewardsWorkflow,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeEpoch, strconv.FormatInt(epoch, 10)),
sdk.NewAttribute(liquidstakeibctypes.AttributeICAMessages, base64.StdEncoding.EncodeToString(encMsgs)),
),
)
}

if hc.RewardsAccount != nil &&
Expand All @@ -685,6 +817,7 @@ func (k *Keeper) LSMWorkflow(ctx sdk.Context) {
}

// attempt to transfer all available LSM deposits
totalLSMDepositsSharesAmount := math.LegacyZeroDec()
for _, deposit := range k.GetTransferableLSMDeposits(ctx, hc.ChainId) {
clientState, err := k.GetClientState(ctx, hc.ConnectionId)
if err != nil {
Expand All @@ -694,7 +827,7 @@ func (k *Keeper) LSMWorkflow(ctx sdk.Context) {

timeoutHeight := clienttypes.NewHeight(
clientState.GetLatestHeight().GetRevisionNumber(),
clientState.GetLatestHeight().GetRevisionHeight()+liquidstakeibctypes.IBCTimeoutHeightIncrement,
clientState.GetLatestHeight().GetRevisionHeight()+1,
)

// craft the IBC message
Expand Down Expand Up @@ -732,6 +865,17 @@ func (k *Keeper) LSMWorkflow(ctx sdk.Context) {
liquidstakeibctypes.LSMDeposit_DEPOSIT_SENT,
k.GetTransactionSequenceID(hc.ChannelId, msgTransferResponse.Sequence),
)

totalLSMDepositsSharesAmount = totalLSMDepositsSharesAmount.Add(deposit.Shares)
}

// emit the validator unbonding event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
liquidstakeibctypes.EventTypeLSMWorkflow,
sdk.NewAttribute(liquidstakeibctypes.AttributeChainID, hc.ChainId),
sdk.NewAttribute(liquidstakeibctypes.AttributeLSMDepositsSharesAmount, totalLSMDepositsSharesAmount.String()),
),
)
}
}
Loading
Loading