def test_expand_offset(btsource, BrownieTester): minified, _ = sources.minify(btsource) expanded = BrownieTester._project._sources.expand_offset( "BrownieTester", (minified.index("contract"), minified.index("contract") + 7)) assert btsource.index( "contract"), btsource.index("contract") + 7 == expanded
def test_minify_vyper(): code = "@public\ndef foo(a: address) -> bool: return True\n\n" foo = compile_source(code)["Vyper"] minified, _ = sources.minify(code, "Vyper") minifoo = compile_source(minified)["Vyper"] assert foo._build["bytecodeSha1"] == minifoo._build["bytecodeSha1"] assert foo._build["source"] != minifoo._build["source"]
def test_expand_offset(): source = sources.get('BrownieTester') minified, _ = sources.minify(source) expanded = sources.expand_offset( "BrownieTester", (minified.index("contract"), minified.index("contract") + 7)) assert source.index("contract"), source.index("contract") + 7 == expanded
def test_minify_compiles(): source = _solc_5_source() build_json = sources.compile_source(source)['TempTester'] minified, _ = sources.minify(source) minified_build = sources.compile_source(minified)['TempTester'] assert build_json['bytecodeSha1'] == minified_build['bytecodeSha1'] assert build_json['source'] != minified_build['source']
def _sources_dict(original: Dict, minify: bool, language: str) -> Dict: result: Dict = {} for key, value in original.items(): if Path(key).suffix == ".json": if isinstance(value, str): value = json.loads(value) result[key] = {"abi": value} else: result[key] = { "content": sources.minify(value, language)[0] if minify else value } return result
def generate_input_json( contract_sources: Dict[str, str], optimize: bool = True, runs: int = 200, evm_version: Union[int, str, None] = None, minify: bool = False, language: str = "Solidity", ) -> Dict: """Formats contracts to the standard solc input json. Args: contract_sources: a dictionary in the form of {path: 'source code'} optimize: enable solc optimizer runs: optimizer runs evm_version: evm version to compile for minify: should source code be minified? language: source language (Solidity or Vyper) Returns: dict """ if language not in ("Solidity", "Vyper"): raise UnsupportedLanguage(f"{language}") if evm_version is None: if language == "Solidity": evm_version = next(i[0] for i in EVM_SOLC_VERSIONS if solidity.get_version() >= i[1]) else: evm_version = "byzantium" input_json: Dict = deepcopy(STANDARD_JSON) input_json["language"] = language input_json["settings"]["evmVersion"] = evm_version if language == "Solidity": input_json["settings"]["optimizer"] = { "enabled": optimize, "runs": runs if optimize else 0 } input_json["sources"] = dict( (k, { "content": sources.minify(v, language)[0] if minify else v }) for k, v in contract_sources.items()) return input_json
def test_minify(btsource): minified, offsets = sources.minify(btsource) assert minified != btsource assert "contract BrownieTester{" in minified assert "contract ExternalCallTester{" in minified
def test_minify_compiles(solc5source): foo = compile_source(solc5source, solc_version="0.5.7")['Foo'] minified, _ = sources.minify(solc5source) minifoo = compile_source(minified, solc_version="0.5.7")['Foo'] assert foo._build['bytecodeSha1'] == minifoo._build['bytecodeSha1'] assert foo._build['source'] != minifoo._build['source']
def test_minify_solc(version): foo = compile_source(MINIFY_SOURCE, solc_version=version)["Foo"] minified, _ = sources.minify(MINIFY_SOURCE) minifoo = compile_source(minified, solc_version=version)["Foo"] assert foo._build["bytecodeSha1"] == minifoo._build["bytecodeSha1"] assert foo._build["source"] != minifoo._build["source"]
def test_minify_json(): foo_list = [{"foo": "bar"}, {"baz": "potato"}] foo = json.dumps(foo_list) minifoo, _ = sources.minify(foo, "json") assert foo != minifoo assert json.loads(minifoo) == foo_list
def test_minify_compiles(solc5source): foo = compile_source(solc5source, solc_version="0.5.7")["Foo"] minified, _ = sources.minify(solc5source) minifoo = compile_source(minified, solc_version="0.5.7")["Foo"] assert foo._build["bytecodeSha1"] == minifoo._build["bytecodeSha1"] assert foo._build["source"] != minifoo._build["source"]
def test_minify(): source = sources.get('BrownieTester') minified, offsets = sources.minify(source) assert minified != source assert "contract BrownieTester{" in minified assert "library UnlinkedLib{" in minified