Example #1
0
def test_linker_with_callback(escrow_deployer, w3):
    sender = w3.eth.accounts[0]
    recipient = w3.eth.accounts[5]

    def callback_fn(package):
        escrow_instance = package.deployments.get_instance("Escrow")
        tx_hash = escrow_instance.functions.releaseFunds().transact({"from": sender})
        w3.eth.waitForTransactionReceipt(tx_hash)

    escrow_strategy = linker(
        deploy("SafeSendLib", transaction={"from": sender}),
        link("Escrow", "SafeSendLib"),
        deploy(
            "Escrow",
            recipient,
            transaction={"from": sender, "value": w3.toWei("1", "ether")},
        ),
        run_python(callback_fn),
    )
    escrow_deployer.register_strategy("Escrow", escrow_strategy)
    assert w3.eth.getBalance(recipient) == w3.toWei("1000000", "ether")
    linked_escrow_package = escrow_deployer.deploy("Escrow")
    escrow_instance = linked_escrow_package.deployments.get_instance("Escrow")
    assert escrow_instance.functions.sender().call() == sender
    assert w3.eth.getBalance(recipient) == w3.toWei("1000001", "ether")
Example #2
0
def test_linker_with_from(escrow_deployer, w3):
    escrow_strategy = linker(
        deploy("SafeSendLib"),
        link("Escrow", "SafeSendLib"),
        deploy("Escrow", w3.eth.accounts[0], transaction={"from": w3.eth.accounts[5]}),
    )
    escrow_deployer.register_strategy("Escrow", escrow_strategy)
    linked_escrow_package = escrow_deployer.deploy("Escrow")
    escrow_instance = linked_escrow_package.deployments.get_instance("Escrow")
    assert escrow_instance.functions.sender().call() == w3.eth.accounts[5]
Example #3
0
def escrow_package(deployer, w3):
    escrow_manifest = ASSETS_DIR / "escrow" / "1.0.3.json"
    escrow_deployer = deployer(escrow_manifest)
    escrow_strategy = l.linker(
        l.deploy("SafeSendLib"),
        l.link("Escrow", "SafeSendLib"),
        l.deploy("Escrow", w3.eth.accounts[0]),
    )
    escrow_deployer.register_strategy("Escrow", escrow_strategy)
    return escrow_deployer.deploy("Escrow")
Example #4
0
def escrow_package(w3, deployer):
    manifest = ASSETS_DIR / "escrow" / "1.0.3.json"
    escrow_deployer = deployer(manifest)
    escrow_strategy = linker(
        deploy("SafeSendLib"),
        link("Escrow", "SafeSendLib"),
        deploy("Escrow", w3.eth.accounts[0]),
    )
    escrow_deployer.register_strategy("Escrow", escrow_strategy)
    escrow_package = escrow_deployer.deploy("Escrow")
    return escrow_package, w3
Example #5
0
 def deploy(self, contract_type: str, *args: Any,
            **kwargs: Any) -> Tuple[Package, Address]:
     factory = self.package.get_contract_factory(contract_type)
     if contract_type in self.strategies:
         strategy = self.strategies[contract_type]
         return strategy(self.package)
     if factory.needs_bytecode_linking:
         raise DeployerError(
             "Unable to deploy an unlinked factory. "
             "Please register a strategy for this contract type.")
     strategy = linker(deploy(contract_type, *args, **kwargs))
     return strategy(self.package)
Example #6
0
def test_linker(escrow_deployer, w3):
    # todo test multiple links in one type
    assert isinstance(escrow_deployer, Deployer)
    with pytest.raises(DeployerError):
        escrow_deployer.deploy("Escrow")

    escrow_strategy = linker(
        deploy("SafeSendLib"),
        link("Escrow", "SafeSendLib"),
        deploy("Escrow", w3.eth.accounts[0]),
    )
    assert hasattr(escrow_strategy, "__call__")
    escrow_deployer.register_strategy("Escrow", escrow_strategy)
    linked_escrow_package = escrow_deployer.deploy("Escrow")
    assert isinstance(linked_escrow_package, Package)
    linked_escrow_factory = linked_escrow_package.get_contract_factory("Escrow")
    assert linked_escrow_factory.needs_bytecode_linking is False
Example #7
0
def solidity_registry_strategy():
    def set_authority(package):
        w3 = package.w3
        authority = package.deployments.get_instance("WhitelistAuthority").address
        package_registry = package.deployments.get_instance("PackageRegistry")
        package_db = package.deployments.get_instance("PackageDB")
        release_db = package.deployments.get_instance("ReleaseDB")
        txh_1 = package_registry.functions.setAuthority(authority).transact()
        w3.eth.waitForTransactionReceipt(txh_1)
        txh_2 = package_db.functions.setAuthority(authority).transact()
        w3.eth.waitForTransactionReceipt(txh_2)
        txh_3 = release_db.functions.setAuthority(authority).transact()
        w3.eth.waitForTransactionReceipt(txh_3)

    def set_dependencies(package):
        w3 = package.w3
        package_db = package.deployments.get_instance("PackageDB").address
        release_db = package.deployments.get_instance("ReleaseDB").address
        release_validator = package.deployments.get_instance("ReleaseValidator").address
        package_registry = package.deployments.get_instance("PackageRegistry")
        txh_1 = package_registry.functions.setPackageDb(package_db).transact()
        w3.eth.waitForTransactionReceipt(txh_1)
        txh_2 = package_registry.functions.setReleaseDb(release_db).transact()
        w3.eth.waitForTransactionReceipt(txh_2)
        txh_3 = package_registry.functions.setReleaseValidator(
            release_validator
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_3)

    def get_selector(deployments, contract, fn):
        function_abi = [
            x for x in deployments.get_instance(contract).abi if x["name"] == fn
        ][0]
        return function_abi_to_4byte_selector(function_abi)

    def set_permissions(package):
        w3 = package.w3
        deployments = package.deployments
        set_version = get_selector(deployments, "ReleaseDB", "setVersion")
        set_release = get_selector(deployments, "ReleaseDB", "setRelease")
        set_package = get_selector(deployments, "PackageDB", "setPackage")
        set_package_owner = get_selector(deployments, "PackageDB", "setPackageOwner")
        release = get_selector(deployments, "PackageRegistry", "release")
        transfer_package_owner = get_selector(
            deployments, "PackageRegistry", "transferPackageOwner"
        )
        package_db = package.deployments.get_instance("PackageDB").address
        release_db = package.deployments.get_instance("ReleaseDB").address
        package_registry = package.deployments.get_instance("PackageRegistry").address
        authority = package.deployments.get_instance("WhitelistAuthority")
        txh_1 = authority.functions.setCanCall(
            package_registry, release_db, set_release, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_1)
        txh_2 = authority.functions.setCanCall(
            package_registry, package_db, set_package, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_2)
        txh_3 = authority.functions.setCanCall(
            package_registry, package_db, set_package_owner, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_3)
        txh_4 = authority.functions.setAnyoneCanCall(
            release_db, set_version, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_4)
        txh_5 = authority.functions.setAnyoneCanCall(
            package_registry, release, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_5)
        txh_6 = authority.functions.setAnyoneCanCall(
            package_registry, transfer_package_owner, True
        ).transact()
        w3.eth.waitForTransactionReceipt(txh_6)

    strategy = linker.linker(
        linker.deploy("IndexedOrderedSetLib"),
        linker.link("PackageDB", "IndexedOrderedSetLib"),
        linker.link("ReleaseDB", "IndexedOrderedSetLib"),
        linker.deploy("PackageRegistry"),
        linker.deploy("WhitelistAuthority"),
        linker.deploy("PackageDB"),
        linker.deploy("ReleaseDB"),
        linker.deploy("ReleaseValidator"),
        linker.run_python(set_authority),
        linker.run_python(set_dependencies),
        linker.run_python(set_permissions),
    )
    return strategy