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

IBC Fees #1946

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions tests/e2e/ibc_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/CosmWasm/wasmd/app"
wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
)

func TestIBCFeesTransfer(t *testing.T) {
Expand Down Expand Up @@ -219,3 +220,119 @@ func TestIBCFeesWasm(t *testing.T) {
payeeBalance = chainB.AllBalances(payee)
assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(2)).String(), payeeBalance.String())
}

func TestIBCFeesReflect(t *testing.T) {
// scenario:
// given 2 chains with reflect on chain A
// and an ibc channel established
// when ibc-reflect sends a PayPacketFee and a PayPacketFeeAsync msg
// then the relayer's payee is receiving the fee(s) on success

marshaler := app.MakeEncodingConfig(t).Codec
coord := wasmibctesting.NewCoordinator(t, 2)
chainA := coord.GetChain(wasmibctesting.GetChainID(1))
chainB := coord.GetChain(ibctesting.GetChainID(2))
actorChainA := sdk.AccAddress(chainA.SenderPrivKey.PubKey().Address())
actorChainB := sdk.AccAddress(chainB.SenderPrivKey.PubKey().Address())

// setup chain A
codeID := chainA.StoreCodeFile("./testdata/reflect_2_2.wasm").CodeID

initMsg := []byte("{}")
reflectContractAddr := chainA.InstantiateContract(codeID, initMsg)

payee := sdk.AccAddress(bytes.Repeat([]byte{2}, address.Len))
oneToken := []wasmvmtypes.Coin{wasmvmtypes.NewCoin(1, sdk.DefaultBondDenom)}

path := wasmibctesting.NewPath(chainA, chainB)
path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{
PortID: ibctransfertypes.PortID,
Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.Version})),
Order: channeltypes.UNORDERED,
}
path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{
PortID: ibctransfertypes.PortID,
Version: string(marshaler.MustMarshalJSON(&ibcfee.Metadata{FeeVersion: ibcfee.Version, AppVersion: ibctransfertypes.Version})),
Order: channeltypes.UNORDERED,
}
// with an ics-29 fee enabled channel setup between both chains
coord.Setup(path)
appA := chainA.App.(*app.WasmApp)
appB := chainB.App.(*app.WasmApp)
require.True(t, appA.IBCFeeKeeper.IsFeeEnabled(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID))
require.True(t, appB.IBCFeeKeeper.IsFeeEnabled(chainB.GetContext(), ibctransfertypes.PortID, path.EndpointB.ChannelID))
// and with a payee registered for A -> B
_, err := chainA.SendMsgs(ibcfee.NewMsgRegisterPayee(ibctransfertypes.PortID, path.EndpointA.ChannelID, actorChainA.String(), payee.String()))
require.NoError(t, err)
_, err = chainB.SendMsgs(ibcfee.NewMsgRegisterCounterpartyPayee(ibctransfertypes.PortID, path.EndpointB.ChannelID, actorChainB.String(), payee.String()))
require.NoError(t, err)

// when reflect contract on A sends a PayPacketFee msg, followed by a transfer
_, err = ExecViaReflectContract(t, chainA, reflectContractAddr, []wasmvmtypes.CosmosMsg{
{
IBC: &wasmvmtypes.IBCMsg{
PayPacketFee: &wasmvmtypes.PayPacketFeeMsg{
Fee: wasmvmtypes.IBCFee{
AckFee: oneToken,
ReceiveFee: oneToken,
TimeoutFee: []wasmvmtypes.Coin{},
},
Relayers: []string{},
PortID: ibctransfertypes.PortID,
ChannelID: path.EndpointA.ChannelID,
},
},
},
{
IBC: &wasmvmtypes.IBCMsg{
Transfer: &wasmvmtypes.TransferMsg{
ChannelID: path.EndpointA.ChannelID,
ToAddress: actorChainB.String(),
Amount: wasmvmtypes.NewCoin(10, sdk.DefaultBondDenom),
Timeout: wasmvmtypes.IBCTimeout{
Timestamp: 9999999999999999999,
},
},
},
},
})
require.NoError(t, err)

pendingIncentivisedPackages := appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 1)

// and sends an PayPacketFeeAsync msg
_, err = ExecViaReflectContract(t, chainA, reflectContractAddr, []wasmvmtypes.CosmosMsg{
{
IBC: &wasmvmtypes.IBCMsg{
PayPacketFeeAsync: &wasmvmtypes.PayPacketFeeAsyncMsg{
Fee: wasmvmtypes.IBCFee{
AckFee: []wasmvmtypes.Coin{},
ReceiveFee: oneToken,
TimeoutFee: oneToken,
},
Relayers: []string{},
Sequence: pendingIncentivisedPackages[0].PacketId.Sequence,
PortID: ibctransfertypes.PortID,
ChannelID: path.EndpointA.ChannelID,
},
},
},
})
require.NoError(t, err)

// and packages relayed
require.NoError(t, coord.RelayAndAckPendingPackets(path))

// then
// on chain A
payeeBalance := chainA.AllBalances(payee)
// 2 tokens from the PayPacketFee and 1 token from the PayPacketFeeAsync
assert.Equal(t, sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(3)).String(), payeeBalance.String())
// and on chain B
pendingIncentivisedPackages = appA.IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(chainA.GetContext(), ibctransfertypes.PortID, path.EndpointA.ChannelID)
assert.Len(t, pendingIncentivisedPackages, 0)
expBalance := ibctransfertypes.GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sdk.DefaultBondDenom, sdkmath.NewInt(10))
gotBalance := chainB.Balance(actorChainB, expBalance.Denom)
assert.Equal(t, expBalance.String(), gotBalance.String(), chainB.AllBalances(actorChainB))
}
Binary file added tests/e2e/testdata/reflect_2_2.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/integration/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func setupTest(t *testing.T) testData {

ctx, keepers := keeper.CreateTestInput(t, false, []string{
"iterator", "staking", "stargate", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_3",
"cosmwasm_1_4", "cosmwasm_2_0", "cosmwasm_2_1",
"cosmwasm_1_4", "cosmwasm_2_0", "cosmwasm_2_1", "cosmwasm_2_2",
})
encConf := keeper.MakeEncodingConfig(t)
queryRouter := baseapp.NewGRPCQueryRouter()
Expand Down
1 change: 1 addition & 0 deletions x/wasm/keeper/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"cosmwasm_1_4",
"cosmwasm_2_0",
"cosmwasm_2_1",
"cosmwasm_2_2",

Check warning on line 20 in x/wasm/keeper/capabilities.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/capabilities.go#L20

Added line #L20 was not covered by tests
}
}
48 changes: 48 additions & 0 deletions x/wasm/keeper/handler_plugin_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"fmt"

wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand Down Expand Up @@ -320,6 +321,33 @@
Memo: msg.Transfer.Memo,
}
return []sdk.Msg{msg}, nil
case msg.PayPacketFee != nil:
fee, err := ConvertIBCFee(&msg.PayPacketFee.Fee)
if err != nil {
return nil, errorsmod.Wrap(err, "fee")

Check warning on line 327 in x/wasm/keeper/handler_plugin_encoders.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/handler_plugin_encoders.go#L327

Added line #L327 was not covered by tests
}
msg := &ibcfeetypes.MsgPayPacketFee{
Fee: fee,
SourcePortId: msg.PayPacketFee.PortID,
SourceChannelId: msg.PayPacketFee.ChannelID,
Signer: sender.String(),
Relayers: msg.PayPacketFee.Relayers,
}
return []sdk.Msg{msg}, nil
case msg.PayPacketFeeAsync != nil:
fee, err := ConvertIBCFee(&msg.PayPacketFeeAsync.Fee)
if err != nil {
return nil, errorsmod.Wrap(err, "fee")

Check warning on line 340 in x/wasm/keeper/handler_plugin_encoders.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/handler_plugin_encoders.go#L340

Added line #L340 was not covered by tests
}
msg := &ibcfeetypes.MsgPayPacketFeeAsync{
PacketId: channeltypes.PacketId{
PortId: msg.PayPacketFeeAsync.PortID,
ChannelId: msg.PayPacketFeeAsync.ChannelID,
Sequence: msg.PayPacketFeeAsync.Sequence,
},
PacketFee: ibcfeetypes.NewPacketFee(fee, sender.String(), msg.PayPacketFeeAsync.Relayers),
}
return []sdk.Msg{msg}, nil
default:
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of IBC")
}
Expand Down Expand Up @@ -406,3 +434,23 @@
}
return r, r.Validate()
}

func ConvertIBCFee(fee *wasmvmtypes.IBCFee) (ibcfeetypes.Fee, error) {
ackFee, err := ConvertWasmCoinsToSdkCoins(fee.AckFee)
if err != nil {
return ibcfeetypes.Fee{}, err

Check warning on line 441 in x/wasm/keeper/handler_plugin_encoders.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/handler_plugin_encoders.go#L441

Added line #L441 was not covered by tests
}
recvFee, err := ConvertWasmCoinsToSdkCoins(fee.ReceiveFee)
if err != nil {
return ibcfeetypes.Fee{}, err

Check warning on line 445 in x/wasm/keeper/handler_plugin_encoders.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/handler_plugin_encoders.go#L445

Added line #L445 was not covered by tests
}
timeoutFee, err := ConvertWasmCoinsToSdkCoins(fee.TimeoutFee)
if err != nil {
return ibcfeetypes.Fee{}, err

Check warning on line 449 in x/wasm/keeper/handler_plugin_encoders.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/keeper/handler_plugin_encoders.go#L449

Added line #L449 was not covered by tests
}
return ibcfeetypes.Fee{
AckFee: ackFee,
RecvFee: recvFee,
TimeoutFee: timeoutFee,
}, nil
}
81 changes: 81 additions & 0 deletions x/wasm/keeper/handler_plugin_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
"github.com/cosmos/gogoproto/proto"
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand Down Expand Up @@ -613,6 +614,86 @@ func TestEncodeIbcMsg(t *testing.T) {
},
},
},
"IBC PayPacketFee": {
sender: addr1,
srcContractIBCPort: "myIBCPort",
srcMsg: wasmvmtypes.CosmosMsg{
IBC: &wasmvmtypes.IBCMsg{
PayPacketFee: &wasmvmtypes.PayPacketFeeMsg{
ChannelID: "myChannelID",
Fee: wasmvmtypes.IBCFee{
TimeoutFee: []wasmvmtypes.Coin{
{
Denom: "ALX",
Amount: "1",
},
},
},
PortID: "myIBCPort",
Relayers: []string{},
},
},
},
output: []sdk.Msg{
&ibcfee.MsgPayPacketFee{
Fee: ibcfee.Fee{
TimeoutFee: []sdk.Coin{
{
Denom: "ALX",
Amount: sdkmath.NewInt(1),
},
},
},
SourcePortId: "myIBCPort",
SourceChannelId: "myChannelID",
Signer: addr1.String(),
Relayers: []string{},
},
},
},
"IBC PayPacketFeeAsync": {
sender: addr1,
srcContractIBCPort: "myIBCPort",
srcMsg: wasmvmtypes.CosmosMsg{
IBC: &wasmvmtypes.IBCMsg{
PayPacketFeeAsync: &wasmvmtypes.PayPacketFeeAsyncMsg{
ChannelID: "myChannelID",
Fee: wasmvmtypes.IBCFee{
TimeoutFee: []wasmvmtypes.Coin{
{
Denom: "ALX",
Amount: "1",
},
},
},
PortID: "myIBCPort",
Relayers: []string{},
Sequence: 42,
},
},
},
output: []sdk.Msg{
&ibcfee.MsgPayPacketFeeAsync{
PacketId: channeltypes.PacketId{
PortId: "myIBCPort",
ChannelId: "myChannelID",
Sequence: 42,
},
PacketFee: ibcfee.PacketFee{
Fee: ibcfee.Fee{
TimeoutFee: []sdk.Coin{
{
Denom: "ALX",
Amount: sdkmath.NewInt(1),
},
},
},
RefundAddress: addr1.String(),
Relayers: []string{},
},
},
},
},
}
encodingConfig := MakeEncodingConfig(t)
for name, tc := range cases {
Expand Down