Ejemplo n.º 1
0
    def deploy_contract(
            self,
            deployer_address: str,
            registry: BaseContractRegistry,
            contract_name: str,
            *constructor_args,
            enroll: bool = True,
            gas_limit: int = None,
            confirmations: int = 0,
            contract_version: str = 'latest',
            **constructor_kwargs) -> Tuple[VersionedContract, TxReceipt]:
        """
        Retrieve compiled interface data from the cache and
        return an instantiated deployed contract
        """

        #
        # Build the deployment transaction #
        #

        deploy_transaction = dict()
        if gas_limit:
            deploy_transaction.update({'gas': gas_limit})

        pprint_args = ', '.join(
            list(map(str, constructor_args)) +
            list(f"{k}={v}" for k, v in constructor_kwargs.items()))

        contract_factory = self.get_contract_factory(
            contract_name=contract_name, version=contract_version)
        self.log.info(
            f"Deploying contract {contract_name}:{contract_factory.version} with "
            f"deployer address {deployer_address} "
            f"and parameters {pprint_args}")

        constructor_function = contract_factory.constructor(
            *constructor_args, **constructor_kwargs)
        constructor_calldata = encode_constructor_arguments(
            self.client.w3, constructor_function, *constructor_args,
            **constructor_kwargs)
        if constructor_calldata:
            self.log.info(f"Constructor calldata: {constructor_calldata}")

        #
        # Transmit the deployment tx #
        #

        receipt = self.send_transaction(contract_function=constructor_function,
                                        sender_address=deployer_address,
                                        payload=deploy_transaction,
                                        confirmations=confirmations)

        # Success
        address = receipt['contractAddress']
        self.log.info(
            f"Confirmed {contract_name}:{contract_factory.version} deployment: new address {address}"
        )

        #
        # Instantiate & Enroll contract
        #

        contract = self.client.w3.eth.contract(
            address=address,
            abi=contract_factory.abi,
            version=contract_factory.version,
            ContractFactoryClass=self._CONTRACT_FACTORY)

        if enroll is True:
            registry.enroll(contract_name=contract_name,
                            contract_address=contract.address,
                            contract_abi=contract.abi,
                            contract_version=contract.version)

        return contract, receipt  # receipt
Ejemplo n.º 2
0
    def deploy_contract(
            self,
            deployer_address: str,
            registry: BaseContractRegistry,
            contract_name: str,
            *constructor_args,
            enroll: bool = True,
            gas_limit: int = None,
            contract_version: str = 'latest',
            **constructor_kwargs) -> Tuple[VersionedContract, dict]:
        """
        Retrieve compiled interface data from the cache and
        return an instantiated deployed contract
        """

        if not is_checksum_address(deployer_address):
            raise ValueError(
                f"{deployer_address} is not a valid EIP-55 checksum address.")

        #
        # Build the deployment transaction #
        #

        deploy_transaction = dict()
        if gas_limit:
            deploy_transaction.update({'gas': gas_limit})

        pprint_args = str(tuple(constructor_args))
        pprint_args = pprint_args.replace("{", "{{").replace("}",
                                                             "}}")  # See #724

        contract_factory = self.get_contract_factory(
            contract_name=contract_name, version=contract_version)
        self.log.info(
            f"Deploying contract {contract_name}:{contract_factory.version} with "
            f"deployer address {deployer_address} "
            f"and parameters {pprint_args}")

        transaction_function = contract_factory.constructor(
            *constructor_args, **constructor_kwargs)

        #
        # Transmit the deployment tx #
        #

        receipt = self.send_transaction(contract_function=transaction_function,
                                        sender_address=deployer_address,
                                        payload=deploy_transaction)

        #
        # Verify deployment success
        #

        # Success
        address = receipt['contractAddress']
        self.log.info(
            f"Confirmed {contract_name}:{contract_factory.version} deployment: new address {address}"
        )

        #
        # Instantiate & Enroll contract
        #

        contract = self.client.w3.eth.contract(
            address=address,
            abi=contract_factory.abi,
            version=contract_factory.version,
            ContractFactoryClass=self._contract_factory)

        if enroll is True:
            registry.enroll(contract_name=contract_name,
                            contract_address=contract.address,
                            contract_abi=contract.abi,
                            contract_version=contract.version)

        return contract, receipt  # receipt