Skip to content

Commit

Permalink
POC: run all contracts through solc with bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 11, 2022
1 parent 1414dde commit 05e65ae
Show file tree
Hide file tree
Showing 24 changed files with 1,733 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
bazel-out/
3 changes: 3 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

# gazelle:prefix github.com/divergencetech/ethier
gazelle(
Expand Down
49 changes: 49 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,52 @@ go_rules_dependencies()
go_register_toolchains(version = "1.18")

gazelle_dependencies()

############################
# Bazel fetch npm packages #
############################

http_archive(
name = "aspect_rules_js",
sha256 = "db9f446752fe4100320cf8487e8fd476b9af0adf6b99b601bcfd70b289bb0598",
strip_prefix = "rules_js-1.1.2",
url = "https://github.com/aspect-build/rules_js/archive/refs/tags/v1.1.2.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
verify_node_modules_ignored = "//:.bazelignore",
lifecycle_hooks_exclude = [
# This package has a preinstall script which asserts
# if(process.env.npm_execpath.indexOf('yarn') === -1) throw...
"@chainlink/contracts",
],
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

load("//sol:repositories.bzl", "LATEST_VERSION", "rules_sol_dependencies", "sol_register_toolchains")

rules_sol_dependencies()

sol_register_toolchains(
name = "solc",
sol_version = LATEST_VERSION,
)
7 changes: 7 additions & 0 deletions contracts/crypto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "crypto",
srcs = glob(["*.sol"]),
deps = ["//:node_modules/@openzeppelin/contracts"],
)
11 changes: 11 additions & 0 deletions contracts/erc721/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "erc721",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"//:node_modules/@openzeppelin/contracts",
"//:node_modules/erc721a",
],
)
6 changes: 6 additions & 0 deletions contracts/factories/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "factories",
srcs = glob(["*.sol"]),
)
6 changes: 6 additions & 0 deletions contracts/random/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "random",
srcs = glob(["*.sol"]),
)
10 changes: 10 additions & 0 deletions contracts/sales/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "sales",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"//:node_modules/@openzeppelin/contracts",
],
)
10 changes: 10 additions & 0 deletions contracts/thirdparty/chainlink/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "chainlink",
srcs = glob(["*.sol"]),
deps = [
"//:node_modules/@chainlink/contracts",
"//:node_modules/@openzeppelin/contracts",
],
)
10 changes: 10 additions & 0 deletions contracts/thirdparty/opensea/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "opensea",
srcs = glob(["*.sol"]),
deps = [
"//contracts/utils",
"//:node_modules/@openzeppelin/contracts",
],
)
6 changes: 6 additions & 0 deletions contracts/thirdparty/weth/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//sol:defs.bzl", "sol_binary")

sol_binary(
name = "weth",
srcs = glob(["*.sol"]),
)
8 changes: 8 additions & 0 deletions contracts/utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//sol:defs.bzl", "sol_sources")

sol_sources(
name = "utils",
srcs = glob(["*.sol"]),
visibility = ["//visibility:public"],
deps = ["//:node_modules/@openzeppelin/contracts"],
)
46 changes: 46 additions & 0 deletions sol/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# For stardoc to reference the files
exports_files(["defs.bzl"])

# This is the target rule authors should put in their "toolchains"
# attribute in order to get a runtime for the correct platform.
# See https://docs.bazel.build/versions/main/toolchains.html#writing-rules-that-use-toolchains
toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
"//sol/private:toolchains_repo",
"//sol/private:versions",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
name = "defs",
srcs = ["defs.bzl"],
visibility = ["//visibility:public"],
deps = [
"//sol/private:sol_binary",
"//sol/private:sol_sources",
],
)

bzl_library(
name = "providers",
srcs = ["providers.bzl"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "toolchain",
srcs = ["toolchain.bzl"],
visibility = ["//visibility:public"],
)
24 changes: 24 additions & 0 deletions sol/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""# Bazel rules for Solidity
See <https://docs.soliditylang.org>
"""

load("//sol/private:sol_binary.bzl", lib = "sol_binary")
load("//sol/private:sol_sources.bzl", src = "sol_sources")
load(":providers.bzl", "SolSourcesInfo")

sol_binary = rule(
implementation = lib.implementation,
attrs = lib.attrs,
doc = """sol_binary compiles Solidity source files with solc""",
toolchains = lib.toolchains,
)

sol_sources = rule(
implementation = src.implementation,
attrs = src.attrs,
doc = """Collect .sol source files to be imported as library code.
Performs no actions, so semantically equivalent to filegroup().
""",
provides = [SolSourcesInfo],
)
36 changes: 36 additions & 0 deletions sol/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "toolchains_repo",
srcs = ["toolchains_repo.bzl"],
visibility = ["//sol:__subpackages__"],
)

bzl_library(
name = "versions",
srcs = ["versions.bzl"],
visibility = ["//sol:__subpackages__"],
)

bzl_library(
name = "sol_binary",
srcs = ["sol_binary.bzl"],
visibility = ["//sol:__subpackages__"],
deps = [
"//sol:providers",
"@aspect_rules_js//js:libs",
"@aspect_rules_js//js:providers",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "sol_sources",
srcs = ["sol_sources.bzl"],
visibility = ["//sol:__subpackages__"],
deps = [
"//sol:providers",
"@aspect_rules_js//js:libs",
"@aspect_rules_js//js:providers",
],
)
Loading

0 comments on commit 05e65ae

Please sign in to comment.