From 78df5f1faca36f9662ce67d6c813f820e85e7a9c Mon Sep 17 00:00:00 2001 From: aljo242 Date: Wed, 3 Aug 2022 16:29:02 -0400 Subject: [PATCH] use ibcmodule idiom --- ignite/templates/ibc/oracle.go | 8 ++-- .../oracle/x/{{moduleName}}/oracle.go.plush | 4 +- ignite/templates/ibc/packet.go | 6 +-- ignite/templates/module/create/ibc.go | 15 ++++++- .../ibc/x/{{moduleName}}/module_ibc.go.plush | 39 ++++++++++++------- .../stargate/x/{{moduleName}}/module.go.plush | 2 +- 6 files changed, 48 insertions(+), 26 deletions(-) diff --git a/ignite/templates/ibc/oracle.go b/ignite/templates/ibc/oracle.go index 5cecdafde2..3933879fdc 100644 --- a/ignite/templates/ibc/oracle.go +++ b/ignite/templates/ibc/oracle.go @@ -86,7 +86,7 @@ func moduleOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genn } // Recv packet dispatch - templateRecv := `oracleAck, err := am.handleOraclePacket(ctx, modulePacket) + templateRecv := `oracleAck, err := im.handleOraclePacket(ctx, modulePacket) if err != nil { return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: "+err.Error())) } else if ack != oracleAck { @@ -97,7 +97,7 @@ func moduleOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genn content := replacer.ReplaceOnce(f.String(), PlaceholderOraclePacketModuleRecv, replacementRecv) // Ack packet dispatch - templateAck := `sdkResult, err := am.handleOracleAcknowledgment(ctx, ack, modulePacket) + templateAck := `sdkResult, err := im.handleOracleAcknowledgment(ctx, ack, modulePacket) if err != nil { return err } @@ -334,7 +334,7 @@ func packetHandlerOracleModify(replacer placeholder.Replacer, opts *OracleOption return ack, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "cannot decode the %[2]v received packet") } - am.keeper.Set%[3]vResult(ctx, types.OracleRequestID(modulePacketData.RequestID), %[2]vResult) + im.keeper.Set%[3]vResult(ctx, types.OracleRequestID(modulePacketData.RequestID), %[2]vResult) // TODO: %[3]v oracle data reception logic %[1]v` @@ -350,7 +350,7 @@ func packetHandlerOracleModify(replacer placeholder.Replacer, opts *OracleOption return nil, sdkerrors.Wrap(err, "cannot decode the %[2]v oracle acknowledgment packet") } - am.keeper.SetLast%[3]vID(ctx, requestID) + im.keeper.SetLast%[3]vID(ctx, requestID) return &sdk.Result{}, nil %[1]v` replacementInterface := fmt.Sprintf(templateAck, PlaceholderOracleModuleAck, diff --git a/ignite/templates/ibc/oracle/x/{{moduleName}}/oracle.go.plush b/ignite/templates/ibc/oracle/x/{{moduleName}}/oracle.go.plush index c7d129020d..dfb5dcc8b4 100644 --- a/ignite/templates/ibc/oracle/x/{{moduleName}}/oracle.go.plush +++ b/ignite/templates/ibc/oracle/x/{{moduleName}}/oracle.go.plush @@ -11,7 +11,7 @@ import ( // handleOraclePacket handles the result of the received BandChain oracles // packet and saves the data into the KV database -func (am AppModule) handleOraclePacket( +func (im IBCModule) handleOraclePacket( ctx sdk.Context, modulePacket channeltypes.Packet, ) (channeltypes.Acknowledgement, error) { @@ -41,7 +41,7 @@ func (am AppModule) handleOraclePacket( // handleOracleAcknowledgment handles the acknowledgment result from the BandChain // request and saves the request-id into the KV database -func (am AppModule) handleOracleAcknowledgment( +func (im IBCModule) handleOracleAcknowledgment( ctx sdk.Context, ack channeltypes.Acknowledgement, modulePacket channeltypes.Packet, diff --git a/ignite/templates/ibc/packet.go b/ignite/templates/ibc/packet.go index aa6f0ac8a2..b4282c8c78 100644 --- a/ignite/templates/ibc/packet.go +++ b/ignite/templates/ibc/packet.go @@ -109,7 +109,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF // Recv packet dispatch templateRecv := `case *types.%[2]vPacketData_%[3]vPacket: - packetAck, err := am.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket) + packetAck, err := im.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket) if err != nil { ack = channeltypes.NewErrorAcknowledgement(err) } else { @@ -138,7 +138,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF // Ack packet dispatch templateAck := `case *types.%[2]vPacketData_%[3]vPacket: - err := am.keeper.OnAcknowledgement%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket, ack) + err := im.keeper.OnAcknowledgement%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket, ack) if err != nil { return err } @@ -154,7 +154,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF // Timeout packet dispatch templateTimeout := `case *types.%[2]vPacketData_%[3]vPacket: - err := am.keeper.OnTimeout%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket) + err := im.keeper.OnTimeout%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket) if err != nil { return err } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 52583f354b..7357701578 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -177,15 +177,26 @@ func appIBCModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunF return err } + // create IBC module + templateIBCModule := `%[2]vIBCModule := %[2]vmodule.NewIBCModule(app.%[3]vKeeper) +%[1]v` + replacementIBCModule := fmt.Sprintf( + templateIBCModule, + module.PlaceholderSgAppKeeperDefinition, + opts.ModuleName, + xstrings.Title(opts.ModuleName), + ) + content := replacer.Replace(f.String(), module.PlaceholderSgAppKeeperDefinition, replacementIBCModule) + // Add route to IBC router - templateRouter := `ibcRouter.AddRoute(%[2]vmoduletypes.ModuleName, %[2]vModule) + templateRouter := `ibcRouter.AddRoute(%[2]vmoduletypes.ModuleName, %[2]vIBCModule) %[1]v` replacementRouter := fmt.Sprintf( templateRouter, module.PlaceholderIBCAppRouter, opts.ModuleName, ) - content := replacer.Replace(f.String(), module.PlaceholderIBCAppRouter, replacementRouter) + content = replacer.Replace(content, module.PlaceholderIBCAppRouter, replacementRouter) // Scoped keeper declaration for the module templateScopedKeeperDeclaration := `Scoped%[1]vKeeper capabilitykeeper.ScopedKeeper` diff --git a/ignite/templates/module/create/ibc/x/{{moduleName}}/module_ibc.go.plush b/ignite/templates/module/create/ibc/x/{{moduleName}}/module_ibc.go.plush index ca0b067795..c01797f675 100644 --- a/ignite/templates/module/create/ibc/x/{{moduleName}}/module_ibc.go.plush +++ b/ignite/templates/module/create/ibc/x/{{moduleName}}/module_ibc.go.plush @@ -10,11 +10,22 @@ import ( porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types" host "github.com/cosmos/ibc-go/v5/modules/core/24-host" ibcexported "github.com/cosmos/ibc-go/v5/modules/core/exported" + "<%= modulePath %>/x/<%= moduleName %>/keeper" "<%= modulePath %>/x/<%= moduleName %>/types" ) +type IBCModule struct { + keeper keeper.Keeper +} + +func NewIBCModule(k keeper.Keeper) IBCModule { + return IBCModule{ + keeper: k, + } +} + // OnChanOpenInit implements the IBCModule interface -func (am AppModule) OnChanOpenInit( +func (im IBCModule) OnChanOpenInit( ctx sdk.Context, order channeltypes.Order, connectionHops []string, @@ -29,7 +40,7 @@ func (am AppModule) OnChanOpenInit( }<% } %> // Require portID is the portID module is bound to - boundPort := am.keeper.GetPort(ctx) + boundPort := im.keeper.GetPort(ctx) if boundPort != portID { return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) } @@ -39,7 +50,7 @@ func (am AppModule) OnChanOpenInit( } // Claim channel capability passed back by IBC module - if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { + if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { return "", err } @@ -47,7 +58,7 @@ func (am AppModule) OnChanOpenInit( } // OnChanOpenTry implements the IBCModule interface -func (am AppModule) OnChanOpenTry( +func (im IBCModule) OnChanOpenTry( ctx sdk.Context, order channeltypes.Order, connectionHops []string, @@ -62,7 +73,7 @@ func (am AppModule) OnChanOpenTry( }<% } %> // Require portID is the portID module is bound to - boundPort := am.keeper.GetPort(ctx) + boundPort := im.keeper.GetPort(ctx) if boundPort != portID { return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort) } @@ -75,9 +86,9 @@ func (am AppModule) OnChanOpenTry( // (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry) // If module can already authenticate the capability then module already owns it so we don't need to claim // Otherwise, module does not have channel capability and we must claim it from IBC - if !am.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) { + if !im.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) { // Only claim channel capability passed back by IBC module if we do not already own it - if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { + if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { return "", err } } @@ -86,7 +97,7 @@ func (am AppModule) OnChanOpenTry( } // OnChanOpenAck implements the IBCModule interface -func (am AppModule) OnChanOpenAck( +func (im IBCModule) OnChanOpenAck( ctx sdk.Context, portID, channelID string, @@ -100,7 +111,7 @@ func (am AppModule) OnChanOpenAck( } // OnChanOpenConfirm implements the IBCModule interface -func (am AppModule) OnChanOpenConfirm( +func (im IBCModule) OnChanOpenConfirm( ctx sdk.Context, portID, channelID string, @@ -109,7 +120,7 @@ func (am AppModule) OnChanOpenConfirm( } // OnChanCloseInit implements the IBCModule interface -func (am AppModule) OnChanCloseInit( +func (im IBCModule) OnChanCloseInit( ctx sdk.Context, portID, channelID string, @@ -119,7 +130,7 @@ func (am AppModule) OnChanCloseInit( } // OnChanCloseConfirm implements the IBCModule interface -func (am AppModule) OnChanCloseConfirm( +func (im IBCModule) OnChanCloseConfirm( ctx sdk.Context, portID, channelID string, @@ -128,7 +139,7 @@ func (am AppModule) OnChanCloseConfirm( } // OnRecvPacket implements the IBCModule interface -func (am AppModule) OnRecvPacket( +func (im IBCModule) OnRecvPacket( ctx sdk.Context, modulePacket channeltypes.Packet, relayer sdk.AccAddress, @@ -155,7 +166,7 @@ func (am AppModule) OnRecvPacket( } // OnAcknowledgementPacket implements the IBCModule interface -func (am AppModule) OnAcknowledgementPacket( +func (im IBCModule) OnAcknowledgementPacket( ctx sdk.Context, modulePacket channeltypes.Packet, acknowledgement []byte, @@ -212,7 +223,7 @@ func (am AppModule) OnAcknowledgementPacket( } // OnTimeoutPacket implements the IBCModule interface -func (am AppModule) OnTimeoutPacket( +func (im IBCModule) OnTimeoutPacket( ctx sdk.Context, modulePacket channeltypes.Packet, relayer sdk.AccAddress, diff --git a/ignite/templates/module/create/stargate/x/{{moduleName}}/module.go.plush b/ignite/templates/module/create/stargate/x/{{moduleName}}/module.go.plush index 32d93a7c38..71b0b44ecb 100644 --- a/ignite/templates/module/create/stargate/x/{{moduleName}}/module.go.plush +++ b/ignite/templates/module/create/stargate/x/{{moduleName}}/module.go.plush @@ -25,7 +25,7 @@ import ( var ( _ module.AppModule = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} - <%= if (isIBC) { %>_ porttypes.IBCModule = AppModule{}<% } %> + <%= if (isIBC) { %>_ porttypes.IBCModule = IBCModule{}<% } %> ) // ----------------------------------------------------------------------------