From 82a820c70b5a522d732c4b2338a94bcdd656174e Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Thu, 12 Dec 2024 11:44:44 +0100 Subject: [PATCH] Add consumer distribution handler (WIP) --- .../ibc_packet_btc_staking_consumer_event.go | 40 +++++++++++++++++++ x/zoneconcierge/module_ibc.go | 6 +++ 2 files changed, 46 insertions(+) diff --git a/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go b/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go index 43c7df0db..33630f8d6 100644 --- a/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go +++ b/x/zoneconcierge/keeper/ibc_packet_btc_staking_consumer_event.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "encoding/json" "fmt" bbn "github.com/babylonlabs-io/babylon/types" @@ -9,6 +10,7 @@ import ( finalitytypes "github.com/babylonlabs-io/babylon/x/finality/types" "github.com/babylonlabs-io/babylon/x/zoneconcierge/types" sdk "github.com/cosmos/cosmos-sdk/types" + ibctransfer "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" ) // BroadcastBTCStakingConsumerEvents retrieves all BTC staking consumer events from the event store, @@ -154,3 +156,41 @@ func (k Keeper) HandleConsumerSlashing( return nil } + +type RewardsDistribution struct { + FpPubkeyHex string `json:"fp_pubkey_hex"` + Reward string `json:"reward"` +} + +func (k Keeper) HandleConsumerDistribution( + ctx sdk.Context, + portID string, + channelID string, + consumerDistribution *ibctransfer.FungibleTokenPacketData, +) error { + // clientID, _, err := k.channelKeeper.GetChannelClientState(ctx, portID, channelID) + // if err != nil { + // return fmt.Errorf("failed to get client state: %w", err) + // } + + // Deserialize the memo + var rewardsDistribution []RewardsDistribution + if err := json.Unmarshal([]byte(consumerDistribution.Memo), &rewardsDistribution); err != nil { + return fmt.Errorf("failed to unmarshal memo: %w", err) + } + + // Process the distribution list + for _, reward := range rewardsDistribution { + fpPubkey, err := bbn.NewBIP340PubKeyFromHex(reward.FpPubkeyHex) + if err != nil { + return fmt.Errorf("failed to create BIP340 public key: %w", err) + } + fmt.Println("FP pubkey:", fpPubkey, ", Reward:", reward.Reward) + + // Apply the finality provider commission + + // Distribute the rewards to the delegators + } + + return nil +} diff --git a/x/zoneconcierge/module_ibc.go b/x/zoneconcierge/module_ibc.go index c74c92fc8..e02151d43 100644 --- a/x/zoneconcierge/module_ibc.go +++ b/x/zoneconcierge/module_ibc.go @@ -191,6 +191,12 @@ func (im IBCModule) OnRecvPacket( return channeltypes.NewErrorAcknowledgement(err) } return channeltypes.NewResultAcknowledgement([]byte("Consumer slashing handled successfully")) + case *types.ZoneconciergePacketData_ConsumerFpDistribution: + err := im.keeper.HandleConsumerDistribution(ctx, modulePacket.DestinationPort, modulePacket.DestinationChannel, packet.ConsumerFpDistribution) + if err != nil { + return channeltypes.NewErrorAcknowledgement(err) + } + return channeltypes.NewResultAcknowledgement([]byte("Consumer distribution handled successfully")) // Add other packet types here if needed default: errMsg := fmt.Sprintf("unrecognized %s packet type: %T", types.ModuleName, packet)