diff --git a/changelog.md b/changelog.md index 7f93b02960..8048420065 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- [#4687](https://github.com/ignite/cli/pull/4687) Add address type with `scalar` annotations, and add `scalar` type to signer field. + ### Changes - [#4689](https://github.com/ignite/cli/pull/4689) Revert `HasGenesis` implementation from retracted `core` v1 to SDK `HasGenesis` interface. diff --git a/ignite/templates/field/datatype/address.go b/ignite/templates/field/datatype/address.go new file mode 100644 index 0000000000..0b6817df50 --- /dev/null +++ b/ignite/templates/field/datatype/address.go @@ -0,0 +1,42 @@ +package datatype + +import ( + "fmt" + + "github.com/emicklei/proto" + + "github.com/ignite/cli/v29/ignite/pkg/multiformatname" + "github.com/ignite/cli/v29/ignite/pkg/protoanalysis/protoutil" +) + +// DataAddress address (string) data type definition. +var DataAddress = DataType{ + DataType: func(string) string { return "string" }, + CollectionsKeyValueName: func(string) string { return "collections.StringKey" }, + DefaultTestValue: "cosmos1abcdefghijklmnopqrstuvwxyz0123456", + ValueLoop: "fmt.Sprintf(\"cosmos1abcdef%d\", i)", + ValueIndex: "\"cosmos1abcdefghijklmnopqrstuvwxyz0123456\"", + ValueInvalidIndex: "\"cosmos1invalid\"", + ProtoType: func(_, name string, index int) string { + return fmt.Sprintf("string %s = %d", name, index) + }, + GenesisArgs: func(name multiformatname.Name, value int) string { + return fmt.Sprintf("%s: \"%d\",\n", name.UpperCamel, value) + }, + CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string { + return fmt.Sprintf("%s%s := args[%d]", prefix, name.UpperCamel, argIndex) + }, + ToBytes: func(name string) string { + return fmt.Sprintf("%[1]vBytes := []byte(%[1]v)", name) + }, + ToString: func(name string) string { + return name + }, + ToProtoField: func(_, name string, index int) *proto.NormalField { + field := protoutil.NewField(name, "string", index) + option := protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom()) + field.Options = append(field.Options, option) + return field + }, + ProtoImports: []string{"cosmos_proto/cosmos.proto"}, +} diff --git a/ignite/templates/field/datatype/types.go b/ignite/templates/field/datatype/types.go index f886878e5d..87b0a66d0b 100644 --- a/ignite/templates/field/datatype/types.go +++ b/ignite/templates/field/datatype/types.go @@ -34,6 +34,8 @@ const ( Coins Name = "array.coin" // Bytes represents the bytes type name. Bytes Name = "bytes" + // Address represents the address type name. + Address Name = "address" // Custom represents the custom type name. Custom Name = Name(TypeCustom) @@ -70,6 +72,7 @@ var supportedTypes = map[Name]DataType{ Coin: DataCoin, Coins: DataCoinSlice, CoinSliceAlias: DataCoinSlice, + Address: DataAddress, Custom: DataCustom, } diff --git a/ignite/templates/field/datatype/types_test.go b/ignite/templates/field/datatype/types_test.go index 5cf3530816..d011a1c485 100644 --- a/ignite/templates/field/datatype/types_test.go +++ b/ignite/templates/field/datatype/types_test.go @@ -89,6 +89,11 @@ func TestIsSupportedType(t *testing.T) { typename: datatype.CoinSliceAlias, ok: true, }, + { + name: "address", + typename: datatype.Address, + ok: true, + }, { name: "invalid type name", typename: datatype.Name("invalid"), diff --git a/ignite/templates/field/field.go b/ignite/templates/field/field.go index 276284c904..e0a0002322 100644 --- a/ignite/templates/field/field.go +++ b/ignite/templates/field/field.go @@ -47,6 +47,7 @@ func (f Field) IsSlice() bool { return true case datatype.String, + datatype.Address, datatype.Bool, datatype.Int, datatype.Int64, diff --git a/ignite/templates/ibc/packet.go b/ignite/templates/ibc/packet.go index 3212471b13..adfd0ffaef 100644 --- a/ignite/templates/ibc/packet.go +++ b/ignite/templates/ibc/packet.go @@ -330,8 +330,13 @@ func protoTxModify(opts *PacketOptions) genny.RunFn { for i, field := range opts.Fields { sendFields = append(sendFields, field.ToProtoField(i+5)) } + + // set address options on signer field + signerField := protoutil.NewField(opts.MsgSigner.Snake, "string", 1) + signerField.Options = append(signerField.Options, protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom())) + sendFields = append(sendFields, - protoutil.NewField(opts.MsgSigner.Snake, "string", 1), + signerField, protoutil.NewField("port", "string", 2), protoutil.NewField("channelID", "string", 3), protoutil.NewField("timeoutTimestamp", "uint64", 4), diff --git a/ignite/templates/message/message.go b/ignite/templates/message/message.go index 233bd27ca5..1ff6e62935 100644 --- a/ignite/templates/message/message.go +++ b/ignite/templates/message/message.go @@ -132,6 +132,7 @@ func protoTxMessageModify(opts *Options) genny.RunFn { } // Prepare the fields and create the messages. creator := protoutil.NewField(opts.MsgSigner.Snake, "string", 1) + creator.Options = append(creator.Options, protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom())) // set the scalar annotation creatorOpt := protoutil.NewOption(typed.MsgSignerOption, opts.MsgSigner.Snake) msgFields := []*proto.NormalField{creator} for i, field := range opts.Fields { diff --git a/ignite/templates/typed/list/list.go b/ignite/templates/typed/list/list.go index 1c53a91814..94ba650cb1 100644 --- a/ignite/templates/typed/list/list.go +++ b/ignite/templates/typed/list/list.go @@ -140,6 +140,7 @@ func protoTxModify(opts *typed.Options) genny.RunFn { } // Messages creator := protoutil.NewField(opts.MsgSigner.Snake, "string", 1) + creator.Options = append(creator.Options, protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom())) // set the scalar annotation creatorOpt := protoutil.NewOption(typed.MsgSignerOption, opts.MsgSigner.Snake) createFields := []*proto.NormalField{creator} for i, field := range opts.Fields { diff --git a/ignite/templates/typed/map/map.go b/ignite/templates/typed/map/map.go index 0e1fb0fdfc..6f07016efc 100644 --- a/ignite/templates/typed/map/map.go +++ b/ignite/templates/typed/map/map.go @@ -621,6 +621,7 @@ func protoTxModify(opts *typed.Options) genny.RunFn { } creator := protoutil.NewField(opts.MsgSigner.Snake, "string", 1) + creator.Options = append(creator.Options, protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom())) // set the scalar annotation creatorOpt := protoutil.NewOption(typed.MsgSignerOption, opts.MsgSigner.Snake) commonFields := []*proto.NormalField{creator} commonFields = append(commonFields, index) diff --git a/ignite/templates/typed/singleton/singleton.go b/ignite/templates/typed/singleton/singleton.go index 0500ffc625..6cbe6a44e2 100644 --- a/ignite/templates/typed/singleton/singleton.go +++ b/ignite/templates/typed/singleton/singleton.go @@ -482,6 +482,7 @@ func protoTxModify(opts *typed.Options) genny.RunFn { // Add the messages creator := protoutil.NewField(opts.MsgSigner.Snake, "string", 1) + creator.Options = append(creator.Options, protoutil.NewOption("cosmos_proto.scalar", "cosmos.AddressString", protoutil.Custom())) // set the scalar annotation creatorOpt := protoutil.NewOption(typed.MsgSignerOption, opts.MsgSigner.Snake) fields := []*proto.NormalField{creator} for i, field := range opts.Fields { diff --git a/integration/other_components/cmd_message_test.go b/integration/other_components/cmd_message_test.go index 98327acfb3..6e2be81825 100644 --- a/integration/other_components/cmd_message_test.go +++ b/integration/other_components/cmd_message_test.go @@ -26,6 +26,7 @@ func TestGenerateAnAppWithMessage(t *testing.T) { "text", "vote:int", "like:bool", + "from:address", "-r", "foo,bar:int,foobar:bool", ),