示例#1
0
    def get_contract_factory(self, name: ContractName, w3: Web3 = None) -> Contract:
        """
        API to generate a contract factory class.
        """
        current_w3 = None

        if w3 is not None:
            current_w3 = w3
        else:
            current_w3 = self.w3

        validate_contract_name(name)
        validate_w3_instance(current_w3)

        try:
            contract_data = self.package_data["contract_types"][name]
            validate_minimal_contract_factory_data(contract_data)
        except KeyError:
            raise InsufficientAssetsError(
                "This package has insufficient package data to generate "
                "a contract factory for contract:{0}.".format(name)
            )

        contract_kwargs = generate_contract_factory_kwargs(contract_data)
        contract_factory = current_w3.eth.contract(**contract_kwargs)
        return contract_factory
示例#2
0
    def get_contract_factory(self, name: ContractName) -> LinkableContract:
        """
        Return the contract factory for a given contract type, generated from the data vailable
        in ``Package.manifest``. Contract factories are accessible from the package class.

        .. code:: python

           Owned = OwnedPackage.get_contract_factory('owned')

        In cases where a contract uses a library, the contract factory will have
        unlinked bytecode. The ``ethpm`` package ships with its own subclass of
        ``web3.contract.Contract``, ``ethpm.contract.LinkableContract`` with a few extra
        methods and properties related to bytecode linking.

        .. code:: python

           >>> math = owned_package.contract_factories.math
           >>> math.needs_bytecode_linking
           True
           >>> linked_math = math.link_bytecode({'MathLib': '0x1234...'})
           >>> linked_math.needs_bytecode_linking
           False
        """
        validate_contract_name(name)

        if "contractTypes" not in self.manifest:
            raise InsufficientAssetsError(
                "This package does not contain any contract type data."
            )

        try:
            contract_data = self.manifest["contractTypes"][name]
        except KeyError:
            raise InsufficientAssetsError(
                "This package does not contain any package data to generate "
                f"a contract factory for contract type: {name}. Available contract types include: "
                f"{self.contract_types}."
            )

        validate_minimal_contract_factory_data(contract_data)
        contract_kwargs = generate_contract_factory_kwargs(contract_data)
        contract_factory = self.w3.eth.contract(**contract_kwargs)
        return contract_factory
示例#3
0
def validate_minimal_contract_factory_data(contract_data: Dict[str, str]) -> None:
    """
    Validate that contract data in a package contains at least an "abi" and
    "deployment_bytecode" necessary to generate a deployable contract factory.
    """
    if not all(key in contract_data.keys() for key in ("abi", "deployment_bytecode")):
        raise InsufficientAssetsError(
            "Minimum required contract data to generate a deployable "
            "contract factory (abi & deployment_bytecode) not found."
        )
def get_all_manifest_paths():
    # Expects all json in ethpm/assets to be either compiler_output or a manifest
    all_use_case_json = set(ASSETS_DIR.glob(SOURCES_GLOB)) - set(
        (ASSETS_DIR / "spec").glob(SOURCES_GLOB))
    all_manifests = [
        json for json in all_use_case_json if json.name == "v3.json"
    ]
    if not all_manifests:
        raise InsufficientAssetsError(
            "Error importing manifests for validation, "
            "no v3 manifests found in `ethpm/ethpm-spec` submodule")
    return all_manifests
示例#5
0
    def get_contract_factory(self, name: ContractName) -> Contract:
        """
        Return a contract factory for a given contract type.
        """
        validate_contract_name(name)

        if "contract_types" not in self.manifest:
            raise InsufficientAssetsError(
                "This package does not contain any contract type data.")

        try:
            contract_data = self.manifest["contract_types"][name]
        except KeyError:
            raise InsufficientAssetsError(
                "This package does not contain any package data to generate "
                f"a contract factory for contract type: {name}. Available contract types include: "
                f"{ list(self.manifest['contract_types'].keys()) }.")

        validate_minimal_contract_factory_data(contract_data)
        contract_kwargs = generate_contract_factory_kwargs(contract_data)
        contract_factory = self.w3.eth.contract(**contract_kwargs)
        return contract_factory
def get_all_manifest_paths():
    # Expects all json in ethpm/assets to be either compiler_output or a manifest
    all_use_case_json = set(ASSETS_DIR.glob(SOURCES_GLOB)) - set(
        (ASSETS_DIR / "spec").glob(SOURCES_GLOB))
    all_manifests = [
        json for json in all_use_case_json
        if "compiler_output" not in json.name
    ]
    if not all_manifests:
        raise InsufficientAssetsError(
            "Error importing manifests for validation, found no manifests in `ethpm/assets/`"
        )
    return all_manifests
示例#7
0
 def get_contract_instance(self, name: ContractName,
                           address: Address) -> Contract:
     """
     Return a Contract object representing the contract type at the provided address.
     """
     validate_address(address)
     validate_contract_name(name)
     try:
         self.manifest["contract_types"][name]["abi"]
     except KeyError:
         raise InsufficientAssetsError(
             "Package does not have the ABI required to generate a contract instance "
             f"for contract: {name} at address: {address}.")
     contract_kwargs = generate_contract_factory_kwargs(
         self.manifest["contract_types"][name])
     canonical_address = to_canonical_address(address)
     contract_instance = self.w3.eth.contract(address=canonical_address,
                                              **contract_kwargs)
     return contract_instance
示例#8
0
 def get_contract_instance(self, name: ContractName,
                           address: Address) -> Contract:
     """
     Will return a ``Web3.contract`` instance generated from the contract type data available
     in ``Package.manifest`` and the provided ``address``. The provided ``address`` must be
     valid on the connected chain available through ``Package.w3``.
     """
     validate_address(address)
     validate_contract_name(name)
     try:
         self.manifest["contract_types"][name]["abi"]
     except KeyError:
         raise InsufficientAssetsError(
             "Package does not have the ABI required to generate a contract instance "
             f"for contract: {name} at address: {address}.")
     contract_kwargs = generate_contract_factory_kwargs(
         self.manifest["contract_types"][name])
     contract_instance = self.w3.eth.contract(address=address,
                                              **contract_kwargs)
     return contract_instance