diff --git a/docs/docs/02-guide/04-ibc.md b/docs/docs/02-guide/04-ibc.md index 6dea8c93ca..e351f85ace 100644 --- a/docs/docs/02-guide/04-ibc.md +++ b/docs/docs/02-guide/04-ibc.md @@ -223,48 +223,48 @@ the `msg.Creator` value to the IBC `packet`. package keeper func (k msgServer) SendIbcPost(goCtx context.Context, msg *types.MsgSendIbcPost) (*types.MsgSendIbcPostResponse, error) { - // validate incoming message - if _, err := k.addressCodec.StringToBytes(msg.Creator); err != nil { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid address: %s", err)) - } - - if msg.Port == "" { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet port") - } - - if msg.ChannelID == "" { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet channel") - } - - if msg.TimeoutTimestamp == 0 { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet timeout") - } - - // TODO: logic before transmitting the packet - - // Construct the packet - var packet types.IbcPostPacketData - - packet.Title = msg.Title - packet.Content = msg.Content - // highlight-next-line - packet.Creator = msg.Creator - - // Transmit the packet - ctx := sdk.UnwrapSDKContext(goCtx) - _, err := k.TransmitIbcPostPacket( - ctx, - packet, - msg.Port, - msg.ChannelID, - clienttypes.ZeroHeight(), - msg.TimeoutTimestamp, - ) - if err != nil { - return nil, err - } - - return &types.MsgSendIbcPostResponse{}, nil + // validate incoming message + if _, err := k.addressCodec.StringToBytes(msg.Creator); err != nil { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid address: %s", err)) + } + + if msg.Port == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet port") + } + + if msg.ChannelID == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet channel") + } + + if msg.TimeoutTimestamp == 0 { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet timeout") + } + + // TODO: logic before transmitting the packet + + // Construct the packet + var packet types.IbcPostPacketData + + packet.Title = msg.Title + packet.Content = msg.Content + // highlight-next-line + packet.Creator = msg.Creator + + // Transmit the packet + ctx := sdk.UnwrapSDKContext(goCtx) + _, err := k.TransmitIbcPostPacket( + ctx, + packet, + msg.Port, + msg.ChannelID, + clienttypes.ZeroHeight(), + msg.TimeoutTimestamp, + ) + if err != nil { + return nil, err + } + + return &types.MsgSendIbcPostResponse{}, nil } ``` @@ -315,11 +315,11 @@ Then modify the `OnRecvIbcPostPacket` keeper function with the following code: package keeper func (k Keeper) OnRecvIbcPostPacket(ctx sdk.Context, packet channeltypes.Packet, data types.IbcPostPacketData) (packetAck types.IbcPostPacketAck, err error) { - packetAck.PostId, err = k.PostSeq.Next(ctx) - if err != nil { - return packetAck, err - } - return packetAck, k.Post.Set(ctx, packetAck.PostId, types.Post{Title: data.Title, Content: data.Content}) + packetAck.PostId, err = k.PostSeq.Next(ctx) + if err != nil { + return packetAck, err + } + return packetAck, k.Post.Set(ctx, packetAck.PostId, types.Post{Title: data.Title, Content: data.Content}) } ``` @@ -339,33 +339,33 @@ from the packet. package keeper func (k Keeper) OnAcknowledgementIbcPostPacket(ctx sdk.Context, packet channeltypes.Packet, data types.IbcPostPacketData, ack channeltypes.Acknowledgement) error { - switch dispatchedAck := ack.Response.(type) { - case *channeltypes.Acknowledgement_Error: - // We will not treat acknowledgment error in this tutorial - return nil - case *channeltypes.Acknowledgement_Result: - // Decode the packet acknowledgment - var packetAck types.IbcPostPacketAck - if err := types.ModuleCdc.UnmarshalJSON(dispatchedAck.Result, &packetAck); err != nil { - // The counter-party module doesn't implement the correct acknowledgment format - return errors.New("cannot unmarshal acknowledgment") - } - - seq, err := k.SentPostSeq.Next(ctx) - if err != nil { - return err - } - - return k.SentPost.Set(ctx, seq, - types.SentPost{ - PostId: packetAck.PostId, - Title: data.Title, - Chain: packet.DestinationPort + "-" + packet.DestinationChannel, - }, - ) - default: - return errors.New("the counter-party module does not implement the correct acknowledgment format") - } + switch dispatchedAck := ack.Response.(type) { + case *channeltypes.Acknowledgement_Error: + // We will not treat acknowledgment error in this tutorial + return nil + case *channeltypes.Acknowledgement_Result: + // Decode the packet acknowledgment + var packetAck types.IbcPostPacketAck + if err := types.ModuleCdc.UnmarshalJSON(dispatchedAck.Result, &packetAck); err != nil { + // The counter-party module doesn't implement the correct acknowledgment format + return errors.New("cannot unmarshal acknowledgment") + } + + seq, err := k.SentPostSeq.Next(ctx) + if err != nil { + return err + } + + return k.SentPost.Set(ctx, seq, + types.SentPost{ + PostId: packetAck.PostId, + Title: data.Title, + Chain: packet.DestinationPort + "-" + packet.DestinationChannel, + }, + ) + default: + return errors.New("the counter-party module does not implement the correct acknowledgment format") + } } ``` @@ -376,17 +376,17 @@ posts. This logic follows the same format as `sentPost`. ```go title="x/blog/keeper/ibc_post.go" func (k Keeper) OnTimeoutIbcPostPacket(ctx sdk.Context, packet channeltypes.Packet, data types.IbcPostPacketData) error { - seq, err := k.TimeoutPostSeq.Next(ctx) - if err != nil { - return err - } - - return k.TimeoutPost.Set(ctx, seq, - types.TimeoutPost{ - Title: data.Title, - Chain: packet.DestinationPort + "-" + packet.DestinationChannel, - }, - ) + seq, err := k.TimeoutPostSeq.Next(ctx) + if err != nil { + return err + } + + return k.TimeoutPost.Set(ctx, seq, + types.TimeoutPost{ + Title: data.Title, + Chain: packet.DestinationPort + "-" + packet.DestinationChannel, + }, + ) } ``` diff --git a/ignite/templates/ibc/packet.go b/ignite/templates/ibc/packet.go index cdfc066137..280bc0de57 100644 --- a/ignite/templates/ibc/packet.go +++ b/ignite/templates/ibc/packet.go @@ -13,11 +13,11 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/multiformatname" "github.com/ignite/cli/v29/ignite/pkg/placeholder" "github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v29/ignite/pkg/xast" "github.com/ignite/cli/v29/ignite/pkg/xgenny" "github.com/ignite/cli/v29/ignite/pkg/xstrings" "github.com/ignite/cli/v29/ignite/templates/field" "github.com/ignite/cli/v29/ignite/templates/field/plushhelpers" - "github.com/ignite/cli/v29/ignite/templates/module" "github.com/ignite/cli/v29/ignite/templates/testutil" "github.com/ignite/cli/v29/ignite/templates/typed" ) @@ -79,7 +79,7 @@ func NewPacket(replacer placeholder.Replacer, opts *PacketOptions) (*genny.Gener if !opts.NoMessage { g.RunFn(protoTxModify(opts)) g.RunFn(clientCliTxModify(replacer, opts)) - g.RunFn(codecModify(replacer, opts)) + g.RunFn(codecModify(opts)) if err := g.Box(messagesTemplate); err != nil { return g, err } @@ -378,7 +378,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *PacketOptions) genny } } -func codecModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn { +func codecModify(opts *PacketOptions) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/codec.go") f, err := r.Disk.Find(path) @@ -387,16 +387,24 @@ func codecModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunFn } // Set import if not set yet - replacement := `sdk "github.com/cosmos/cosmos-sdk/types"` - content := replacer.ReplaceOnce(f.String(), module.Placeholder, replacement) + content, err := xast.AppendImports(f.String(), xast.WithLastNamedImport("sdk", "github.com/cosmos/cosmos-sdk/types")) + if err != nil { + return err + } // Register the module packet interface templateInterface := `registrar.RegisterImplementations((*sdk.Msg)(nil), - &MsgSend%[2]v{}, -) -%[1]v` - replacementInterface := fmt.Sprintf(templateInterface, module.Placeholder3, opts.PacketName.UpperCamel) - content = replacer.Replace(content, module.Placeholder3, replacementInterface) + &MsgSend%[1]v{}, +)` + replacementInterface := fmt.Sprintf(templateInterface, opts.PacketName.UpperCamel) + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncAtLine(replacementInterface, 0), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/message/message.go b/ignite/templates/message/message.go index 9fa658adb8..3bfbb77781 100644 --- a/ignite/templates/message/message.go +++ b/ignite/templates/message/message.go @@ -14,6 +14,7 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/pkg/placeholder" "github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v29/ignite/pkg/xast" "github.com/ignite/cli/v29/ignite/pkg/xgenny" "github.com/ignite/cli/v29/ignite/templates/field/plushhelpers" "github.com/ignite/cli/v29/ignite/templates/testutil" @@ -176,19 +177,29 @@ func typesCodecModify(replacer placeholder.Replacer, opts *Options) genny.RunFn if err != nil { return err } - replacementImport := `sdk "github.com/cosmos/cosmos-sdk/types"` - content := replacer.ReplaceOnce(f.String(), Placeholder, replacementImport) + + // Import + content, err := xast.AppendImports(f.String(), xast.WithLastNamedImport("sdk", "github.com/cosmos/cosmos-sdk/types")) + if err != nil { + return err + } templateRegisterImplementations := `registrar.RegisterImplementations((*sdk.Msg)(nil), - &Msg%[2]v{}, -) -%[1]v` + &Msg%[1]v{}, +)` replacementRegisterImplementations := fmt.Sprintf( templateRegisterImplementations, - Placeholder3, opts.MsgName.UpperCamel, ) - content = replacer.Replace(content, Placeholder3, replacementRegisterImplementations) + + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncAtLine(replacementRegisterImplementations, 0), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/message/placeholders.go b/ignite/templates/message/placeholders.go deleted file mode 100644 index c6cf265b8a..0000000000 --- a/ignite/templates/message/placeholders.go +++ /dev/null @@ -1,6 +0,0 @@ -package message - -const ( - Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder3 = "// this line is used by starport scaffolding # 3" -) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/codec.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/codec.go.plush index 301948bdb7..a19449d7dd 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/codec.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/codec.go.plush @@ -7,13 +7,9 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" - - // this line is used by starport scaffolding # 1 ) func RegisterInterfaces(registrar registry.InterfaceRegistrar) { - // this line is used by starport scaffolding # 3 - registrar.RegisterImplementations((*sdk.Msg)(nil), &MsgUpdateParams{}, ) diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush index 9c741f7f66..d7e9110548 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/types/genesis.go.plush @@ -3,7 +3,6 @@ package types // DefaultGenesis returns the default genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - // this line is used by starport scaffolding # genesis/types/default Params: DefaultParams(), } } @@ -11,7 +10,5 @@ func DefaultGenesis() *GenesisState { // Validate performs basic genesis state validation returning an error upon any // failure. func (gs GenesisState) Validate() error { - // this line is used by starport scaffolding # genesis/types/validate - return gs.Params.Validate() } diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index 3e5ad04196..0713720cfb 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -27,7 +27,7 @@ func NewIBC(replacer placeholder.Replacer, opts *CreateOptions) (*genny.Generato ) g.RunFn(genesisModify(replacer, opts)) - g.RunFn(genesisTypesModify(replacer, opts)) + g.RunFn(genesisTypesModify(opts)) g.RunFn(genesisProtoModify(opts)) g.RunFn(keysModify(replacer, opts)) @@ -100,7 +100,7 @@ if k.ShouldBound(ctx, genState.PortId) { } } -func genesisTypesModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunFn { +func genesisTypesModify(opts *CreateOptions) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis.go") f, err := r.Disk.Find(path) @@ -118,19 +118,28 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *CreateOptions) genn } // Default genesis - templateDefault := `PortId: PortID, -%s` - replacementDefault := fmt.Sprintf(templateDefault, typed.PlaceholderGenesisTypesDefault) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementDefault) + content, err = xast.ModifyFunction( + content, + "DefaultGenesis", + xast.AppendInsideFuncStruct("GenesisState", "PortId", "PortID", -1), + ) + if err != nil { + return err + } // Validate genesis // PlaceholderIBCGenesisTypeValidate - templateValidate := `if err := host.PortIdentifierValidator(gs.PortId); err != nil { + replacementTypesValidate := `if err := host.PortIdentifierValidator(gs.PortId); err != nil { return err -} -%s` - replacementValidate := fmt.Sprintf(templateValidate, typed.PlaceholderGenesisTypesValidate) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesValidate, replacementValidate) +}` + content, err = xast.ModifyFunction( + content, + "Validate", + xast.AppendFuncCode(replacementTypesValidate), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/module/create/msgserver.go b/ignite/templates/module/create/msgserver.go index d2e2b8a9f0..a5d5bc0e08 100644 --- a/ignite/templates/module/create/msgserver.go +++ b/ignite/templates/module/create/msgserver.go @@ -9,6 +9,7 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/gomodulepath" "github.com/ignite/cli/v29/ignite/pkg/placeholder" + "github.com/ignite/cli/v29/ignite/pkg/xast" "github.com/ignite/cli/v29/ignite/pkg/xgenny" "github.com/ignite/cli/v29/ignite/templates/field/plushhelpers" "github.com/ignite/cli/v29/ignite/templates/module" @@ -64,11 +65,14 @@ func codecPath(replacer placeholder.Replacer, appPath, moduleName string) genny. content := replacer.Replace(f.String(), oldImport, newImport) // Add RegisterMsgServiceDesc method call - template := `%[1]v - -msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)` - replacement := fmt.Sprintf(template, module.Placeholder3) - content = replacer.Replace(content, module.Placeholder3, replacement) + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncCode("msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)"), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) } diff --git a/ignite/templates/module/placeholders.go b/ignite/templates/module/placeholders.go index 84c4040443..a3399ba53a 100644 --- a/ignite/templates/module/placeholders.go +++ b/ignite/templates/module/placeholders.go @@ -2,9 +2,6 @@ package module //nolint:godot const ( - Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder3 = "// this line is used by starport scaffolding # 3" - // Placeholders in app.go PlaceholderSgAppKeeperDeclaration = "// this line is used by starport scaffolding # stargate/app/keeperDeclaration" PlaceholderSgAppInitGenesis = "// this line is used by starport scaffolding # stargate/app/initGenesis" diff --git a/ignite/templates/typed/list/genesis.go b/ignite/templates/typed/list/genesis.go index bf60552b23..5542adcc80 100644 --- a/ignite/templates/typed/list/genesis.go +++ b/ignite/templates/typed/list/genesis.go @@ -16,7 +16,7 @@ import ( func genesisModify(replacer placeholder.Replacer, opts *typed.Options, g *genny.Generator) { g.RunFn(genesisProtoModify(opts)) - g.RunFn(genesisTypesModify(replacer, opts)) + g.RunFn(genesisTypesModify(opts)) g.RunFn(genesisModuleModify(replacer, opts)) g.RunFn(genesisTestsModify(replacer, opts)) g.RunFn(genesisTypesTestsModify(replacer, opts)) @@ -67,7 +67,7 @@ func genesisProtoModify(opts *typed.Options) genny.RunFn { } } -func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { +func genesisTypesModify(opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis.go") f, err := r.Disk.Find(path) @@ -80,35 +80,42 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - templateTypesDefault := `%[2]vList: []%[2]v{}, -%[1]v` - replacementTypesDefault := fmt.Sprintf( - templateTypesDefault, - typed.PlaceholderGenesisTypesDefault, - opts.TypeName.UpperCamel, - ) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault) + // add parameter to the struct into the new method. + content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendInsideFuncStruct( + "GenesisState", + fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel), + fmt.Sprintf("[]%[1]v{}", opts.TypeName.UpperCamel), + -1, + )) + if err != nil { + return err + } - templateTypesValidate := `// Check for duplicated ID in %[2]v -%[2]vIdMap := make(map[uint64]bool) -%[2]vCount := gs.Get%[3]vCount() -for _, elem := range gs.%[3]vList { - if _, ok := %[2]vIdMap[elem.Id]; ok { - return fmt.Errorf("duplicated id for %[2]v") + templateTypesValidate := `// Check for duplicated ID in %[1]v +%[1]vIdMap := make(map[uint64]bool) +%[1]vCount := gs.Get%[2]vCount() +for _, elem := range gs.%[2]vList { + if _, ok := %[1]vIdMap[elem.Id]; ok { + return fmt.Errorf("duplicated id for %[1]v") } - if elem.Id >= %[2]vCount { - return fmt.Errorf("%[2]v id should be lower or equal than the last id") + if elem.Id >= %[1]vCount { + return fmt.Errorf("%[1]v id should be lower or equal than the last id") } - %[2]vIdMap[elem.Id] = true -} -%[1]v` + %[1]vIdMap[elem.Id] = true +}` replacementTypesValidate := fmt.Sprintf( templateTypesValidate, - typed.PlaceholderGenesisTypesValidate, opts.TypeName.LowerCamel, opts.TypeName.UpperCamel, ) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesValidate, replacementTypesValidate) + content, err = xast.ModifyFunction( + content, + "Validate", + xast.AppendFuncCode(replacementTypesValidate), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/typed/list/list.go b/ignite/templates/typed/list/list.go index c3aa8f94f8..92a9a42b59 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -14,6 +14,7 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/gomodulepath" "github.com/ignite/cli/v29/ignite/pkg/placeholder" "github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v29/ignite/pkg/xast" "github.com/ignite/cli/v29/ignite/pkg/xgenny" "github.com/ignite/cli/v29/ignite/templates/typed" ) @@ -350,18 +351,26 @@ func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny. } // Import - replacementImport := `sdk "github.com/cosmos/cosmos-sdk/types"` - content := replacer.ReplaceOnce(f.String(), typed.Placeholder, replacementImport) + content, err := xast.AppendImports(f.String(), xast.WithLastNamedImport("sdk", "github.com/cosmos/cosmos-sdk/types")) + if err != nil { + return err + } // Interface templateInterface := `registrar.RegisterImplementations((*sdk.Msg)(nil), - &MsgCreate%[2]v{}, - &MsgUpdate%[2]v{}, - &MsgDelete%[2]v{}, -) -%[1]v` - replacementInterface := fmt.Sprintf(templateInterface, typed.Placeholder3, opts.TypeName.UpperCamel) - content = replacer.Replace(content, typed.Placeholder3, replacementInterface) + &MsgCreate%[1]v{}, + &MsgUpdate%[1]v{}, + &MsgDelete%[1]v{}, +)` + replacementInterface := fmt.Sprintf(templateInterface, opts.TypeName.UpperCamel) + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncAtLine(replacementInterface, 0), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 2dd0408242..3c57de4a1a 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -80,7 +80,7 @@ func NewGenerator(replacer placeholder.Replacer, opts *typed.Options) (*genny.Ge g.RunFn(keeperModify(replacer, opts)) g.RunFn(clientCliQueryModify(replacer, opts)) g.RunFn(genesisProtoModify(opts)) - g.RunFn(genesisTypesModify(replacer, opts)) + g.RunFn(genesisTypesModify(opts)) g.RunFn(genesisModuleModify(replacer, opts)) g.RunFn(genesisTestsModify(replacer, opts)) g.RunFn(genesisTypesTestsModify(replacer, opts)) @@ -89,7 +89,7 @@ func NewGenerator(replacer placeholder.Replacer, opts *typed.Options) (*genny.Ge if !opts.NoMessage { g.RunFn(protoTxModify(opts)) g.RunFn(clientCliTxModify(replacer, opts)) - g.RunFn(typesCodecModify(replacer, opts)) + g.RunFn(typesCodecModify(opts)) if !opts.NoSimulation { g.RunFn(moduleSimulationModify(replacer, opts)) @@ -342,7 +342,7 @@ func genesisProtoModify(opts *typed.Options) genny.RunFn { } } -func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { +func genesisTypesModify(opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis.go") f, err := r.Disk.Find(path) @@ -355,37 +355,42 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - templateTypesDefault := `%[2]vList: []%[2]v{}, -%[1]v` - replacementTypesDefault := fmt.Sprintf( - templateTypesDefault, - typed.PlaceholderGenesisTypesDefault, - opts.TypeName.UpperCamel, - ) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault) + content, err = xast.ModifyFunction(content, "DefaultGenesis", xast.AppendInsideFuncStruct( + "GenesisState", + fmt.Sprintf("%[1]vList", opts.TypeName.UpperCamel), + fmt.Sprintf("[]%[1]v{}", opts.TypeName.UpperCamel), + -1, + )) + if err != nil { + return err + } // lines of code to call the key function with the indexes of the element keyCall := fmt.Sprintf(`fmt.Sprint(elem.%s)`, opts.Index.Name.UpperCamel) + templateTypesValidate := `// Check for duplicated index in %[1]v +%[1]vIndexMap := make(map[string]struct{}) - templateTypesValidate := `// Check for duplicated index in %[2]v -%[2]vIndexMap := make(map[string]struct{}) - -for _, elem := range gs.%[3]vList { - index := %[4]v - if _, ok := %[2]vIndexMap[index]; ok { - return fmt.Errorf("duplicated index for %[2]v") +for _, elem := range gs.%[2]vList { + index := %[3]v + if _, ok := %[1]vIndexMap[index]; ok { + return fmt.Errorf("duplicated index for %[1]v") } - %[2]vIndexMap[index] = struct{}{} -} -%[1]v` + %[1]vIndexMap[index] = struct{}{} +}` replacementTypesValidate := fmt.Sprintf( templateTypesValidate, - typed.PlaceholderGenesisTypesValidate, opts.TypeName.LowerCamel, opts.TypeName.UpperCamel, keyCall, ) - content = replacer.Replace(content, typed.PlaceholderGenesisTypesValidate, replacementTypesValidate) + content, err = xast.ModifyFunction( + content, + "Validate", + xast.AppendFuncCode(replacementTypesValidate), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) @@ -695,7 +700,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny } } -func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { +func typesCodecModify(opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/codec.go") f, err := r.Disk.Find(path) @@ -703,25 +708,30 @@ func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny. return err } - content := f.String() - // Import - replacementImport := `sdk "github.com/cosmos/cosmos-sdk/types"` - content = replacer.ReplaceOnce(content, typed.Placeholder, replacementImport) + content, err := xast.AppendImports(f.String(), xast.WithLastNamedImport("sdk", "github.com/cosmos/cosmos-sdk/types")) + if err != nil { + return err + } // Interface templateInterface := `registrar.RegisterImplementations((*sdk.Msg)(nil), - &MsgCreate%[2]v{}, - &MsgUpdate%[2]v{}, - &MsgDelete%[2]v{}, -) -%[1]v` + &MsgCreate%[1]v{}, + &MsgUpdate%[1]v{}, + &MsgDelete%[1]v{}, +)` replacementInterface := fmt.Sprintf( templateInterface, - typed.Placeholder3, opts.TypeName.UpperCamel, ) - content = replacer.Replace(content, typed.Placeholder3, replacementInterface) + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncAtLine(replacementInterface, 0), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) diff --git a/ignite/templates/typed/placeholders.go b/ignite/templates/typed/placeholders.go index f574119b87..c348e2c739 100644 --- a/ignite/templates/typed/placeholders.go +++ b/ignite/templates/typed/placeholders.go @@ -2,15 +2,11 @@ package typed //nolint:godot const ( - Placeholder = "// this line is used by starport scaffolding # 1" - Placeholder3 = "// this line is used by starport scaffolding # 3" Placeholder4 = "" // Genesis - PlaceholderGenesisTypesDefault = "// this line is used by starport scaffolding # genesis/types/default" - PlaceholderGenesisTypesValidate = "// this line is used by starport scaffolding # genesis/types/validate" - PlaceholderGenesisModuleInit = "// this line is used by starport scaffolding # genesis/module/init" - PlaceholderGenesisModuleExport = "// this line is used by starport scaffolding # genesis/module/export" + PlaceholderGenesisModuleInit = "// this line is used by starport scaffolding # genesis/module/init" + PlaceholderGenesisModuleExport = "// this line is used by starport scaffolding # genesis/module/export" PlaceholderSimappConst = "// this line is used by starport scaffolding # simapp/module/const" PlaceholderSimappGenesisState = "// this line is used by starport scaffolding # simapp/module/genesisState" diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index dd8b855329..28d280ec02 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -15,6 +15,7 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/gomodulepath" "github.com/ignite/cli/v29/ignite/pkg/placeholder" "github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil" + "github.com/ignite/cli/v29/ignite/pkg/xast" "github.com/ignite/cli/v29/ignite/pkg/xgenny" "github.com/ignite/cli/v29/ignite/templates/module" "github.com/ignite/cli/v29/ignite/templates/typed" @@ -58,7 +59,7 @@ func NewGenerator(replacer placeholder.Replacer, opts *typed.Options) (*genny.Ge g.RunFn(keeperModify(replacer, opts)) g.RunFn(clientCliQueryModify(replacer, opts)) g.RunFn(genesisProtoModify(opts)) - g.RunFn(genesisTypesModify(replacer, opts)) + g.RunFn(genesisTypesModify(opts)) g.RunFn(genesisModuleModify(replacer, opts)) g.RunFn(genesisTestsModify(replacer, opts)) g.RunFn(genesisTypesTestsModify(replacer, opts)) @@ -67,7 +68,7 @@ func NewGenerator(replacer placeholder.Replacer, opts *typed.Options) (*genny.Ge if !opts.NoMessage { g.RunFn(protoTxModify(opts)) g.RunFn(clientCliTxModify(replacer, opts)) - g.RunFn(typesCodecModify(replacer, opts)) + g.RunFn(typesCodecModify(opts)) if !opts.NoSimulation { g.RunFn(moduleSimulationModify(replacer, opts)) @@ -265,7 +266,7 @@ func genesisProtoModify(opts *typed.Options) genny.RunFn { } } -func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { +func genesisTypesModify(opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis.go") f, err := r.Disk.Find(path) @@ -273,14 +274,15 @@ func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genn return err } - templateTypesDefault := `%[2]v: nil, -%[1]v` - replacementTypesDefault := fmt.Sprintf( - templateTypesDefault, - typed.PlaceholderGenesisTypesDefault, - opts.TypeName.UpperCamel, - ) - content := replacer.Replace(f.String(), typed.PlaceholderGenesisTypesDefault, replacementTypesDefault) + content, err := xast.ModifyFunction(f.String(), "DefaultGenesis", xast.AppendInsideFuncStruct( + "GenesisState", + fmt.Sprintf("%[1]v", opts.TypeName.UpperCamel), + "nil", + -1, + )) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile) @@ -550,7 +552,7 @@ func clientCliTxModify(replacer placeholder.Replacer, opts *typed.Options) genny } } -func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn { +func typesCodecModify(opts *typed.Options) genny.RunFn { return func(r *genny.Runner) error { path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/codec.go") f, err := r.Disk.Find(path) @@ -558,25 +560,30 @@ func typesCodecModify(replacer placeholder.Replacer, opts *typed.Options) genny. return err } - content := f.String() - // Import - replacementImport := `sdk "github.com/cosmos/cosmos-sdk/types"` - content = replacer.ReplaceOnce(content, typed.Placeholder, replacementImport) + content, err := xast.AppendImports(f.String(), xast.WithLastNamedImport("sdk", "github.com/cosmos/cosmos-sdk/types")) + if err != nil { + return err + } // Interface templateInterface := `registrar.RegisterImplementations((*sdk.Msg)(nil), - &MsgCreate%[2]v{}, - &MsgUpdate%[2]v{}, - &MsgDelete%[2]v{}, -) -%[1]v` + &MsgCreate%[1]v{}, + &MsgUpdate%[1]v{}, + &MsgDelete%[1]v{}, +)` replacementInterface := fmt.Sprintf( templateInterface, - typed.Placeholder3, opts.TypeName.UpperCamel, ) - content = replacer.Replace(content, typed.Placeholder3, replacementInterface) + content, err = xast.ModifyFunction( + content, + "RegisterInterfaces", + xast.AppendFuncAtLine(replacementInterface, 0), + ) + if err != nil { + return err + } newFile := genny.NewFileS(path, content) return r.File(newFile)