Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix etherscan json #637

Merged
merged 2 commits into from
Jun 19, 2020
Merged
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
39 changes: 27 additions & 12 deletions brownie/network/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
UndeployedLibrary,
VirtualMachineError,
)
from brownie.project import ethpm
from brownie.project.compiler import compile_and_format
from brownie.project import compiler, ethpm
from brownie.typing import AccountsType, TransactionReceiptType
from brownie.utils import color

Expand Down Expand Up @@ -672,32 +671,48 @@ def from_explorer(
)
return cls.from_abi(name, address, abi, owner)

sources = {f"{name}-flattened.sol": data["result"][0]["SourceCode"]}
optimizer = {
"enabled": bool(int(data["result"][0]["OptimizationUsed"])),
"runs": int(data["result"][0]["Runs"]),
}

evm_version = data["result"][0].get("EVMVersion", "Default")
if evm_version == "Default":
evm_version = None

build = compile_and_format(
sources, solc_version=str(version), optimizer=optimizer, evm_version=evm_version
)
build = build[name]
if data["result"][0]["SourceCode"].startswith("{"):
# source was verified using compiler standard JSON
input_json = json.loads(data["result"][0]["SourceCode"][1:-1])
sources = {k: v["content"] for k, v in input_json["sources"].items()}
evm_version = input_json["settings"].get("evmVersion", evm_version)

compiler.set_solc_version(str(version))
input_json.update(
compiler.generate_input_json(sources, optimizer=optimizer, evm_version=evm_version)
)
output_json = compiler.compile_from_input_json(input_json)
build_json = compiler.generate_build_json(input_json, output_json)
else:
# source was submitted as a single flattened file
sources = {f"{name}-flattened.sol": data["result"][0]["SourceCode"]}
build_json = compiler.compile_and_format(
sources, solc_version=str(version), optimizer=optimizer, evm_version=evm_version
)

build_json = build_json[name]
if as_proxy_for is not None:
build.update(abi=abi, natspec=implementation_contract._build.get("natspec"))
build_json.update(abi=abi, natspec=implementation_contract._build.get("natspec"))

if not _verify_deployed_code(address, build["deployedBytecode"], build["language"]):
if not _verify_deployed_code(
address, build_json["deployedBytecode"], build_json["language"]
):
warnings.warn(
f"{address}: Locally compiled and on-chain bytecode do not match!",
BrownieCompilerWarning,
)
del build["pcMap"]
del build_json["pcMap"]

self = cls.__new__(cls)
_ContractBase.__init__(self, None, build, sources) # type: ignore
_ContractBase.__init__(self, None, build_json, sources) # type: ignore
_DeployedContractBase.__init__(self, address, owner)
_add_deployment(self)
return self
Expand Down
4 changes: 2 additions & 2 deletions tests/network/contract/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ def test_as_proxy_for(network):
original = Contract.from_explorer("0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b")
proxy = Contract.from_explorer(
"0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b",
as_proxy_for="0x97BD4Cc841FC999194174cd1803C543247a014fe",
as_proxy_for="0x9d0a0443ff4bb04391655b8cd205683d9fa75550",
)
implementation = Contract("0x97BD4Cc841FC999194174cd1803C543247a014fe")
implementation = Contract("0x9d0a0443ff4bb04391655b8cd205683d9fa75550")

assert original.abi == proxy.abi
assert original.address == proxy.address
Expand Down