Skip to content

Commit

Permalink
op-bindings: make gen fast
Browse files Browse the repository at this point in the history
  • Loading branch information
tynes committed Jun 26, 2023
1 parent 2bb7866 commit 9b16d36
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 193 deletions.
138 changes: 2 additions & 136 deletions op-bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,6 @@ pkg := bindings

all: version mkdir bindings more

bindings: l1block-bindings \
l1-blocknumber-bindings \
system-config-bindings \
l1-cross-domain-messenger-bindings \
l1-standard-bridge-bindings \
l2-to-l1-message-passer-bindings \
optimism-portal-bindings \
l2-output-oracle-bindings \
gas-price-oracle-bindings \
legacy-message-passer-bindings \
address-manager-bindings \
l2-cross-domain-messenger-bindings \
l2-standard-bridge-bindings \
sequencer-fee-vault-bindings \
optimism-mintable-erc20-factory-bindings \
optimism-mintable-erc20-bindings \
proxy-bindings \
proxy-admin-bindings \
erc20-bindings \
weth9-bindings \
deployer-whitelist-bindings \
l2-erc721-bridge-bindings \
l1-erc721-bridge-bindings \
optimism-mintable-erc721-factory-bindings \
l1-fee-vault-bindings \
basefee-vault-bindings \
legacy-erc20-eth-bindings \
dispute-game-factory-bindings \
fault-dispute-game-bindings \
standard-bridge-bindings \
cross-domain-messenger-bindings \
cannon-mips \
cannon-preimage-oracle

version:
forge --version
abigen --version
Expand All @@ -47,111 +13,11 @@ compile:
yarn clean && \
yarn build

system-config-bindings: compile
./gen_bindings.sh contracts/L1/SystemConfig.sol:SystemConfig $(pkg)

l1-cross-domain-messenger-bindings: compile
./gen_bindings.sh contracts/L1/L1CrossDomainMessenger.sol:L1CrossDomainMessenger $(pkg)

l1-standard-bridge-bindings: compile
./gen_bindings.sh contracts/L1/L1StandardBridge.sol:L1StandardBridge $(pkg)

optimism-portal-bindings: compile
./gen_bindings.sh contracts/L1/OptimismPortal.sol:OptimismPortal $(pkg)

l2-output-oracle-bindings: compile
./gen_bindings.sh contracts/L1/L2OutputOracle.sol:L2OutputOracle $(pkg)

address-manager-bindings: compile
./gen_bindings.sh contracts/legacy/AddressManager.sol:AddressManager $(pkg)

l1block-bindings: compile
./gen_bindings.sh contracts/L2/L1Block.sol:L1Block $(pkg)

l2-to-l1-message-passer-bindings: compile
./gen_bindings.sh contracts/L2/L2ToL1MessagePasser.sol:L2ToL1MessagePasser $(pkg)

gas-price-oracle-bindings: compile
./gen_bindings.sh contracts/L2/GasPriceOracle.sol:GasPriceOracle $(pkg)

l2-cross-domain-messenger-bindings: compile
./gen_bindings.sh contracts/L2/L2CrossDomainMessenger.sol:L2CrossDomainMessenger $(pkg)

l2-standard-bridge-bindings: compile
./gen_bindings.sh contracts/L2/L2StandardBridge.sol:L2StandardBridge $(pkg)

l2-erc721-bridge-bindings:
./gen_bindings.sh contracts/L2/L2ERC721Bridge.sol:L2ERC721Bridge $(pkg)

l1-erc721-bridge-bindings:
./gen_bindings.sh contracts/L1/L1ERC721Bridge.sol:L1ERC721Bridge $(pkg)

optimism-mintable-erc721-factory-bindings:
./gen_bindings.sh contracts/universal/OptimismMintableERC721Factory.sol:OptimismMintableERC721Factory $(pkg)

sequencer-fee-vault-bindings: compile
./gen_bindings.sh contracts/L2/SequencerFeeVault.sol:SequencerFeeVault $(pkg)

basefee-vault-bindings: compile
./gen_bindings.sh contracts/L2/BaseFeeVault.sol:BaseFeeVault $(pkg)

l1-fee-vault-bindings: compile
./gen_bindings.sh contracts/L2/L1FeeVault.sol:L1FeeVault $(pkg)

optimism-mintable-erc20-factory-bindings: compile
./gen_bindings.sh contracts/universal/OptimismMintableERC20Factory.sol:OptimismMintableERC20Factory $(pkg)

optimism-mintable-erc20-bindings: compile
./gen_bindings.sh contracts/universal/OptimismMintableERC20.sol:OptimismMintableERC20 $(pkg)

legacy-erc20-eth-bindings: compile
./gen_bindings.sh contracts/legacy/LegacyERC20ETH.sol:LegacyERC20ETH $(pkg)

proxy-bindings: compile
./gen_bindings.sh contracts/universal/Proxy.sol:Proxy $(pkg)

proxy-admin-bindings: compile
./gen_bindings.sh contracts/universal/ProxyAdmin.sol:ProxyAdmin $(pkg)

legacy-message-passer-bindings: compile
./gen_bindings.sh contracts/legacy/LegacyMessagePasser.sol:LegacyMessagePasser $(pkg)

erc20-bindings: compile
./gen_bindings.sh node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20 $(pkg)

weth9-bindings: compile
./gen_bindings.sh contracts/vendor/WETH9.sol:WETH9 $(pkg)

deployer-whitelist-bindings: compile
./gen_bindings.sh contracts/legacy/DeployerWhitelist.sol:DeployerWhitelist $(pkg)

l1-blocknumber-bindings: compile
./gen_bindings.sh contracts/legacy/L1BlockNumber.sol:L1BlockNumber $(pkg)

dispute-game-factory-bindings: compile
./gen_bindings.sh contracts/dispute/DisputeGameFactory.sol:DisputeGameFactory $(pkg)

fault-dispute-game-bindings: compile
./gen_bindings.sh contracts/dispute/FaultDisputeGame.sol:FaultDisputeGame $(pkg)

standard-bridge-bindings: compile
./gen_bindings.sh contracts/universal/StandardBridge.sol:StandardBridge $(pkg)

cross-domain-messenger-bindings: compile
./gen_bindings.sh contracts/universal/CrossDomainMessenger.sol:CrossDomainMessenger $(pkg)

cannon-mips: compile
./gen_bindings.sh contracts/cannon/MIPS.sol:MIPS $(pkg)

cannon-preimage-oracle: compile
./gen_bindings.sh contracts/cannon/PreimageOracle.sol:PreimageOracle $(pkg)


more:
bindings: compile
go run ./gen/main.go \
-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,PreimageOracle \
-contracts ./artifacts.json \
-source-maps MIPS,PreimageOracle \
-package bindings

Expand Down
35 changes: 35 additions & 0 deletions op-bindings/artifacts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
"SystemConfig",
"L1CrossDomainMessenger",
"L1StandardBridge",
"OptimismPortal",
"L2OutputOracle",
"AddressManager",
"L1Block",
"L2ToL1MessagePasser",
"GasPriceOracle",
"L2CrossDomainMessenger",
"L2StandardBridge",
"L2ERC721Bridge",
"L1ERC721Bridge",
"OptimismMintableERC721Factory",
"SequencerFeeVault",
"BaseFeeVault",
"L1FeeVault",
"OptimismMintableERC20Factory",
"OptimismMintableERC20",
"LegacyERC20ETH",
"Proxy",
"ProxyAdmin",
"LegacyMessagePasser",
"ERC20",
"WETH9",
"DeployerWhitelist",
"L1BlockNumber",
"DisputeGameFactory",
"FaultDisputeGame",
"StandardBridge",
"CrossDomainMessenger",
"MIPS",
"PreimageOracle"
]
8 changes: 5 additions & 3 deletions op-bindings/foundry/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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"
)

// Artifact represents a foundry compilation artifact.
// The Abi is specifically left as a json.RawMessage because
// round trip marshaling/unmarshaling of the abi.ABI type
// causes issues.
type Artifact struct {
Abi abi.ABI `json:"abi"`
Abi json.RawMessage `json:"abi"`
StorageLayout solc.StorageLayout `json:"storageLayout"`
DeployedBytecode DeployedBytecode `json:"deployedBytecode"`
Bytecode Bytecode `json:"bytecode"`
Expand Down
59 changes: 56 additions & 3 deletions op-bindings/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -35,12 +36,20 @@ func main() {
var f flags
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.Contracts, "contracts", "artifacts.json", "Path to file containing list of contracts to generate bindings 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()

contracts := strings.Split(f.Contracts, ",")
contractData, err := os.ReadFile(f.Contracts)
if err != nil {
log.Fatal("error reading contract list: %w\n", err)
}
contracts := []string{}
if err := json.Unmarshal(contractData, &contracts); err != nil {
log.Fatal("error parsing contract list: %w\n", err)
}

sourceMaps := strings.Split(f.SourceMaps, ",")
sourceMapsSet := make(map[string]struct{})
for _, k := range sourceMaps {
Expand All @@ -53,10 +62,22 @@ func main() {

t := template.Must(template.New("artifact").Parse(tmpl))

// Make a temp dir to hold all the inputs for abigen
dir, err := os.MkdirTemp("", "op-bindings")
if err != nil {
log.Fatal(err)
}
log.Printf("Using package %s\n", f.Package)

defer os.RemoveAll(dir)
log.Printf("created temp dir %s\n", dir)

for _, name := range contracts {
log.Printf("generating code for %s\n", name)

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)
log.Fatalf("cannot find forge-artifact of %q\n", name)
}

var artifact foundry.Artifact
Expand All @@ -67,6 +88,38 @@ func main() {
log.Fatalf("error reading storage layout %s: %v\n", name, err)
}

rawAbi := artifact.Abi
if err != nil {
log.Fatalf("error marshaling abi: %v\n", err)
}
abiFile := path.Join(dir, name+".abi")
if err := os.WriteFile(abiFile, rawAbi, 0o600); err != nil {
log.Fatalf("error writing file: %v\n", err)
}
rawBytecode := artifact.Bytecode.Object.String()
if err != nil {
log.Fatalf("error marshaling bytecode: %v\n", err)
}
bytecodeFile := path.Join(dir, name+".bin")
if err := os.WriteFile(bytecodeFile, []byte(rawBytecode), 0o600); err != nil {
log.Fatalf("error writing file: %v\n", err)
}

cwd, err := os.Getwd()
if err != nil {
log.Fatalf("error getting cwd: %v\n", err)
}

lowerName := strings.ToLower(name)
outFile := path.Join(cwd, f.Package, lowerName+".go")

cmd := exec.Command("abigen", "--abi", abiFile, "--bin", bytecodeFile, "--pkg", f.Package, "--type", name, "--out", outFile)
cmd.Stdout = os.Stdout

if err := cmd.Run(); err != nil {
log.Fatalf("error running abigen: %v\n", err)
}

storage := artifact.StorageLayout
canonicalStorage := ast.CanonicalizeASTIDs(&storage)
ser, err := json.Marshal(canonicalStorage)
Expand Down
51 changes: 0 additions & 51 deletions op-bindings/gen_bindings.sh

This file was deleted.

0 comments on commit 9b16d36

Please sign in to comment.