Commit 64add84e authored by Mark Tyneway's avatar Mark Tyneway

op-bindings: cleanup gen script

Does a little refactor to the gen script to dedupe some
code and clean things up.
parent a6cd52cc
package foundry
import (
"encoding/json"
"github.com/ethereum-optimism/optimism/op-bindings/solc"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type Artifact struct {
Abi abi.ABI `json:"abi"`
StorageLayout solc.StorageLayout `json:"storageLayout"`
DeployedBytecode DeployedBytecode `json:"deployedBytecode"`
Bytecode Bytecode `json:"bytecode"`
}
type DeployedBytecode struct {
SourceMap string `json:"sourceMap"`
Object hexutil.Bytes `json:"object"`
LinkReferences json.RawMessage `json:"linkReferences"`
ImmutableReferences json.RawMessage `json:"immutableReferences"`
}
type Bytecode struct {
SourceMap string `json:"sourceMap"`
Object hexutil.Bytes `json:"object"`
LinkReferences json.RawMessage `json:"linkReferences"`
}
......@@ -12,12 +12,10 @@ import (
"text/template"
"github.com/ethereum-optimism/optimism/op-bindings/ast"
"github.com/ethereum-optimism/optimism/op-bindings/solc"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum-optimism/optimism/op-bindings/foundry"
)
type flags struct {
ArtifactsDir string
ForgeArtifacts string
Contracts string
SourceMaps string
......@@ -33,17 +31,8 @@ type data struct {
DeployedSourceMap string
}
type forgeArtifact struct {
StorageLayout *solc.StorageLayout `json:"storageLayout"`
DeployedBytecode struct {
SourceMap string `json:"sourceMap"`
Object hexutil.Bytes `json:"object"`
} `json:"deployedBytecode"`
}
func main() {
var f flags
flag.StringVar(&f.ArtifactsDir, "artifacts", "", "Comma-separated list of directories build info")
flag.StringVar(&f.ForgeArtifacts, "forge-artifacts", "", "Forge artifacts directory, to load sourcemaps from, if available")
flag.StringVar(&f.OutDir, "out", "", "Output directory to put code in")
flag.StringVar(&f.Contracts, "contracts", "", "Comma-separated list of contracts to generate code for")
......@@ -51,7 +40,6 @@ func main() {
flag.StringVar(&f.Package, "package", "artifacts", "Go package name")
flag.Parse()
artifacts := strings.Split(f.ArtifactsDir, ",")
contracts := strings.Split(f.Contracts, ",")
sourceMaps := strings.Split(f.SourceMaps, ",")
sourceMapsSet := make(map[string]struct{})
......@@ -59,10 +47,6 @@ func main() {
sourceMapsSet[k] = struct{}{}
}
if len(artifacts) == 0 {
log.Fatalf("must define a list of artifacts")
}
if len(contracts) == 0 {
log.Fatalf("must define a list of contracts")
}
......@@ -75,7 +59,7 @@ func main() {
log.Printf("cannot find forge-artifact with source-map data of %q\n", name)
}
var artifact forgeArtifact
var artifact foundry.Artifact
if err := json.Unmarshal(forgeArtifactData, &artifact); err != nil {
log.Fatalf("failed to parse forge artifact of %q: %v\n", name, err)
}
......@@ -84,11 +68,7 @@ func main() {
}
storage := artifact.StorageLayout
if storage == nil {
log.Fatalf("no storage layout for %s\n", name)
}
canonicalStorage := ast.CanonicalizeASTIDs(storage)
canonicalStorage := ast.CanonicalizeASTIDs(&storage)
ser, err := json.Marshal(canonicalStorage)
if err != nil {
log.Fatalf("error marshaling storage: %v\n", err)
......@@ -97,18 +77,7 @@ func main() {
deployedSourceMap := ""
if _, ok := sourceMapsSet[name]; ok {
// directory has .sol extension
forgeArtifactData, err := os.ReadFile(path.Join(f.ForgeArtifacts, name+".sol", name+".json"))
if errors.Is(err, os.ErrNotExist) {
log.Printf("cannot find forge-artifact with source-map data of %q\n", name)
}
if err == nil {
var artifact forgeArtifact
if err := json.Unmarshal(forgeArtifactData, &artifact); err != nil {
log.Fatalf("failed to parse forge artifact of %q: %v\n", name, err)
}
deployedSourceMap = artifact.DeployedBytecode.SourceMap
}
deployedSourceMap = artifact.DeployedBytecode.SourceMap
}
d := data{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment