Skip to content

Commit

Permalink
remove some modules placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Dec 4, 2024
1 parent 7dbbee8 commit 404f2a3
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 230 deletions.
170 changes: 85 additions & 85 deletions docs/docs/02-guide/04-ibc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down Expand Up @@ -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})
}
```

Expand All @@ -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")
}
}
```

Expand All @@ -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,
},
)
}
```

Expand Down
28 changes: 18 additions & 10 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 18 additions & 7 deletions ignite/templates/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions ignite/templates/message/placeholders.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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{},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ 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(),
}
}

// 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()
}
31 changes: 20 additions & 11 deletions ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 404f2a3

Please sign in to comment.