Skip to content

Commit

Permalink
op-bindings: make sourcemap optional
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Jun 19, 2023
1 parent 26f490d commit 38307d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions op-bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ more:
-forge-artifacts ../packages/contracts-bedrock/forge-artifacts \
-out ./bindings \
-contracts SystemConfig,OptimismMintableERC20Factory,L2StandardBridge,L1BlockNumber,LegacyMessagePasser,DeployerWhitelist,Proxy,OptimismPortal,L2ToL1MessagePasser,L2CrossDomainMessenger,SequencerFeeVault,L1Block,LegacyERC20ETH,WETH9,GovernanceToken,L1CrossDomainMessenger,L2ERC721Bridge,OptimismMintableERC721Factory,ProxyAdmin,MIPS,Oracle \
-source-maps MIPS,Oracle \
-package bindings

mkdir:
Expand Down
33 changes: 21 additions & 12 deletions op-bindings/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type flags struct {
ArtifactsDir string
ForgeArtifacts string
Contracts string
SourceMaps string
OutDir string
Package string
}
Expand All @@ -43,11 +44,17 @@ func main() {
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")
flag.StringVar(&f.SourceMaps, "source-maps", "", "Comma-separated list of contracts to generate source-maps for")
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{})
for _, k := range sourceMaps {
sourceMapsSet[k] = struct{}{}
}

if len(artifacts) == 0 {
log.Fatalf("must define a list of artifacts")
Expand Down Expand Up @@ -82,18 +89,20 @@ func main() {
}
serStr := strings.Replace(string(ser), "\"", "\\\"", -1)

// 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)
}
deployedSourceMap := ""
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)
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{
Expand Down Expand Up @@ -138,9 +147,9 @@ const {{.Name}}StorageLayoutJSON = "{{.StorageLayout}}"
var {{.Name}}StorageLayout = new(solc.StorageLayout)
var {{.Name}}DeployedBin = "{{.DeployedBin}}"
{{if .DeployedSourceMap}}
var {{.Name}}DeployedSourceMap = "{{.DeployedSourceMap}}"
{{end}}
func init() {
if err := json.Unmarshal([]byte({{.Name}}StorageLayoutJSON), {{.Name}}StorageLayout); err != nil {
panic(err)
Expand Down

0 comments on commit 38307d9

Please sign in to comment.