Esempio n. 1
0
    def get_contract_proxy(self, contract_name: str, address: str) -> Contract:
        """Get web3.Contract to interact directly with the network"""
        abi_data = self.abi[contract_name]

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

        return contract_class(address=to_checksum_address(address))
Esempio 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]

        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
Esempio n. 3
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
Esempio n. 4
0
def token_contract(sample_token, web3) -> Contract:
    """Proxied ABI to the deployed SecurityToken token contract."""

    contract_name = "SecurityToken"

    abi = get_abi(None)

    abi_data = abi[contract_name]

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

    return contract_class(address=sample_token)
 def factory(cls, *args, **kwargs):
     return compose(cls, Contract.factory(*args, **kwargs))
Esempio n. 6
0
def deploy_contract(web3: Web3,
                    abi_data: dict,
                    gas=1500000,
                    timeout=60.0,
                    constructor_arguments: Optional[list] = None,
                    from_account=None) -> Tuple[Contract, str]:
    """Deploys a single contract using Web3 client.

    :param web3: Web3 client instance

    :param contract_definition: Dictionary of describing the contract interface,
        as read from ``contracts.json`` Contains

    :param gas: Max gas

    :param timeout: How many seconds to wait the transaction to
        confirm to get the contract address.

    :param constructor_arguments: Arguments passed to the smart contract
        constructor. Automatically encoded through ABI signature.

    :param from_account: Geth account that's balance is used for deployment.
        By default, the gas is spent from Web3 coinbase account. Account must be unlocked.

    :return: Tuple containing Contract proxy object and the transaction hash where it was deployed

    :raise web3.utils.compat.Timeout: If we can't get our contract in a block within given timeout
    """

    # Check we are passed valid contract definition
    assert "abi" in abi_data, \
        "Please pass a valid contract definition dictionary, got {}".format(abi_data.keys())

    assert len(abi_data["bytecode"]
               ) > 8, "Contract did not have properly compiled code payload"

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

    if not from_account:
        from_account = web3.eth.coinbase

    # Set transaction parameters
    transaction = {
        "gas": gas,
        "from": from_account,
    }

    # Call web3 to deploy the contract
    txn_hash = contract_class.deploy(transaction, constructor_arguments)

    # Wait until we get confirmation and address
    address = get_contract_address_from_txn(web3, txn_hash, timeout=timeout)

    # Create Contract proxy object
    contract = contract_class(address=address, )

    return contract, txn_hash