Skip to content

Commit

Permalink
generators: support on-chain TypeScript SDK (#10)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: luc10921 <[email protected]>
  • Loading branch information
luc10921 and luc10921 authored Sep 19, 2023
1 parent e6c4ac8 commit d180fc3
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 18 deletions.
62 changes: 52 additions & 10 deletions generators/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generators
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -30,27 +31,37 @@ type (
Imports []string
Hash string
Methods []methodTmpl
Events []eventTmpl
}

methodTmpl struct {
Name string
NameABI string
Comment string
Arguments []paramTmpl
ReturnType string
Name string
NameABI string
Comment string
Safe bool
Arguments []paramTmpl
ReturnType string
ReturnTypeABI string
}

eventTmpl struct {
Name string
NameABI string
Arguments []paramTmpl
}

paramTmpl struct {
Name string
Type string
Name string
Type string
TypeABI string
}

convertParam func(typ smartcontract.ParamType) string
)

func templateFromManifest(cfg *GenerateCfg) (contractTmpl, error) {
ctr := contractTmpl{
ContractName: upperFirst(cfg.Manifest.Name),
ContractName: cleanContractName(cfg.Manifest.Name),
Hash: "0x" + cfg.ContractHash.StringLE(),
}

Expand All @@ -77,6 +88,7 @@ func templateFromManifest(cfg *GenerateCfg) (contractTmpl, error) {
Name: cfg.MethodNameConverter(name),
NameABI: method.Name,
Comment: fmt.Sprintf("invokes `%s` method of contract.", method.Name),
Safe: method.Safe,
}

for i := range method.Parameters {
Expand All @@ -88,16 +100,46 @@ func templateFromManifest(cfg *GenerateCfg) (contractTmpl, error) {
var typeStr = cfg.ParamTypeConverter(method.Parameters[i].Type)

mtd.Arguments = append(mtd.Arguments, paramTmpl{
Name: name,
Type: typeStr,
Name: name,
Type: typeStr,
TypeABI: (smartcontract.ParamType).String(method.Parameters[i].Type),
})
}
mtd.ReturnType = cfg.ParamTypeConverter(method.ReturnType)
mtd.ReturnTypeABI = (smartcontract.ParamType).String(method.ReturnType)
ctr.Methods = append(ctr.Methods, mtd)
}

for _, event := range cfg.Manifest.ABI.Events {
name := event.Name

evt := eventTmpl{
Name: name,
}

for i := range event.Parameters {
name := event.Parameters[i].Name
if name == "" {
name = fmt.Sprintf("arg%d", i)
}

var typeStr = cfg.ParamTypeConverter(event.Parameters[i].Type)

evt.Arguments = append(evt.Arguments, paramTmpl{
Name: name,
Type: typeStr,
})
}
ctr.Events = append(ctr.Events, evt)
}

return ctr, nil
}

func upperFirst(s string) string {
return strings.ToUpper(s[0:1]) + s[1:]
}

func cleanContractName(s string) string {
return upperFirst(regexp.MustCompile(`[\W]+`).ReplaceAllString(s, ""))
}
Loading

0 comments on commit d180fc3

Please sign in to comment.