Skip to content

Commit 1a4e252

Browse files
authored
feat: add decimal coin type (#4758)
* feat: add decimal coin type * add integration tests * add changelog * fix deccoin parser for simulation * fix dec coin parse for simulation * add the dec.coin missing case for the field switch
1 parent 838cbb0 commit 1a4e252

File tree

11 files changed

+119
-1
lines changed

11 files changed

+119
-1
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- [#4676](https://github.com/ignite/cli/issues/4676) Add Decimal Coin Type.
78
- [#4765](https://github.com/ignite/cli/pull/4765) Create `scaffold type-list` command.
89

910
### Changes

ignite/templates/field/datatype/coin.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
DefaultTestValue: "20stake",
4848
ValueLoop: "sdk.NewCoins(sdk.NewInt64Coin(`token`, int64(i%1+100)), sdk.NewInt64Coin(`stake`, int64(i%2+100)))",
4949
ProtoType: func(_, name string, index int) string {
50-
return fmt.Sprintf("repeated cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]",
50+
return fmt.Sprintf(`repeated cosmos.base.v1beta1.Coin %s = %d [(gogoproto.nullable) = false]`,
5151
name, index)
5252
},
5353
GenesisArgs: func(multiformatname.Name, int) string { return "" },
@@ -67,4 +67,63 @@ var (
6767
)
6868
},
6969
}
70+
71+
// DataDecCoin decimal coin data type definition.
72+
DataDecCoin = DataType{
73+
DataType: func(string) string { return "sdk.DecCoin" },
74+
CollectionsKeyValueName: func(string) string { return collectionValueComment },
75+
DefaultTestValue: "100001token",
76+
ValueLoop: "sdk.NewInt64DecCoin(`token`, int64(i+100))",
77+
ProtoType: func(_, name string, index int) string {
78+
return fmt.Sprintf("cosmos.base.v1beta1.DecCoin %s = %d [(gogoproto.nullable) = false]",
79+
name, index)
80+
},
81+
GenesisArgs: func(multiformatname.Name, int) string { return "" },
82+
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
83+
return fmt.Sprintf(`%s%s, err := sdk.ParseDecCoins(args[%d])
84+
if err != nil {
85+
return err
86+
}`, prefix, name.UpperCamel, argIndex)
87+
},
88+
GoCLIImports: []GoImport{{Name: "github.com/cosmos/cosmos-sdk/types", Alias: "sdk"}},
89+
ProtoImports: []string{"gogoproto/gogo.proto", "cosmos/base/v1beta1/coin.proto"},
90+
NonIndex: true,
91+
ToProtoField: func(_, name string, index int) *proto.NormalField {
92+
option := protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())
93+
return protoutil.NewField(
94+
name, "cosmos.base.v1beta1.DecCoin", index, protoutil.WithFieldOptions(option),
95+
)
96+
},
97+
}
98+
99+
// DataDecCoinSlice is a decimal coin array data type definition.
100+
DataDecCoinSlice = DataType{
101+
DataType: func(string) string { return "sdk.DecCoins" },
102+
CollectionsKeyValueName: func(string) string { return collectionValueComment },
103+
DefaultTestValue: "20000002stake",
104+
ValueLoop: "sdk.NewDecCoins(sdk.NewInt64DecCoin(`token`, int64(i%1+100)), sdk.NewInt64DecCoin(`stake`, int64(i%2+100)))",
105+
ProtoType: func(_, name string, index int) string {
106+
return fmt.Sprintf(`repeated cosmos.base.v1beta1.DecCoin %s = %d [(gogoproto.nullable) = false]`,
107+
name, index)
108+
},
109+
GenesisArgs: func(multiformatname.Name, int) string { return "" },
110+
CLIArgs: func(name multiformatname.Name, _, prefix string, argIndex int) string {
111+
return fmt.Sprintf(`%s%s, err := sdk.ParseDecCoins(args[%d])
112+
if err != nil {
113+
return err
114+
}`, prefix, name.UpperCamel, argIndex)
115+
},
116+
GoCLIImports: []GoImport{{Name: "github.com/cosmos/cosmos-sdk/types", Alias: "sdk"}},
117+
ProtoImports: []string{"gogoproto/gogo.proto", "cosmos/base/v1beta1/coin.proto"},
118+
NonIndex: true,
119+
ToProtoField: func(_, name string, index int) *proto.NormalField {
120+
optionNullable := protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())
121+
optionCast := protoutil.NewOption("gogoproto.castrepeated", "github.com/cosmos/cosmos-sdk/types.DecCoins", protoutil.Custom())
122+
return protoutil.NewField(name, "cosmos.base.v1beta1.DecCoin", index,
123+
protoutil.WithFieldOptions(optionNullable),
124+
protoutil.WithFieldOptions(optionCast),
125+
protoutil.Repeated(),
126+
)
127+
},
128+
}
70129
)

ignite/templates/field/datatype/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const (
3434
Coin Name = "coin"
3535
// Coins represents the coin array type name.
3636
Coins Name = "array.coin"
37+
// DecCoin represents the coin type name.
38+
DecCoin Name = "dec.coin"
39+
// DecCoins represents the decimal coin array type name.
40+
DecCoins Name = "dec.coins"
3741
// Bytes represents the bytes type name.
3842
Bytes Name = "bytes"
3943
// Address represents the address type name.
@@ -74,6 +78,8 @@ var supportedTypes = map[Name]DataType{
7478
Coin: DataCoin,
7579
Coins: DataCoinSlice,
7680
CoinSliceAlias: DataCoinSlice,
81+
DecCoin: DataDecCoin,
82+
DecCoins: DataDecCoinSlice,
7783
Address: DataAddress,
7884
Custom: DataCustom,
7985
}

ignite/templates/field/field.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (f Field) IsSlice() bool {
3939
datatype.IntSlice,
4040
datatype.UintSlice,
4141
datatype.Coins,
42+
datatype.DecCoins,
4243
datatype.StringSliceAlias,
4344
datatype.IntSliceAlias,
4445
datatype.UintSliceAlias,
@@ -53,6 +54,7 @@ func (f Field) IsSlice() bool {
5354
datatype.Int64,
5455
datatype.Uint,
5556
datatype.Uint64,
57+
datatype.DecCoin,
5658
datatype.Coin,
5759
datatype.Custom:
5860
return false

ignite/templates/message/files/message/x/{{moduleName}}/types/message_{{msgName}}.go.plush

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package types
22

3+
import (
4+
<%= for (import) in Fields.GoCLIImports() { %>
5+
<%= import.Alias %> "<%= import.Name %>"<% } %>
6+
)
7+
38
func NewMsg<%= MsgName.PascalCase %>(<%= MsgSigner.LowerCamel %> string<%= for (field) in Fields { %>, <%= field.Name.LowerCamel %> <%= field.DataType() %><% } %>) *Msg<%= MsgName.PascalCase %> {
49
return &Msg<%= MsgName.PascalCase %>{
510
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,<%= for (field) in Fields { %>

ignite/templates/typed/list/files/messages/x/{{moduleName}}/types/messages_{{typeName}}.go.plush

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package types
22

3+
import (
4+
<%= for (import) in Fields.GoCLIImports() { %>
5+
<%= import.Alias %> "<%= import.Name %>"<% } %>
6+
)
7+
38
func NewMsgCreate<%= TypeName.PascalCase %>(<%= MsgSigner.LowerCamel %> string<%= for (field) in Fields { %>, <%= field.Name.LowerCamel %> <%= field.DataType() %><% } %>) *MsgCreate<%= TypeName.PascalCase %> {
49
return &MsgCreate<%= TypeName.PascalCase %>{
510
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,<%= for (field) in Fields { %>

ignite/templates/typed/map/files/messages/x/{{moduleName}}/types/messages_{{typeName}}.go.plush

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package types
22

3+
import (
4+
<%= for (import) in Fields.GoCLIImports() { %>
5+
<%= import.Alias %> "<%= import.Name %>"<% } %>
6+
)
7+
38
func NewMsgCreate<%= TypeName.PascalCase %>(
49
<%= MsgSigner.LowerCamel %> string,
510
<%= Index.Name.LowerCamel %> <%= Index.DataType() %>,

ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/types/messages_{{typeName}}.go.plush

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package types
22

3+
import (
4+
<%= for (import) in Fields.GoCLIImports() { %>
5+
<%= import.Alias %> "<%= import.Name %>"<% } %>
6+
)
7+
38
func NewMsgCreate<%= TypeName.PascalCase %>(<%= MsgSigner.LowerCamel %> string<%= for (field) in Fields { %>, <%= field.Name.LowerCamel %> <%= field.DataType() %><% } %>) *MsgCreate<%= TypeName.PascalCase %> {
49
return &MsgCreate<%= TypeName.PascalCase %>{
510
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,<%= for (field) in Fields { %>

integration/list/cmd_list_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func TestGenerateAnAppWithListAndVerify(t *testing.T) {
6969
"example",
7070
)
7171

72+
app.Scaffold(
73+
"create a list with decimal coin",
74+
false,
75+
"list",
76+
"decimal",
77+
"deccointype:dec.coin",
78+
"deccoins:dec.coins",
79+
"--module",
80+
"example",
81+
)
82+
7283
app.Scaffold(
7384
"create a list with custom field type",
7485
false,

integration/map/cmd_map_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ func TestCreateMap(t *testing.T) {
5151
"--no-simulation",
5252
)
5353

54+
app.Scaffold(
55+
"create a map with decimal coin",
56+
false,
57+
"map",
58+
"decimal",
59+
"deccointype:dec.coin",
60+
"deccoins:dec.coins",
61+
"--module",
62+
"example",
63+
)
64+
5465
app.Scaffold(
5566
"should prevent creating a map with a typename that already exist",
5667
true,

0 commit comments

Comments
 (0)