Пример #1
0
def test_remove_deployment(network):
    network.connect("mainnet")
    address = "0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e"
    Contract.from_explorer(address)
    Contract.remove_deployment(address)

    assert _get_deployment(address) == (None, None)
Пример #2
0
def test_deprecated_init_ethpm(ipfs_mock, network):
    network.connect("ropsten")

    with pytest.warns(DeprecationWarning):
        old = Contract("ComplexNothing", manifest_uri="ipfs://testipfs-complex")

    assert old == Contract.from_ethpm("ComplexNothing", manifest_uri="ipfs://testipfs-complex")
Пример #3
0
def test_autofetch(network, config):
    network.connect("mainnet")
    with pytest.raises(ValueError):
        Contract("0xdAC17F958D2ee523a2206206994597C13D831ec7")

    config.settings["autofetch_sources"] = True
    Contract("0xdAC17F958D2ee523a2206206994597C13D831ec7")
Пример #4
0
def test_retrieve_existing(network):
    network.connect("mainnet")
    with pytest.warns(BrownieCompilerWarning):
        new = Contract.from_explorer("0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2")

    existing = Contract("0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2")
    assert new == existing
Пример #5
0
def test_namespace_collision(tester, build):
    build["abi"].append({
        "constant":
        False,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            },
            {
                "name": "_test",
                "type": "uint256"
            },
        ],
        "name":
        "bytecode",
        "outputs": [{
            "name": "",
            "type": "bool"
        }],
        "payable":
        False,
        "stateMutability":
        "nonpayable",
        "type":
        "function",
    })
    with pytest.raises(AttributeError):
        Contract.from_abi(None, tester.address, build["abi"])
Пример #6
0
def test_from_explorer_deployment_enabled_by_default(network):
    network.connect("mainnet")
    address = "0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e"
    Contract.from_explorer(address)

    assert _get_deployment(address) != (None, None)
    # cleanup
    Contract.remove_deployment(address)
Пример #7
0
def test_abi_deployment_enabled_by_default(network, build):
    network.connect("mainnet")
    address = "0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e"
    Contract.from_abi("abiTester", address, build["abi"])

    assert _get_deployment(address) != (None, None)
    # cleanup
    Contract.remove_deployment(address)
Пример #8
0
def test_existing_different_chains(network):
    network.connect("mainnet")
    Contract.from_explorer("0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2")

    network.disconnect()
    network.connect("ropsten")
    with pytest.raises(ValueError):
        Contract("0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2")
Пример #9
0
def test_remove_deployment_returns(network):
    network.connect("mainnet")
    address = "0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e"
    Contract.from_explorer(address)
    build_json, sources = _get_deployment(address)

    assert (build_json, sources) != (None, None)
    assert (build_json, sources) == (Contract.remove_deployment(address))
 def rule_receive_erc20(
     self,
     st_contract: Contract,
     st_token: int,
     st_amount: int,
 ):
     if st_contract.balanceOf(self.alice) >= st_amount:
         st_contract.approve(self.xhibit, 2**256 - 1, {"from": self.alice})
         self.xhibit.getERC20(self.alice, st_token, st_contract, st_amount)
         self.state.receive_erc20(st_token, str(st_contract), st_amount)
Пример #11
0
 def rule_receive_child(
     self,
     st_contract: Contract,
     token_id: int = "st_token",
     receiving_token: int = "st_token",
 ):
     if st_contract.ownerOf(token_id) == self.alice:
         st_contract.safeTransferFrom(self.alice, self.xhibit, token_id,
                                      receiving_token, {"from": self.alice})
         self.state.receive_child(receiving_token, str(st_contract),
                                  token_id)
Пример #12
0
def test_as_proxy_for(network, original, impl):
    network.connect("mainnet")
    original = Contract.from_explorer(original)
    proxy = Contract.from_explorer(original, as_proxy_for=impl)
    implementation = Contract(impl)

    assert original.abi == proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
Пример #13
0
def test_duplicate_alias(network):
    network.connect("mainnet")
    foo = Contract.from_explorer("0x2af5d2ad76741191d15dfe7bf6ac92d4bd912ca3")
    bar = Contract.from_explorer("0x2157a7894439191e520825fe9399ab8655e0f708")

    foo.set_alias("foo")
    with pytest.raises(ValueError):
        bar.set_alias("foo")

    bar.set_alias("bar")
    foo.set_alias(None)
    bar.set_alias("foo")
Пример #14
0
    def __init__(cls, alice: Account, ERC721: ContractContainer,
                 xhibit: Contract):
        cls.alice = alice
        cls.xhibit = xhibit

        # create 10 mock ERC721 contracts
        for _ in range(10):
            instance = alice.deploy(ERC721)
            # create 10 xhibit NFTs as well
            xhibit.mint(alice, {"from": alice})
            # create 10 NFTs in this contract instance
            for _ in range(10):
                instance._mint_for_testing(alice, {"from": alice})
Пример #15
0
def test_as_proxy_for(network):
    network.connect("mainnet")
    original = Contract.from_explorer("0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b")
    proxy = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b",
        as_proxy_for="0x97BD4Cc841FC999194174cd1803C543247a014fe",
    )
    implementation = Contract("0x97BD4Cc841FC999194174cd1803C543247a014fe")
    assert original.abi != proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
Пример #16
0
def test_alias(network):
    network.connect("mainnet")
    contract = Contract.from_explorer("0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2")

    contract.set_alias("testalias")

    assert contract.alias == "testalias"
    assert Contract("testalias") == contract

    contract.set_alias(None)

    assert contract.alias is None
    with pytest.raises(ValueError):
        Contract("testalias")
Пример #17
0
def test_as_proxy_for(network):
    proxy = "0xAfE212AEDf1c8dCd0E470d73B3B4035D32A75Efd"
    impl = "0x098DCb095318A8B5321c5f2DB3fA04a55A780A20"
    network.connect("ropsten")

    original = Contract.from_explorer(proxy)
    proxy = Contract.from_explorer(proxy, as_proxy_for=impl)
    implementation = Contract(impl)

    assert original.abi == proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
    def __init__(cls, alice: Account, ERC20: ContractContainer,
                 xhibit: Contract):
        cls.alice = alice
        cls.xhibit = xhibit

        # create 10 mock ERC20 contracts
        for i in range(10):
            instance = alice.deploy(ERC20, f"Test Token {i}", f"TST{i}", 18)
            # mint a s#!t ton of tokens :)
            instance._mint_for_testing(alice, 2**256 - 1, {"from": alice})
            # approve ahead of time
            instance.approve(xhibit, 2**256 - 1, {"from": alice})
            # create 10 xhibit NFTs as well
            xhibit.mint(alice, {"from": alice})
Пример #19
0
def test_as_proxy_for(network):
    network.connect("mainnet")
    original = Contract.from_explorer("0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b")
    proxy = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b",
        as_proxy_for="0x7b5e3521a049C8fF88e6349f33044c6Cc33c113c",
    )
    implementation = Contract("0x7b5e3521a049C8fF88e6349f33044c6Cc33c113c")

    assert original.abi == proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
Пример #20
0
def test_autofetch_missing(network, config, mocker):
    # an issue woth pytest-mock prevents spying on Contract.from_explorer,
    # so we watch requests.get which is only called inside Contract.from_explorer
    mocker.spy(requests, "get")

    network.connect("mainnet")
    config.settings["autofetch_sources"] = True

    with pytest.raises(ValueError):
        Contract("0xff031750F29b24e6e5552382F6E0c065830085d2")
    assert requests.get.call_count == 2

    with pytest.raises(ValueError):
        Contract("0xff031750F29b24e6e5552382F6E0c065830085d2")
    assert requests.get.call_count == 2
Пример #21
0
def test_as_proxy_for(network):
    network.connect("mainnet")
    original = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b")
    proxy = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b",
        as_proxy_for="0x9d0a0443ff4bb04391655b8cd205683d9fa75550",
    )
    implementation = Contract("0x9d0a0443ff4bb04391655b8cd205683d9fa75550")

    assert original.abi == proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
Пример #22
0
def test_as_proxy_for(network):
    network.connect("mainnet")
    original = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b")
    proxy = Contract.from_explorer(
        "0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b",
        as_proxy_for="0xAf601CbFF871d0BE62D18F79C31e387c76fa0374",
    )
    implementation = Contract("0xAf601CbFF871d0BE62D18F79C31e387c76fa0374")

    assert original.abi == proxy.abi
    assert original.address == proxy.address

    assert proxy.abi == implementation.abi
    assert proxy.address != implementation.address
Пример #23
0
def test_from_explorer_vyper(network):
    network.connect("mainnet")
    contract = Contract.from_explorer(
        "0x2157a7894439191e520825fe9399ab8655e0f708")

    assert contract._name == "Vyper_contract"
    assert "pcMap" not in contract._build
Пример #24
0
def test_from_explorer(network):
    network.connect("mainnet")
    contract = Contract.from_explorer("0x2af5d2ad76741191d15dfe7bf6ac92d4bd912ca3")

    assert contract._name == "LEO"
    assert "pcMap" in contract._build
    assert len(contract._sources) == 1
Пример #25
0
def test_comparison(testproject, tester):
    del testproject.BrownieTester[0]
    assert tester != 123
    assert tester == str(tester.address)
    assert tester == Contract.from_abi("BrownieTester", tester.address,
                                       tester.abi)
    repr(tester)
Пример #26
0
def test_namespace_collision():
    b = deepcopy(build.get('Token'))
    b['abi'].append({
        'constant':
        False,
        'inputs': [{
            'name': '_to',
            'type': 'address'
        }, {
            'name': '_value',
            'type': 'uint256'
        }, {
            'name': '_test',
            'type': 'uint256'
        }],
        'name':
        'bytecode',
        'outputs': [{
            'name': '',
            'type': 'bool'
        }],
        'payable':
        False,
        'stateMutability':
        'nonpayable',
        'type':
        'function'
    })
    with pytest.raises(AttributeError):
        Contract(str(accounts[1]), b, None)
Пример #27
0
def test_from_explorer_etc(network):
    network.connect("etc")
    with pytest.warns(BrownieCompilerWarning):
        contract = Contract.from_explorer(
            "0x085b0fdf115aa9e16ae1bddd396ce1f993c52220")

    assert contract._name == "ONEX"
Пример #28
0
    def _load_deployments(self) -> None:
        if CONFIG.network_type != "live" and not CONFIG.settings["dev_deployment_artifacts"]:
            return
        chainid = CONFIG.active_network["chainid"] if CONFIG.network_type == "live" else "dev"
        path = self._build_path.joinpath(f"deployments/{chainid}")
        path.mkdir(exist_ok=True)
        deployments = list(path.glob("*.json"))
        deployments.sort(key=lambda k: k.stat().st_mtime)
        deployment_map = self._load_deployment_map()
        for build_json in deployments:
            with build_json.open() as fp:
                build = json.load(fp)

            contract_name = build["contractName"]
            if contract_name not in self._containers:
                build_json.unlink()
                continue
            if "pcMap" in build:
                contract = ProjectContract(self, build, build_json.stem)
            else:
                contract = Contract.from_abi(  # type: ignore
                    contract_name, build_json.stem, build["abi"]
                )
                contract._project = self
            container = self._containers[contract_name]
            _add_contract(contract)
            container._contracts.append(contract)

            # update deployment map for the current chain
            instances = deployment_map.setdefault(chainid, {}).setdefault(contract_name, [])
            if build_json.stem in instances:
                instances.remove(build_json.stem)
            instances.insert(0, build_json.stem)

        self._save_deployment_map(deployment_map)
Пример #29
0
def test_overloaded(testproject, tester, build):
    build["abi"].append(
        {
            "constant": False,
            "inputs": [
                {"name": "_to", "type": "address"},
                {"name": "_value", "type": "uint256"},
                {"name": "_test", "type": "uint256"},
            ],
            "name": "revertStrings",
            "outputs": [{"name": "", "type": "bool"}],
            "payable": False,
            "stateMutability": "nonpayable",
            "type": "function",
        }
    )
    del testproject.BrownieTester[0]
    c = Contract.from_abi(None, tester.address, build["abi"])
    fn = c.revertStrings
    assert type(fn) == OverloadedMethod
    assert len(fn) == 2
    assert type(fn["uint"]) == ContractTx
    assert fn["address", "uint256", "uint256"] == fn["address, uint256, uint256"]
    assert fn["uint"] == fn["uint256"]
    assert fn["uint"] != fn["address, uint256, uint256"]
    repr(fn)
Пример #30
0
def test_from_explorer(network):
    network.connect("mainnet")
    contract = Contract.from_explorer("0x973e52691176d36453868d9d86572788d27041a9")

    assert contract._name == "DxToken"
    assert "pcMap" in contract._build
    assert len(contract._sources) == 1