Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions contracts/scripts/native_solc_compile_all_shared
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ compileContract() {
local contract
contract=$(basename "$path")
echo "Compiling" "$contract"
dir=$CONTRACTS_DIR/solc/$PROJECT/$contract

local current_project="$PROJECT"
if [ -n "$project" ]; then
Expand All @@ -31,7 +32,9 @@ compileContract() {
local command
command="forge build $CONTRACTS_DIR/src/v0.8/$current_project/"$path.sol" \
--root $CONTRACTS_DIR \
--extra-output-files bin abi"
--extra-output-files bin abi metadata \
--build-info \
--build-info-path $dir/build"

# Add version if provided
if [ -n "$version" ]; then
Expand All @@ -41,9 +44,12 @@ compileContract() {

# Output directory
command="$command \
-o $CONTRACTS_DIR/solc/$PROJECT/$contract"
-o $dir"

$command

# Move the build info to an expected file name
mv $(find $dir/build -type f -name '*.json' ! -name 'build.json') $dir/build/build.json
}

# Various contracts are compiled with v0.8.19 instead of the default specified in foundry.toml.
Expand Down
75 changes: 74 additions & 1 deletion gethwrappers/abigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gethwrappers

import (
"bytes"
"encoding/json"
"fmt"
"go/ast"
"go/format"
Expand All @@ -11,6 +12,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -28,7 +30,29 @@ var GethVersion = fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.
// AbigenArgs is the arguments to the abigen executable. E.g., Bin is the -bin
// arg.
type AbigenArgs struct {
Bin, ABI, Out, Type, Pkg string
Bin, ABI, BuildInfo, Metadata, Out, BuildInfoOut, Type, Pkg string
}

// compiler defines the compiler section of contract metadata.
type compiler struct {
Version string `json:"version"`
}

// metadata defines the sections of the contract metadata required for verification.
type metadata struct {
Compiler compiler `json:"compiler"`
}

// standardInput defines the sections of the Solidity standard input required for verification.
type standardInput struct {
Version string `json:"version"`
Language string `json:"language"`
Settings map[string]any `json:"settings"`
Sources map[string]any `json:"sources"`
}

type buildInfo struct {
Input standardInput `json:"input"`
}

// Abigen calls Abigen with the given arguments
Expand Down Expand Up @@ -70,6 +94,55 @@ func Abigen(a AbigenArgs) {
}

ImproveAbigenOutput(a.Out, a.ABI)

// Add build info to exported package
info, err := os.ReadFile(a.BuildInfo)
if err != nil {
Exit("Error while reading build info file", err)
}
// Unmarshal into BuildInfo struct to filter out unnecessary fields
// and marshal back to JSON bytes afterwards
var build buildInfo
err = json.Unmarshal(info, &build)
if err != nil {
Exit("Error while unmarshalling build info JSON", err)
}
// Get version from metadata file, as it contains the commit hash required by etherscan
metadataBytes, err := os.ReadFile(a.Metadata)
if err != nil {
Exit("Error while reading metadata file", err)
}
var metadata metadata
err = json.Unmarshal(metadataBytes, &metadata)
if err != nil {
Exit("Error while unmarshalling metadata JSON", err)
}

if !strings.HasPrefix(metadata.Compiler.Version, "v") {
// Verification requires the version to be prefixed with "v"
metadata.Compiler.Version = "v" + metadata.Compiler.Version
}
build.Input.Version = metadata.Compiler.Version

refinedMeta, err := json.Marshal(build.Input)
if err != nil {
Exit("Error while marshalling build info JSON", err)
}
// Export the metadata as a variable in the generated Go file
var buf bytes.Buffer
if err := json.Compact(&buf, refinedMeta); err != nil {
Exit("Error while compacting build info JSON", err)
}
code := fmt.Sprintf(
"%s\npackage %s\n\nvar SolidityStandardInput = %s\n",
headerComment,
a.Pkg,
strconv.Quote(buf.String()),
)
err = os.WriteFile(a.BuildInfoOut, []byte(code), 0600)
if err != nil {
Exit("Error while writing build info file", err)
}
}

func ImproveAbigenOutput(path string, abiPath string) {
Expand Down
12 changes: 10 additions & 2 deletions gethwrappers/generation/generate/genwrapper/genwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// <project>/generated/<pkgName>/<pkgName>.go. The suffix will take place after
// the <project>/generated, so the overridden location would be
// <project>/generated/<outDirSuffixInput>/<pkgName>/<pkgName>.go.
func GenWrapper(abiPath, binPath, className, pkgName, outDirSuffixInput string) {
func GenWrapper(abiPath, binPath, buildInfoPath, metadataPath, className, pkgName, outDirSuffixInput string) {
fmt.Println("Generating", pkgName, "contract wrapper")

cwd, err := os.Getwd() // gethwrappers directory
Expand All @@ -38,9 +38,17 @@ func GenWrapper(abiPath, binPath, className, pkgName, outDirSuffixInput string)
mkdErr)
}
outPath := filepath.Join(outDir, pkgName+".go")
metadataOutPath := filepath.Join(outDir, pkgName+"_metadata.go")

gethwrappers.Abigen(gethwrappers.AbigenArgs{
Bin: binPath, ABI: abiPath, Out: outPath, Type: className, Pkg: pkgName,
Bin: binPath,
ABI: abiPath,
BuildInfo: buildInfoPath,
Metadata: metadataPath,
Out: outPath,
BuildInfoOut: metadataOutPath,
Type: className,
Pkg: pkgName,
})

// Build succeeded, so update the versions db with the new contract data
Expand Down
2 changes: 1 addition & 1 deletion gethwrappers/generation/generate/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func main() {
outDirSuffix = os.Args[5]
}

genwrapper.GenWrapper(abiPath, binPath, className, pkgName, outDirSuffix)
genwrapper.GenWrapper(abiPath, binPath, "", "", className, pkgName, outDirSuffix)
}
4 changes: 3 additions & 1 deletion gethwrappers/generation/generate_automation/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func main() {
pkgName := os.Args[3]

abiPath := rootDir + project + "/" + inputClassName + "/" + inputClassName + ".sol/" + inputClassName + ".abi.json"
metadataPath := rootDir + project + "/" + inputClassName + "/" + inputClassName + ".sol/" + inputClassName + ".metadata.json"
binPath := rootDir + project + "/" + inputClassName + "/" + inputClassName + ".sol/" + inputClassName + ".bin"
buildInfoPath := rootDir + project + "/" + inputClassName + "/build/build.json"

genwrapper.GenWrapper(abiPath, binPath, outputClassName, pkgName, "")
genwrapper.GenWrapper(abiPath, binPath, buildInfoPath, metadataPath, outputClassName, pkgName, "")
}
4 changes: 3 additions & 1 deletion gethwrappers/generation/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func main() {
}

abiPath := rootDir + project + "/" + className + "/" + className + ".sol/" + className + ".abi.json"
metadataPath := rootDir + project + "/" + className + "/" + className + ".sol/" + className + ".metadata.json"
binPath := rootDir + project + "/" + className + "/" + className + ".sol/" + className + ".bin"
buildInfoPath := rootDir + project + "/" + className + "/build/build.json"

genwrapper.GenWrapper(abiPath, binPath, className, pkgName, outDirSuffix)
genwrapper.GenWrapper(abiPath, binPath, buildInfoPath, metadataPath, className, pkgName, outDirSuffix)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading
Loading