-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: balaji <rbalajis25@gmail.com>
- Loading branch information
Showing
10 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
|
Oops, something went wrong.