Skip to content

Commit 13e77ed

Browse files
committed
create band cli command
1 parent 2697e4f commit 13e77ed

File tree

19 files changed

+222
-114
lines changed

19 files changed

+222
-114
lines changed

starport/cmd/scaffold.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewScaffold() *cobra.Command {
2727
Args: cobra.ExactArgs(1),
2828
}
2929

30+
c.AddCommand(NewScaffoldBandchain())
3031
c.AddCommand(NewScaffoldChain())
3132
c.AddCommand(NewScaffoldModule())
3233
c.AddCommand(NewScaffoldList())

starport/cmd/scaffold_band.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package starportcmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/tendermint/starport/starport/pkg/clispinner"
9+
"github.com/tendermint/starport/starport/pkg/placeholder"
10+
"github.com/tendermint/starport/starport/services/scaffolder"
11+
)
12+
13+
// NewScaffoldBandchain creates a new Bandchain oracle in the module
14+
func NewScaffoldBandchain() *cobra.Command {
15+
c := &cobra.Command{
16+
Use: "band [oracleName] ... --module [moduleName]",
17+
Short: "Scaffold an IBC Bandchain oracle",
18+
Long: "Scaffold an IBC Bandchain oracle in a specific IBC-enabled Cosmos SDK module",
19+
Args: cobra.MinimumNArgs(1),
20+
RunE: createBandchainHandler,
21+
}
22+
23+
c.Flags().String(flagModule, "", "IBC Module to add the packet into")
24+
25+
return c
26+
}
27+
28+
func createBandchainHandler(cmd *cobra.Command, args []string) error {
29+
s := clispinner.New().SetText("Scaffolding...")
30+
defer s.Stop()
31+
32+
oracle := args[0]
33+
module, err := cmd.Flags().GetString(flagModule)
34+
if err != nil {
35+
return err
36+
}
37+
if module == "" {
38+
return errors.New("please specify a module to create the packet into: --module <module_name>")
39+
}
40+
41+
sc, err := scaffolder.New(appPath)
42+
if err != nil {
43+
return err
44+
}
45+
sm, err := sc.AddOracle(placeholder.New(), module, oracle)
46+
if err != nil {
47+
return err
48+
}
49+
50+
s.Stop()
51+
52+
fmt.Println(sourceModificationToString(sm))
53+
fmt.Printf("\n🎉 Created a packet `%[1]v`.\n\n", args[0])
54+
return nil
55+
}

starport/cmd/scaffold_module.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
const (
1919
flagDep = "dep"
2020
flagIBC = "ibc"
21-
flagOracle = "oracle"
2221
flagIBCOrdering = "ordering"
2322
flagRequireRegistration = "require-registration"
2423
)
@@ -34,7 +33,6 @@ func NewScaffoldModule() *cobra.Command {
3433
}
3534
c.Flags().StringSlice(flagDep, []string{}, "module dependencies (e.g. --dep account,bank)")
3635
c.Flags().Bool(flagIBC, false, "scaffold an IBC module")
37-
c.Flags().Bool(flagOracle, false, "scaffold a Bandchain oracle into the module to send queries to the oracle chain")
3836
c.Flags().String(flagIBCOrdering, "none", "channel ordering of the IBC module [none|ordered|unordered]")
3937
c.Flags().Bool(flagRequireRegistration, false, "if true command will fail if module can't be registered")
4038
return c
@@ -62,16 +60,6 @@ func scaffoldModuleHandler(cmd *cobra.Command, args []string) error {
6260
return err
6361
}
6462

65-
// Check if the module must be a Bandchain oracle module
66-
oracle, err := cmd.Flags().GetBool(flagOracle)
67-
if err != nil {
68-
return err
69-
}
70-
if oracle {
71-
ibcModule = true
72-
options = append(options, scaffolder.WithOracleIntegration())
73-
}
74-
7563
// Check if the module must be an IBC module
7664
if ibcModule {
7765
options = append(options, scaffolder.WithIBCChannelOrdering(ibcOrdering), scaffolder.WithIBC())

starport/services/scaffolder/module.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ const (
3535

3636
// moduleCreationOptions holds options for creating a new module
3737
type moduleCreationOptions struct {
38-
// ibc if ibc is needed
38+
// chainID is the chain's id.
3939
ibc bool
4040

41-
// oracle if oracle integration is needed
42-
oracle bool
43-
4441
// homePath of the chain's config dir.
4542
ibcChannelOrdering string
4643

@@ -72,13 +69,6 @@ func WithIBCChannelOrdering(ordering string) ModuleCreationOption {
7269
}
7370
}
7471

75-
// WithOracleIntegration configures the Bandchain oracle integration
76-
func WithOracleIntegration() ModuleCreationOption {
77-
return func(m *moduleCreationOptions) {
78-
m.oracle = true
79-
}
80-
}
81-
8272
// WithDependencies specifies the name of the modules that the module depends on
8373
func WithDependencies(dependencies []modulecreate.Dependency) ModuleCreationOption {
8474
return func(m *moduleCreationOptions) {
@@ -133,7 +123,6 @@ func (s *Scaffolder) CreateModule(
133123
AppName: path.Package,
134124
OwnerName: owner(path.RawPath),
135125
IsIBC: creationOpts.ibc,
136-
IsOracle: creationOpts.oracle,
137126
IBCOrdering: creationOpts.ibcChannelOrdering,
138127
Dependencies: creationOpts.dependencies,
139128
}
@@ -147,16 +136,7 @@ func (s *Scaffolder) CreateModule(
147136

148137
// Scaffold IBC module
149138
if opts.IsIBC {
150-
g, err := modulecreate.NewIBC(tracer, opts)
151-
if err != nil {
152-
return sm, err
153-
}
154-
gens = append(gens, g)
155-
}
156-
157-
// Scaffold Oracle
158-
if opts.IsOracle {
159-
g, err := modulecreate.NewOracle(tracer, opts)
139+
g, err = modulecreate.NewIBC(tracer, opts)
160140
if err != nil {
161141
return sm, err
162142
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package scaffolder
2+
3+
import (
4+
"fmt"
5+
"github.com/gobuffalo/genny"
6+
"github.com/tendermint/starport/starport/pkg/gomodulepath"
7+
"github.com/tendermint/starport/starport/pkg/multiformatname"
8+
"github.com/tendermint/starport/starport/pkg/placeholder"
9+
"github.com/tendermint/starport/starport/pkg/xgenny"
10+
"github.com/tendermint/starport/starport/templates/ibc"
11+
"os"
12+
)
13+
14+
// AddOracle adds a new Bandchain oracle integration.
15+
func (s *Scaffolder) AddOracle(
16+
tracer *placeholder.Tracer,
17+
moduleName,
18+
oracleName string,
19+
) (sm xgenny.SourceModification, err error) {
20+
path, err := gomodulepath.ParseAt(s.path)
21+
if err != nil {
22+
return sm, err
23+
}
24+
25+
mfName, err := multiformatname.NewName(moduleName, multiformatname.NoNumber)
26+
if err != nil {
27+
return sm, err
28+
}
29+
moduleName = mfName.Lowercase
30+
31+
name, err := multiformatname.NewName(oracleName)
32+
if err != nil {
33+
return sm, err
34+
}
35+
36+
if err := checkComponentValidity(s.path, moduleName, name); err != nil {
37+
return sm, err
38+
}
39+
40+
// Module must implement IBC
41+
ok, err := isIBCModule(s.path, moduleName)
42+
if err != nil {
43+
return sm, err
44+
}
45+
if !ok {
46+
return sm, fmt.Errorf("the module %s doesn't implement IBC module interface", moduleName)
47+
}
48+
49+
// Generate the packet
50+
var (
51+
g *genny.Generator
52+
opts = &ibc.OracleOptions{
53+
AppName: path.Package,
54+
ModulePath: path.RawPath,
55+
ModuleName: moduleName,
56+
OwnerName: owner(path.RawPath),
57+
OracleName: name,
58+
}
59+
)
60+
g, err = ibc.NewOracle(tracer, opts)
61+
if err != nil {
62+
return sm, err
63+
}
64+
sm, err = xgenny.RunWithValidation(tracer, g)
65+
if err != nil {
66+
return sm, err
67+
}
68+
pwd, err := os.Getwd()
69+
if err != nil {
70+
return sm, err
71+
}
72+
return sm, s.finish(pwd, path.RawPath)
73+
}

0 commit comments

Comments
 (0)