Skip to content

Commit

Permalink
migrate wrapper to ts(-proto) and new version of cosmjs
Browse files Browse the repository at this point in the history
* several code generation improvements made.
  • Loading branch information
ilgooz committed Feb 28, 2021
1 parent 794550b commit 353052e
Show file tree
Hide file tree
Showing 12 changed files with 1,296 additions and 85 deletions.
1,204 changes: 1,195 additions & 9 deletions scripts/data/gen-nodetime/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion scripts/data/gen-nodetime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"build": "./node_modules/pkg/lib-es5/bin.js --debug -t node14-linux-x64,node14-macos-x64 -o nodetime ."
},
"dependencies": {
"@cosmjs/launchpad": "^0.23.2",
"@cosmjs/proto-signing": "^0.24.0-alpha.26",
"@cosmjs/stargate": "^0.24.0-alpha.26",
"@types/node": "^13.13.45",
"long": "^4.0.0",
"pkg": "^4.4.9",
Expand All @@ -22,7 +25,8 @@
"./node_modules/typescript/lib/**/*.ts",
"./node_modules/protobufjs/**/*",
"./node_modules/long/**/*",
"./node_modules/@types/**/*"
"./node_modules/@types/**/*",
"./node_modules/@cosmjs/**/*"
]
}
}
2 changes: 2 additions & 0 deletions scripts/install-light
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
go install ./...
12 changes: 8 additions & 4 deletions starport/pkg/cosmosanalysis/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,16 @@ func DiscoverModule(modulePath string) (msgs []string, err error) {
fname := fdecl.Name.Name

// find the struct name that method belongs to.
sexp, ok := fdecl.Recv.List[0].Type.(*ast.StarExpr)
t := fdecl.Recv.List[0].Type
sident, ok := t.(*ast.Ident)
if !ok {
return true
sexp, ok := t.(*ast.StarExpr)
if !ok {
return true
}
sident = sexp.X.(*ast.Ident)
}

sname := sexp.X.(*ast.Ident).Name
sname := sident.Name

// mark the requirement that this struct satisfies.
if _, ok := structs[sname]; !ok {
Expand Down
39 changes: 20 additions & 19 deletions starport/pkg/cosmosgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -47,13 +48,20 @@ var templates embed.FS

// tpl holds the js client template which is for wrapping the generated protobufjs types and rest client,
// utilizing cosmjs' type registry, tx signing & broadcasting through exported, high level txClient() and queryClient() funcs.
var tpl = template.Must(
template.New("client.js.tpl").
Funcs(template.FuncMap{
"camelCase": strcase.ToLowerCamel,
}).
ParseFS(templates, "templates/client.js.tpl"),
)
func tpl(protoPath string) *template.Template {
return template.Must(
template.New("client.ts.tpl").
Funcs(template.FuncMap{
"camelCase": strcase.ToLowerCamel,
"resolveFile": func(fullPath string) string {
rel, _ := filepath.Rel(protoPath, fullPath)
rel = strings.TrimSuffix(rel, ".proto")
return rel
},
}).
ParseFS(templates, "templates/client.ts.tpl"),
)
}

type generateOptions struct {
gomodPath string
Expand Down Expand Up @@ -257,32 +265,25 @@ func (g *generator) generateJS() error {
outREST = filepath.Join(out, "rest.ts")
)

if err := sta.Generate(g.ctx, outREST, srcspec, "2"); err != nil { // 2 points to sdk module name.
if err := sta.Generate(g.ctx, outREST, srcspec, "-1"); err != nil { // -1 removes the route namespace.
return err
}

// generate the client, the js wrapper.
outclient := filepath.Join(out, "index.js")
outclient := filepath.Join(out, "index.ts")
f, err := os.OpenFile(outclient, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

err = tpl.Execute(f, struct {
Module module.Module
TypesPath string
RESTPath string
}{
m,
"./types",
"./rest",
})
// generate the js client wrapper.
err = tpl(g.protoPath).Execute(f, struct{ Module module.Module }{m})
if err != nil {
return err
}

// generate .js and .d.ts files for ts files.
// generate .js and .d.ts files for all ts files.
if err := tsc.Generate(g.ctx, tsc.Config{
Include: []string{out + "/**/*.ts"},
CompilerOptions: tsc.CompilerOptions{
Expand Down
38 changes: 0 additions & 38 deletions starport/pkg/cosmosgen/templates/client.js.tpl

This file was deleted.

47 changes: 47 additions & 0 deletions starport/pkg/cosmosgen/templates/client.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { coins } from "@cosmjs/launchpad";
import { SigningStargateClient } from "@cosmjs/stargate";
import { Registry, OfflineSigner, EncodeObject, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { Api } from "./rest";
{{ range .Module.Msgs }}import { {{ .Name }} } from "./types/{{ resolveFile .FilePath }}";
{{ end }}

const types = [
{{ range .Module.Msgs }}["/{{ .URI }}", {{ .Name }}],
{{ end }}
];

const registry = new Registry(<any>types);

interface TxClientOptions {
addr: string
}

const txClient = async (wallet: OfflineSigner, { addr: addr }: TxClientOptions = { addr: "http://localhost:26657" }) => {
if (!wallet) throw new Error("wallet is required");
const client = await SigningStargateClient.connectWithSigner(addr, wallet, { registry });
const { address } = (await wallet.getAccounts())[0];
const fee = {
amount: coins(0, "token"),
gas: "200000",
};

return {
signAndBroadcast: (msgs: EncodeObject[]) => client.signAndBroadcast(address, msgs, fee),
{{ range .Module.Msgs }}{{ camelCase .Name }}: (data: {{ .Name }}): EncodeObject => ({ typeUrl: "/{{ .URI }}", value: data }),
{{ end }}
};
};

interface QueryClientOptions {
addr: string
}

const queryClient = async ({ addr: addr }: QueryClientOptions = { addr: "http://localhost:1317" }) => {
return new Api({ baseUrl: addr });
};

export {
txClient,
queryClient,
};
Binary file modified starport/pkg/nodetime/nodetime-darwin.tar.gz
Binary file not shown.
Binary file modified starport/pkg/nodetime/nodetime-linux.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions starport/pkg/nodetime/sta/sta.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Generate(ctx context.Context, outPath, specPath, moduleNameIndex string) er
nodetime.BinaryPath,
nodetime.CommandSTA,
"--module-name-index",
moduleNameIndex,
"-p",
specPath,
"-o",
Expand Down
29 changes: 15 additions & 14 deletions starport/pkg/nodetime/tsc/tsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
"github.com/tendermint/starport/starport/pkg/nodetime"
)

const nodeModulesPath = "/snapshot/gen-nodetime/node_modules"

var (
defaultConfig = func() Config {
return Config{
CompilerOptions: CompilerOptions{
Target: "es2020",
Module: "es6",
TypeRoots: []string{"/snapshot/gen-nodetime/node_modules/@types"},
Types: []string{"node"},
Paths: map[string][]string{
"*": {"/snapshot/gen-nodetime/node_modules/*"},
"long": {"/snapshot/gen-nodetime/node_modules/long/index.js"},
},
BaseURL: nodeModulesPath,
ModuleResolution: "node",
Target: "es2020",
Module: "es2020",
TypeRoots: []string{filepath.Join(nodeModulesPath, "@types")},
SkipLibCheck: true,
},
}
}
Expand All @@ -38,12 +38,13 @@ type Config struct {

// CompilerOptions section of tsconfig.json.
type CompilerOptions struct {
Declaration bool `json:"declaration"`
Paths map[string][]string `json:"paths"`
Target string `json:"target"`
Module string `json:"module"`
TypeRoots []string `json:"typeRoots"`
Types []string `json:"types"`
BaseURL string `json:"baseUrl"`
ModuleResolution string `json:"moduleResolution"`
Target string `json:"target"`
Module string `json:"module"`
TypeRoots []string `json:"typeRoots"`
Declaration bool `json:"declaration"`
SkipLibCheck bool `json:"skipLibCheck"`
}

var placeOnce sync.Once
Expand Down
3 changes: 3 additions & 0 deletions starport/templates/app/stargate/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"build": "vue-cli-service build"
},
"dependencies": {
"@cosmjs/launchpad": "0.23.2",
"@cosmjs/proto-signing": "0.24.0-alpha.26",
"@cosmjs/stargate": "0.24.0-alpha.26",
"@tendermint/vue": "0.1.12",
"core-js": "^3.6.5",
"vue": "^2.6.11",
Expand Down

0 comments on commit 353052e

Please sign in to comment.