Exemplo n.º 1
0
    def deploy_contract(self, contract_name: str, abi: dict, note: str, constructor_args=None) -> _PreparedTransaction:
        """Deploys a contract."""

        if not constructor_args:
            constructor_args = {}

        abi_data = abi[contract_name]

        assert "source" in abi_data, "We need to have special postprocessed ABI data bundle, as we need the contract source code for EtherScan verification"

        contract_class = Contract.factory(
            web3=self.web3,
            abi=abi_data["abi"],
            bytecode=abi_data["bytecode"],
            bytecode_runtime=abi_data["bytecode_runtime"],
            )

        broadcast_account = self.get_or_create_broadcast_account()

        next_nonce = self.get_next_nonce()

        # Creates a dict for signing
        tx_data = self.generate_tx_data(next_nonce, contract_tx=True)
        constructed_txn = contract_class.constructor(**constructor_args).buildTransaction(tx_data)

        constructor_arguments = get_constructor_arguments(contract_class, kwargs=constructor_args)

        derived_contract_address = mk_contract_address(self.address, next_nonce)
        derived_contract_address = to_checksum_address(derived_contract_address.lower())

        constructed_txn["to"] = "" # Otherwise database serializer complains about bytes string

        tx = self.allocate_transaction(
            broadcast_account=broadcast_account,
            receiver=None,
            contract_address=derived_contract_address,
            contract_deployment=True,
            nonce=next_nonce,
            note=note,
            unsigned_payload=constructed_txn,
            gas_price=self.gas_price,
            gas_limit=self.gas_limit,
        )

        self.dbsession.flush()  # Populate other_data

        tx.abi = abi_data
        tx.constructor_arguments = constructor_arguments
        assert tx.compiler_version
        assert tx.flattened_source_code

        return tx
Exemplo n.º 2
0
    def deploy_contract(self,
                        contract_name: str,
                        abi: dict,
                        note: str,
                        constructor_args=None) -> _PreparedTransaction:
        """Deploys a contract."""

        if not constructor_args:
            constructor_args = {}

        abi_data = abi[contract_name]

        contract_class = Contract.factory(
            web3=self.web3,
            abi=abi_data["abi"],
            bytecode=abi_data["bytecode"],
            bytecode_runtime=abi_data["bytecode_runtime"],
        )

        broadcast_account = self.get_or_create_broadcast_account()

        next_nonce = self.get_next_nonce()

        # Creates a dict for signing
        tx_data = self.generate_tx_data(next_nonce, contract_tx=True)
        constructed_txn = contract_class.constructor(
            **constructor_args).buildTransaction(tx_data)

        derived_contract_address = mk_contract_address(self.address,
                                                       next_nonce)
        derived_contract_address = to_checksum_address(
            derived_contract_address.lower())

        constructed_txn[
            "to"] = ""  # Otherwise database serializer complains about bytes string

        tx = self.allocate_transaction(
            broadcast_account=broadcast_account,
            receiver=None,
            contract_address=derived_contract_address,
            contract_deployment=True,
            nonce=next_nonce,
            note=note,
            unsigned_payload=constructed_txn,
            gas_price=self.gas_price,
            gas_limit=self.gas_limit,
        )

        self.dbsession.flush()
        return tx