-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorg test. Add import statement expander. Make owner parameter of pr…
…esale contract explicit.
- Loading branch information
Showing
15 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""etherscan.io utilities.""" | ||
|
||
import requests | ||
|
||
from populus import Project | ||
|
||
from ico.importexpand import expand_contract_imports | ||
|
||
|
||
def verify_contract(project: Project, chain_name: str, address: str, contract_name, contract_filename: str, constructor_args: str, libraries: dict, optimization=True, compiler: str="v0.4.8-nightly.2017.1.13+commit.bde0b406"): | ||
"""Make a contract verified on Etherscan. | ||
See the page in action: https://etherscan.io/verifyContract?a=0xcd111aa492a9c77a367c36e6d6af8e6f212e0c8e | ||
""" | ||
|
||
src = expand_contract_imports(project, contract_filename) | ||
|
||
if chain_name == "mainnet": | ||
url = "https://etherscan.io/verifyContract" | ||
elif chain_name == "ropsten": | ||
url = "https://ropsten.etherscan.io/verifyContract" | ||
else: | ||
raise RuntimeError("Unknown chain") | ||
|
||
data = { | ||
"ctl00$ContentPlaceHolder1$txtContractAddress": address, | ||
"ctl00$ContentPlaceHolder1$txtContractName": contract_name, | ||
"ctl00$ContentPlaceHolder1$ddlCompilerVersions": compiler, | ||
"ctl00$ContentPlaceHolder1$ddlOptimization": "1" if optimization else "0", | ||
"ctl00$ContentPlaceHolder1$txtSourceCode": src, | ||
"ctl00$ContentPlaceHolder1$txtConstructorArguements": constructor_args, | ||
} | ||
|
||
idx = 1 | ||
for library_name, library_address in libraries.items(): | ||
data["ctl00$ContentPlaceHolder1$txtLibraryAddress{}".format(idx)] = library_address | ||
data["ctl00$ContentPlaceHolder1$txtLibraryName{}".format(idx)] = library_name | ||
|
||
resp = requests.post(url, data) | ||
resp.raise_for_status() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Expand Solidity import statements for Etherscan verification service. | ||
Mainly need for EtherScan verification service. | ||
""" | ||
import os | ||
from typing import Tuple | ||
|
||
from populus import Project | ||
|
||
|
||
class Expander: | ||
"""Solidity import expanded.""" | ||
|
||
def __init__(self, project: Project): | ||
self.project = project | ||
self.processed_imports = set() | ||
|
||
def expand_file(self, import_path: str): | ||
"""Read Solidity source code and expart any import paths inside. | ||
Supports Populus remapping settings: | ||
http://populus.readthedocs.io/en/latest/config.html#compiler-settings | ||
:param import_path: | ||
""" | ||
|
||
# Already handled | ||
if import_path in self.processed_imports: | ||
return "" | ||
|
||
# TODO: properly handle import remapping here, read them from project config | ||
if import_path.startswith("zeppelin/"): | ||
abs_import_path = os.path.join(os.getcwd(), import_path) | ||
else: | ||
abs_import_path = os.path.join(os.getcwd(), "contracts", import_path) | ||
|
||
abs_import_path = os.path.abspath(abs_import_path) | ||
|
||
with open(abs_import_path, "rt") as inp: | ||
source = inp.read() | ||
self.processed_imports.add(import_path) | ||
return self.process_source(source) | ||
|
||
|
||
def process_source(self, src: str): | ||
"""Process Solidity source code and expand any import statement.""" | ||
|
||
out = [] | ||
|
||
for line in src.split("\n"): | ||
# Detect import statements, ghetto way | ||
if line.startswith('import "'): | ||
prefix, import_path, suffix = line.split('"') | ||
source = self.expand_file(import_path) | ||
out += source.split("\n") | ||
else: | ||
out.append(line) | ||
|
||
return "\n".join(out) | ||
|
||
|
||
def expand_contract_imports(project: Project, contract_filename: str) -> Tuple[str, str]: | ||
"""Expand Solidity import statements. | ||
:return: Tuple[final expanded source, set of processed filenames] | ||
""" | ||
exp = Expander(project) | ||
return exp.expand_file(contract_filename), exp.processed_imports |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Test import expansion tool.""" | ||
|
||
from populus import Project | ||
|
||
from ico.importexpand import expand_contract_imports | ||
|
||
|
||
def test_expand_token(project: Project): | ||
"""Expand import statement in a Solidity source file.""" | ||
|
||
expanded, imported_files = expand_contract_imports(project, "Crowdsale.sol") | ||
|
||
assert imported_files == {'zeppelin/contracts/token/ERC20.sol', './PricingStrategy.sol', './Haltable.sol', './SafeMathLib.sol', 'zeppelin/contracts/ownership/Ownable.sol', 'Crowdsale.sol', './FinalizeAgent.sol'} | ||
|
||
assert "contract Crowdsale" in expanded | ||
assert 'import "' not in expanded | ||
|