Skip to content

Commit

Permalink
feat(denommetadata): charge extra fee for IBC denom metadata registra…
Browse files Browse the repository at this point in the history
…tion (#1609)
  • Loading branch information
mtsitrin authored Dec 4, 2024
1 parent 9c96919 commit 8df230b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (a *AppKeepers) InitTransferStack() {
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)

a.TransferStack = denommetadatamodule.NewIBCModule(a.TransferStack, a.DenomMetadataKeeper, a.RollappKeeper)
a.TransferStack = denommetadatamodule.NewIBCModule(a.TransferStack, a.DenomMetadataKeeper, a.RollappKeeper, a.TxFeesKeeper)
// already instantiated in SetupHooks()
a.delayedAckMiddleware.Setup(
delayedackmodule.WithIBCModule(a.TransferStack),
Expand Down
5 changes: 1 addition & 4 deletions ibctesting/eibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,7 @@ func (s *eibcSuite) TestEIBCDemandOrderFulfillment() {
}

func (s *eibcSuite) rollappHasPacketCommitment(packet channeltypes.Packet) bool {
// TODO: this should be used to check that a commitment does (or doesn't) exist, when it should
// TODO: this is important to check that things actually work as expected and dont just look ok on the outside
// TODO: finish implementing, true is a temporary placeholder
return true
return s.hubChain().App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(s.rollappCtx(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
}

// TestTimeoutEIBCDemandOrderFulfillment: when a packet hub->rollapp times out, or gets an error ack, than eIBC can be used to recover quickly.
Expand Down
20 changes: 19 additions & 1 deletion x/denommetadata/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ import (
"github.com/dymensionxyz/sdk-utils/utils/uevent"
"github.com/dymensionxyz/sdk-utils/utils/uibc"

commontypes "github.com/dymensionxyz/dymension/v3/x/common/types"
"github.com/dymensionxyz/dymension/v3/x/denommetadata/types"
)

var RegistrationFeeAmt = commontypes.DYM.QuoRaw(10) // 0.1 DYM

var _ porttypes.IBCModule = &IBCModule{}

// IBCModule implements the ICS26 callbacks for the transfer middleware
type IBCModule struct {
porttypes.IBCModule
keeper types.DenomMetadataKeeper
rollappKeeper types.RollappKeeper
txFeesKeeper types.TxFeesKeeper
}

// NewIBCModule creates a new IBCModule given the keepers and underlying application
func NewIBCModule(
app porttypes.IBCModule,
keeper types.DenomMetadataKeeper,
rollappKeeper types.RollappKeeper,
txFeesKeeper types.TxFeesKeeper,
) IBCModule {
return IBCModule{
IBCModule: app,
keeper: keeper,
rollappKeeper: rollappKeeper,
txFeesKeeper: txFeesKeeper,
}
}

Expand Down Expand Up @@ -87,9 +93,21 @@ func (im IBCModule) OnRecvPacket(
return im.IBCModule.OnRecvPacket(ctx, packet, relayer)
}

// charge denom metadata registration fee
baseDenom, err := im.txFeesKeeper.GetBaseDenom(ctx)
if err != nil {
return uevent.NewErrorAcknowledgement(ctx, err)
}

registrationFee := sdk.NewCoin(baseDenom, RegistrationFeeAmt)
err = im.txFeesKeeper.ChargeFeesFromPayer(ctx, relayer, registrationFee, nil)
if err != nil {
return uevent.NewErrorAcknowledgement(ctx, err)
}

// adjust the denom metadata with the IBC denom
dm.Base = ibcDenom
dm.DenomUnits[0].Denom = dm.Base

if err = im.keeper.CreateDenomMetadata(ctx, *dm); err != nil {
if errorsmod.IsOf(err, gerrc.ErrAlreadyExists) {
return im.IBCModule.OnRecvPacket(ctx, packet, relayer)
Expand Down
20 changes: 18 additions & 2 deletions x/denommetadata/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestIBCModule_OnRecvPacket(t *testing.T) {
name string
keeper *mockDenomMetadataKeeper
rollappKeeper *mockRollappKeeper
txFeesKeeper *mockTxFeeKeeper

memoData *memoData
wantAck exported.Acknowledgement
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestIBCModule_OnRecvPacket(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &mockIBCModule{}
im := denommetadata.NewIBCModule(app, tt.keeper, tt.rollappKeeper)
im := denommetadata.NewIBCModule(app, tt.keeper, tt.rollappKeeper, tt.txFeesKeeper)
var memo string
if tt.memoData != nil {
memo = mustMarshalJSON(tt.memoData)
Expand Down Expand Up @@ -345,6 +346,7 @@ func TestIBCModule_OnAcknowledgementPacket(t *testing.T) {
type fields struct {
IBCModule porttypes.IBCModule
rollappKeeper *mockRollappKeeper
txFeesKeeper *mockTxFeeKeeper
}
type args struct {
packetData *transfertypes.FungibleTokenPacketData
Expand Down Expand Up @@ -524,7 +526,7 @@ func TestIBCModule_OnAcknowledgementPacket(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := denommetadata.NewIBCModule(tt.fields.IBCModule, nil, tt.fields.rollappKeeper)
m := denommetadata.NewIBCModule(tt.fields.IBCModule, nil, tt.fields.rollappKeeper, tt.fields.txFeesKeeper)

packet := channeltypes.Packet{}

Expand Down Expand Up @@ -745,3 +747,17 @@ func (m mockBankKeeper) SetDenomMetaData(ctx sdk.Context, denomMetaData banktype
func (m mockBankKeeper) GetDenomMetaData(sdk.Context, string) (banktypes.Metadata, bool) {
return m.returnMetadata, m.returnMetadata.Base != ""
}

type mockTxFeeKeeper struct {
err error
}

// ChargeFeesFromPayer implements types.TxFeesKeeper.
func (m *mockTxFeeKeeper) ChargeFeesFromPayer(ctx sdk.Context, payer sdk.AccAddress, takerFeeCoin sdk.Coin, beneficiary *sdk.AccAddress) error {
return m.err
}

// GetBaseDenom implements types.TxFeesKeeper.
func (m *mockTxFeeKeeper) GetBaseDenom(ctx sdk.Context) (denom string, err error) {
return "adym", nil
}
5 changes: 5 additions & 0 deletions x/denommetadata/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ type RollappKeeper interface {
HasRegisteredDenom(ctx sdk.Context, rollappID, denom string) (bool, error)
ClearRegisteredDenoms(ctx sdk.Context, rollappID string) error
}

type TxFeesKeeper interface {
GetBaseDenom(ctx sdk.Context) (denom string, err error)
ChargeFeesFromPayer(ctx sdk.Context, payer sdk.AccAddress, takerFeeCoin sdk.Coin, beneficiary *sdk.AccAddress) error
}
2 changes: 1 addition & 1 deletion x/rollapp/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

KeyMinSequencerBondGlobal = []byte("KeyMinSequencerBondGlobal")

DefaultAppRegistrationFee = commontypes.DYMCoin
DefaultAppRegistrationFee = commontypes.Dym(sdk.NewInt(1))
DefaultMinSequencerBondGlobalCoin = commontypes.Dym(sdk.NewInt(100))
)

Expand Down

0 comments on commit 8df230b

Please sign in to comment.