Skip to content

Commit

Permalink
poonao template
Browse files Browse the repository at this point in the history
Signed-off-by: balaji <rbalajis25@gmail.com>
  • Loading branch information
poonai committed Feb 14, 2024
1 parent 3f76064 commit a26ad99
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ethkit/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
31 changes: 31 additions & 0 deletions cmd/ethkit/template/rpc.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{define "rpc"}}
package {{.Package}}
import(
"context"
"math/big"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
"github.com/0xsequence/go-sequence/lib/prototyp"
proto "{{.RpcPackage}}"
)

{{ template "WebRpcSchema" .}}

type Rpc struct {
contractABI abi.ABI
}

func NewRpc() *Rpc {
abi, err := abi.JSON(strings.NewReader(OrderbookMetaData.ABI))
if err != nil {
panic(err)
}
return &Rpc{contractABI:abi}
}


{{range $methodName,$method := .Methods}}
{{- template "RpcMethodExapansion" dict "MethodName" $methodName "Method" $method}}
{{end}}
{{end}}
17 changes: 17 additions & 0 deletions cmd/ethkit/template/rpc_method_expansion.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{define "RpcMethodExapansion"}}
func(r *Rpc) {{firstLetterUpper .MethodName}}(ctx context.Context, {{- range $inputIndex, $input := .Method.Inputs}} {{ $input.Name}} {{argType $input.Type}}{{if len $.Method.Inputs | needArgSeperator $inputIndex}},{{end}}{{end}}) (string, error) {

{{- range $inputIndex, $input := .Method.Inputs}}

{{- template "TypeExapansion" dict "Input" $input "ParamIndex" $inputIndex}}

{{- template "TypeValueExapansion" dict "Input" $input "ParamIndex" $inputIndex}}

{{- end }}
buf, err := r.contractABI.Methods["{{.MethodName}}"].Inputs.Pack({{- range $inputIndex, $input := .Method.Inputs }}param{{- $inputIndex}} {{- if len $.Method.Inputs | needArgSeperator $inputIndex}},{{end}} {{- end}})
if err != nil {
return "", err
}
return hexutil.Encode(buf), nil
}
{{- end}}
21 changes: 21 additions & 0 deletions cmd/ethkit/template/type_expansion.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{define "TypeExapansion"}}
{{if hasStruct .Input.Type}}
param{{.ParamIndex}}:={{if isSlice .Input.Type }}[]{{end}}struct{
{{- $elem := $.Input.Type -}}
{{- if isSlice .Input.Type }} {{$elem = $.Input.Type.Elem}} {{end }}
{{range $index, $tupleElem := $elem.TupleElems}}
{{- index $elem.TupleRawNames $index | firstLetterUpper}} {{goType $tupleElem.String}}
{{end -}}
}{}
{{else}}
{{- if eq .Input.Type.String "uint256"}}
param{{.ParamIndex}} := big.NewInt(0)
{{- else if eq .Input.Type.String "address"}}
param{{.ParamIndex}} := common.Address{}
{{- else if eq .Input.Type.String "address[]"}}
param{{.ParamIndex}} := []common.Address{}
{{- else if eq .Input.Type.String "uint256[]"}}
param{{.ParamIndex}} := []*big.Int{}
{{end}}
{{- end}}
{{- end}}
57 changes: 57 additions & 0 deletions cmd/ethkit/template/type_value_explansion.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{- define "TypeValueExapansion"}}

{{- if hasStruct .Input.Type}}
{{if isSlice .Input.Type}}
{{$elem := .Input.Type.Elem}}
for _, tmp := range {{.Input.Name}} {
param{{.ParamIndex}} = append(param{{.ParamIndex}},struct {
{{ range $index, $tupleElem := $elem.TupleElems}}
{{- index $elem.TupleRawNames $index | firstLetterUpper}} {{goType $tupleElem.String }}
{{end -}}
}{
{{ range $index, $tupleElem := $elem.TupleElems}}
{{- index $elem.TupleRawNames $index | firstLetterUpper}}: tmp.{{index $elem.TupleRawNames $index | firstLetterUpper}}{{structTypeConversion $tupleElem.String}},
{{end -}}
})
}
{{else}}
param{{.ParamIndex}} = struct {
{{ range $index, $tupleElem := $.Input.Type.TupleElems}}
{{- index $.Input.Type.TupleRawNames $index | firstLetterUpper}} {{goType $tupleElem.String }}
{{end -}}
}{
{{ range $index, $tupleElem := $.Input.Type.TupleElems}}
{{- index $.Input.Type.TupleRawNames $index | firstLetterUpper}}: {{$.Input.Name}}.{{index $.Input.Type.TupleRawNames $index | firstLetterUpper}}{{structTypeConversion $tupleElem.String}},
{{end -}}
}
{{end}}

{{else if eq .Input.Type.String "uint256"}}
_, ok{{.ParamIndex}} := param{{.ParamIndex}}.SetString({{.Input.Name}}, 10)
if !ok{{.ParamIndex}} {
return "", proto.Errorf(proto.ErrInvalidArgument,"exptected int type string but got %s", {{.Input.Name}})
}
{{- else if eq .Input.Type.String "address"}}

param{{.ParamIndex}} = common.HexToAddress({{.Input.Name}})

{{- else if eq .Input.Type.String "uint256[]"}}

for _, tmp := range {{.Input.Name}} {
bigTmp := big.NewInt(0)
_, ok{{.ParamIndex}} := bigTmp.SetString(tmp,10)
if !ok{{.ParamIndex}} {
return "", proto.Errorf(proto.ErrInvalidArgument,"exptected int type string but got %v", {{.Input.Name}})
}
param{{.ParamIndex}} = append(param{{.ParamIndex}}, bigTmp)
}

{{- else if eq .Input.Type.String "address[]"}}

for _, tmp := range {{.Input.Name}} {
addressTmp := common.HexToAddress(tmp)
param{{.ParamIndex}} = append(param{{.ParamIndex}}, addressTmp)
}

{{- end}}
{{end}}
29 changes: 29 additions & 0 deletions cmd/ethkit/template/webrpc_schema.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{define "WebRpcSchema"}}
/**
webrpc schema

{{range $struct := .Structs}}
struct {{$struct.TupleRawName}}
{{- range $index, $elem := $struct.TupleElems}}
{{- if eq $elem.String "bool"}}
- {{index $struct.TupleRawNames $index}}: bool
{{- else if eq $elem.String "address"}}
- {{index $struct.TupleRawNames $index}}: string
+ go.field.type = prototyp.Hash
{{- else if eq $elem.String "uint256"}}
- {{index $struct.TupleRawNames $index}}: string
+ go.field.type = prototyp.BigInt
{{- else if eq $elem.String "uint96"}}
- {{index $struct.TupleRawNames $index}}: string
+ go.field.type = prototyp.BigInt
{{- end}}
{{- end}}
{{end}}

service {{firstLetterUpper .Package}}
{{range $methodName, $method := .Methods}}
- {{firstLetterUpper $methodName}}({{- range $inputIndex, $input := $method.Inputs}} {{ $input.Name}}: {{webrpcArgType $input.Type}}{{if len $method.Inputs | needArgSeperator $inputIndex}},{{end}}{{end}}) => (output: string)
{{end}}
**/
{{end}}

Loading

0 comments on commit a26ad99

Please sign in to comment.