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 ( ...@@ -12,12 +12,10 @@ import (
"text/template" "text/template"
"github.com/ethereum-optimism/optimism/op-bindings/ast" "github.com/ethereum-optimism/optimism/op-bindings/ast"
"github.com/ethereum-optimism/optimism/op-bindings/solc" "github.com/ethereum-optimism/optimism/op-bindings/foundry"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
type flags struct { type flags struct {
ArtifactsDir string
ForgeArtifacts string ForgeArtifacts string
Contracts string Contracts string
SourceMaps string SourceMaps string
...@@ -33,17 +31,8 @@ type data struct { ...@@ -33,17 +31,8 @@ type data struct {
DeployedSourceMap string 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() { func main() {
var f flags 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.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.OutDir, "out", "", "Output directory to put code in")
flag.StringVar(&f.Contracts, "contracts", "", "Comma-separated list of contracts to generate code for") flag.StringVar(&f.Contracts, "contracts", "", "Comma-separated list of contracts to generate code for")
...@@ -51,7 +40,6 @@ func main() { ...@@ -51,7 +40,6 @@ func main() {
flag.StringVar(&f.Package, "package", "artifacts", "Go package name") flag.StringVar(&f.Package, "package", "artifacts", "Go package name")
flag.Parse() flag.Parse()
artifacts := strings.Split(f.ArtifactsDir, ",")
contracts := strings.Split(f.Contracts, ",") contracts := strings.Split(f.Contracts, ",")
sourceMaps := strings.Split(f.SourceMaps, ",") sourceMaps := strings.Split(f.SourceMaps, ",")
sourceMapsSet := make(map[string]struct{}) sourceMapsSet := make(map[string]struct{})
...@@ -59,10 +47,6 @@ func main() { ...@@ -59,10 +47,6 @@ func main() {
sourceMapsSet[k] = struct{}{} sourceMapsSet[k] = struct{}{}
} }
if len(artifacts) == 0 {
log.Fatalf("must define a list of artifacts")
}
if len(contracts) == 0 { if len(contracts) == 0 {
log.Fatalf("must define a list of contracts") log.Fatalf("must define a list of contracts")
} }
...@@ -75,7 +59,7 @@ func main() { ...@@ -75,7 +59,7 @@ func main() {
log.Printf("cannot find forge-artifact with source-map data of %q\n", name) 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 { if err := json.Unmarshal(forgeArtifactData, &artifact); err != nil {
log.Fatalf("failed to parse forge artifact of %q: %v\n", name, err) log.Fatalf("failed to parse forge artifact of %q: %v\n", name, err)
} }
...@@ -84,11 +68,7 @@ func main() { ...@@ -84,11 +68,7 @@ func main() {
} }
storage := artifact.StorageLayout storage := artifact.StorageLayout
if storage == nil { canonicalStorage := ast.CanonicalizeASTIDs(&storage)
log.Fatalf("no storage layout for %s\n", name)
}
canonicalStorage := ast.CanonicalizeASTIDs(storage)
ser, err := json.Marshal(canonicalStorage) ser, err := json.Marshal(canonicalStorage)
if err != nil { if err != nil {
log.Fatalf("error marshaling storage: %v\n", err) log.Fatalf("error marshaling storage: %v\n", err)
...@@ -97,19 +77,8 @@ func main() { ...@@ -97,19 +77,8 @@ func main() {
deployedSourceMap := "" deployedSourceMap := ""
if _, ok := sourceMapsSet[name]; ok { 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{ d := data{
Name: name, Name: name,
......
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