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

chore: adding distribute fee events to ics29 #2975

Merged
merged 7 commits into from
Jan 19, 2023
4 changes: 4 additions & 0 deletions modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ func (k Keeper) distributeFee(ctx sdk.Context, receiver, refundAccAddress sdk.Ac
k.Logger(ctx).Error("error refunding fee to the original sender", "refund address", refundAccAddress, "fee", fee)
return // if sending to the refund address fails, no-op
}

EmitDistributeFeeEvent(ctx, refundAccAddress.String(), fee)
} else {
EmitDistributeFeeEvent(ctx, receiver.String(), fee)
}

// write the cache
Expand Down
15 changes: 15 additions & 0 deletions modules/apps/29-fee/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,18 @@ func EmitRegisterCounterpartyPayeeEvent(ctx sdk.Context, relayer, counterpartyPa
),
})
}

// EmitDistributeFeeEvent emits an event containing a distribution fee and receiver address
func EmitDistributeFeeEvent(ctx sdk.Context, receiver string, fee sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDistributeFee,
sdk.NewAttribute(types.AttributeKeyReceiver, receiver),
sdk.NewAttribute(types.AttributeKeyFee, fee.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
),
})
}
82 changes: 82 additions & 0 deletions modules/apps/29-fee/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
abcitypes "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/ibc-go/v6/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
ibctesting "github.com/cosmos/ibc-go/v6/testing"
)

func (suite *KeeperTestSuite) TestIncentivizePacketEvent() {
Expand Down Expand Up @@ -81,3 +86,80 @@ func (suite *KeeperTestSuite) TestIncentivizePacketEvent() {
}
}
}

func (suite *KeeperTestSuite) TestDistributeFeeEvent() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Thanks, @damiannolan!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the prettiest test, but it should do the job. Sadly, we can't really emit whether its a recv, ack or timeout fee within the events.

// create an incentivized transfer path
path := ibctesting.NewPath(suite.chainA, suite.chainB)
feeTransferVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version}))
path.EndpointA.ChannelConfig.Version = feeTransferVersion
path.EndpointB.ChannelConfig.Version = feeTransferVersion
path.EndpointA.ChannelConfig.PortID = transfertypes.PortID
path.EndpointB.ChannelConfig.PortID = transfertypes.PortID

suite.coordinator.Setup(path)

// send a new MsgPayPacketFee and MsgTransfer to chainA
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
msgPayPacketFee := types.NewMsgPayPacketFee(
fee,
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
suite.chainA.SenderAccount.GetAddress().String(),
nil,
)

msgTransfer := transfertypes.NewMsgTransfer(
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(),
clienttypes.NewHeight(1, 100), 0, "",
)

res, err := suite.chainA.SendMsgs(msgPayPacketFee, msgTransfer)
suite.Require().NoError(err)
suite.Require().NotNil(res)

// parse the packet from result events and recv packet on chainB
packet, err := ibctesting.ParsePacketFromEvents(res.GetEvents())
suite.Require().NoError(err)
suite.Require().NotNil(packet)

err = path.EndpointB.UpdateClient()
suite.Require().NoError(err)

res, err = path.EndpointB.RecvPacketWithResult(packet)
suite.Require().NoError(err)
suite.Require().NotNil(res)

// parse the acknowledgement from result events and acknowledge packet on chainA
ack, err := ibctesting.ParseAckFromEvents(res.GetEvents())
suite.Require().NoError(err)
suite.Require().NotNil(ack)

packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight := path.EndpointA.Counterparty.QueryProof(packetKey)

msgAcknowledgement := channeltypes.NewMsgAcknowledgement(packet, ack, proof, proofHeight, path.EndpointA.Chain.SenderAccount.GetAddress().String())
res, err = suite.chainA.SendMsgs(msgAcknowledgement)
suite.Require().NoError(err)
suite.Require().NotNil(res)

// calculate the total paid out fees using "distribute_fee" events
var totalPaid sdk.Coins
for _, event := range res.Events {
if event.Type == types.EventTypeDistributeFee {
for _, attr := range event.Attributes {
switch string(attr.Key) {
case types.AttributeKeyFee:
coins, err := sdk.ParseCoinsNormalized(string(attr.Value))
suite.Require().NoError(err)

totalPaid = totalPaid.Add(coins...)
}
}

}
}

// assert the total fees paid out equal the total incentivization fees escrowed
suite.Require().Equal(fee.Total(), totalPaid)
}
3 changes: 3 additions & 0 deletions modules/apps/29-fee/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
EventTypeIncentivizedPacket = "incentivized_ibc_packet"
EventTypeRegisterPayee = "register_payee"
EventTypeRegisterCounterpartyPayee = "register_counterparty_payee"
EventTypeDistributeFee = "distribute_fee"

AttributeKeyRecvFee = "recv_fee"
AttributeKeyAckFee = "ack_fee"
Expand All @@ -13,4 +14,6 @@ const (
AttributeKeyRelayer = "relayer"
AttributeKeyPayee = "payee"
AttributeKeyCounterpartyPayee = "counterparty_payee"
AttributeKeyReceiver = "receiver"
AttributeKeyFee = "fee"
)