-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathzksyncwrapper.go
More file actions
152 lines (133 loc) · 3.88 KB
/
zksyncwrapper.go
File metadata and controls
152 lines (133 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package zksyncwrapper
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"go/ast"
"go/format"
"go/token"
"os"
"strings"
)
//go:embed template.go
var zksyncDeployTemplate string
func WrapZksyncDeploy(zksyncBytecodePath, className, pkgName, outPath string) {
fmt.Printf("Generating zk bytecode binding for %s\n", pkgName)
fileNode := &ast.File{
Name: ast.NewIdent(pkgName),
Decls: []ast.Decl{
declareImports(),
declareDeployFunction(className),
declareBytecodeVar(zksyncBytecodePath)}}
writeFile(fileNode, outPath)
}
const comment = `// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
`
var importValues = []string{
`"context"`,
`"crypto/rand"`,
`"fmt"`,
`"github.com/ethereum/go-ethereum/accounts/abi/bind"`,
`"github.com/ethereum/go-ethereum/common"`,
`"github.com/zksync-sdk/zksync2-go/accounts"`,
`"github.com/zksync-sdk/zksync2-go/clients"`,
`"github.com/zksync-sdk/zksync2-go/types"`,
}
func declareImports() ast.Decl {
specs := make([]ast.Spec, len(importValues))
for i, value := range importValues {
specs[i] = &ast.ImportSpec{
Path: &ast.BasicLit{
Kind: token.STRING,
Value: value}}
}
return &ast.GenDecl{
Tok: token.IMPORT,
Specs: specs}
}
func declareDeployFunction(contractName string) ast.Decl {
template := zksyncDeployTemplate
sep := "\n"
lines := strings.Split(template, sep)
from := 0
to := 0
// get the func body as string
for !strings.Contains(lines[to], "return address, receipt, contract, nil") {
if strings.Contains(lines[to], "DeployPlaceholderContractNameZk") {
from = to
}
to++
}
template = strings.Join(lines[from+1:to+1], sep)
template = template[1:] // remove the first space
template = strings.Replace(template, "PlaceholderContractName", contractName, 2)
return &ast.FuncDecl{
Name: ast.NewIdent("Deploy" + contractName + "Zk"),
Type: &ast.FuncType{
Params: &ast.FieldList{
List: []*ast.Field{{
Names: []*ast.Ident{ast.NewIdent("deployOpts")},
Type: &ast.Ident{Name: "*accounts.TransactOpts"}}, {
Names: []*ast.Ident{ast.NewIdent("client")},
Type: &ast.Ident{Name: "*clients.Client"}}, {
Names: []*ast.Ident{ast.NewIdent("wallet")},
Type: &ast.Ident{Name: "*accounts.Wallet"}}, {
Names: []*ast.Ident{ast.NewIdent("backend")},
Type: &ast.Ident{Name: "bind.ContractBackend"}}, {
Names: []*ast.Ident{ast.NewIdent("args")},
Type: &ast.Ellipsis{Elt: &ast.Ident{Name: "interface{}"}}}}},
Results: &ast.FieldList{
List: []*ast.Field{
{Type: &ast.Ident{Name: "common.Address"}},
{Type: &ast.Ident{Name: "*types.Receipt"}},
{Type: &ast.StarExpr{X: &ast.Ident{Name: contractName}}},
{Type: &ast.Ident{Name: "error"}}}}},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
X: &ast.BasicLit{
Kind: token.STRING,
Value: template}}}}}
}
func declareBytecodeVar(srcFile string) ast.Decl {
jsonData, err := os.ReadFile(srcFile)
if err != nil {
panic(err)
}
var bytecodeData struct {
Bytecode struct {
Object string `json:"object"`
} `json:"bytecode"`
}
if err := json.Unmarshal(jsonData, &bytecodeData); err != nil {
panic(err)
}
return &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{ast.NewIdent("ZkBytecode")},
Values: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("common"),
Sel: ast.NewIdent("Hex2Bytes")},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf(`"%s"`, bytecodeData.Bytecode.Object)}}}}}}}
}
func writeFile(fileNode *ast.File, dstFile string) {
var buf bytes.Buffer
fset := token.NewFileSet()
if err := format.Node(&buf, fset, fileNode); err != nil {
panic(err)
}
bs := buf.Bytes()
bs = append([]byte(comment), bs...)
if err := os.WriteFile(dstFile, bs, 0600); err != nil {
panic(err)
}
}