-
Notifications
You must be signed in to change notification settings - Fork 217
/
main.go
155 lines (141 loc) · 3.98 KB
/
main.go
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
153
154
155
// (c) 2019-2020, Ava Labs, Inc.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/ava-labs/subnet-evm/accounts/abi/bind"
"github.com/ava-labs/subnet-evm/internal/flags"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"
)
var (
// Git SHA1 commit hash of the release (set via linker flags)
gitCommit = ""
gitDate = ""
app *cli.App
)
var (
// Flags needed by abigen
abiFlag = &cli.StringFlag{
Name: "abi",
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
}
typeFlag = &cli.StringFlag{
Name: "type",
Usage: "Struct name for the precompile (default = ABI name)",
}
pkgFlag = &cli.StringFlag{
Name: "pkg",
Usage: "Package name to generate the precompile into (default = precompile)",
}
outFlag = &cli.StringFlag{
Name: "out",
Usage: "Output file for the generated precompile (default = STDOUT)",
}
)
func init() {
app = flags.NewApp(gitCommit, gitDate, "subnet-evm precompile generator tool")
app.Name = "precompilegen"
app.Flags = []cli.Flag{
abiFlag,
outFlag,
pkgFlag,
typeFlag,
}
app.Action = precompilegen
}
func precompilegen(c *cli.Context) error {
if !c.IsSet(outFlag.Name) && !c.IsSet(typeFlag.Name) {
utils.Fatalf("type (--type) should be set explicitly for STDOUT ")
}
pkg := pkgFlag.Name
if pkg == "" {
pkg = "precompile"
}
lang := bind.LangGo
// If the entire solidity code was specified, build and bind based on that
var (
abis []string
bins []string
types []string
sigs []map[string]string
libs = make(map[string]string)
aliases = make(map[string]string)
)
if c.String(abiFlag.Name) == "" {
utils.Fatalf("no abi path is specified (--abi)")
}
// Load up the ABI
var (
abi []byte
err error
)
input := c.String(abiFlag.Name)
if input == "-" {
abi, err = io.ReadAll(os.Stdin)
} else {
abi, err = os.ReadFile(input)
}
if err != nil {
utils.Fatalf("Failed to read input ABI: %v", err)
}
abis = append(abis, string(abi))
bins = append(bins, "")
kind := c.String(typeFlag.Name)
if kind == "" {
fn := filepath.Base(input)
kind = strings.TrimSuffix(fn, filepath.Ext(fn))
kind = strings.TrimSpace(kind)
}
types = append(types, kind)
// Generate the contract precompile
code, err := bind.Bind(types, abis, bins, sigs, pkg, lang, libs, aliases, true)
if err != nil {
utils.Fatalf("Failed to generate ABI precompile: %v", err)
}
// Either flush it out to a file or display on the standard output
if !c.IsSet(outFlag.Name) {
fmt.Printf("%s\n", code)
return nil
}
if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0o600); err != nil {
utils.Fatalf("Failed to write ABI precompile: %v", err)
}
fmt.Println("Precompile Generation was a success!")
return nil
}
func main() {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}