Пример #1
0
def build_transaction_for_function(abi,
                                   address,
                                   web3,
                                   function_name=None,
                                   transaction=None,
                                   *args,
                                   **kwargs):
    """Builds a dictionary with the fields required to make the given transaction

    Don't call this directly, instead use :meth:`Contract.buildTransaction`
    on your contract instance.
    """
    prepared_transaction = prepare_transaction(
        abi,
        address,
        web3,
        fn_identifier=function_name,
        fn_args=args,
        fn_kwargs=kwargs,
        transaction=transaction,
    )

    prepared_transaction = fill_transaction_defaults(web3,
                                                     prepared_transaction)

    return prepared_transaction
Пример #2
0
def build_transaction_for_function(abi,
                                   address,
                                   web3,
                                   function_name=None,
                                   transaction=None,
                                   *args,
                                   **kwargs):
    """Builds a dictionary with the fields required to make the given transaction

    Don't call this directly, instead use :meth:`Contract.buildTransaction`
    on your contract instance.
    """
    prepared_transaction = prepare_transaction(
        abi,
        address,
        web3,
        fn_name=function_name,
        fn_args=args,
        fn_kwargs=kwargs,
        transaction=transaction,
    )

    prepared_transaction = fill_transaction_defaults(web3, prepared_transaction)

    return prepared_transaction
Пример #3
0
    def web3py_contract_deploy(self, w3, nonce, abi, bytecode, args):
        """
        TODO 远程部署智能合约
        :param w3:   Web3 实例
        :param abi:   智能合约编译的 abi
        :param bytecode: 智能合约编译bin code
        :return: tx_hash
        """
        from web3.utils.transactions import (
            fill_transaction_defaults, )
        if not isinstance(w3, Web3):
            raise EtherError({"err": u"无效的Web3实例!", "code": 4000})

        contract = w3.eth.contract(abi=abi, bytecode=bytecode)
        deploy_contract = contract.constructor(args)
        gas = deploy_contract.estimateGas()
        deploy_transaction = fill_transaction_defaults(
            w3, {
                "from": to_checksum_address(self.address),
                "to": "0x",
                "nonce": nonce,
                "gas": gas,
                "gasPrice": w3.eth.gasPrice,
                "chainId": int(w3.net.chainId),
                "data": deploy_contract.data_in_transaction
            })

        signed_txn = w3.eth.account.signTransaction(deploy_transaction,
                                                    self.private_key)
        tx_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
        return bytes_to_str(tx_hash)
Пример #4
0
    def sign_and_send_raw_middleware(make_request, w3):

        format_and_fill_tx = compose(
            format_transaction,
            fill_transaction_defaults(w3),
            fill_nonce(w3))

        def middleware(method, params):
            if method != "eth_sendTransaction":
                return make_request(method, params)
            else:
                transaction = format_and_fill_tx(params[0])

            if 'from' not in transaction:
                return make_request(method, params)
            elif transaction.get('from') not in accounts:
                return make_request(method, params)

            account = accounts[transaction['from']]
            raw_tx = account.signTransaction(transaction).rawTransaction

            return make_request(
                "eth_sendRawTransaction",
                [raw_tx])

        return middleware
Пример #5
0
    def buildTransaction(self, transaction=None):
        """
        Build the transaction dictionary without sending
        """

        if transaction is None:
            built_transaction = {}
        else:
            built_transaction = dict(**transaction)
            self.check_forbidden_keys_in_transaction(built_transaction,
                                                     ["data", "to"])

        if self.web3.eth.defaultAccount is not empty:
            built_transaction.setdefault('from', self.web3.eth.defaultAccount)

        built_transaction['data'] = self.data_in_transaction
        built_transaction['to'] = b''
        return fill_transaction_defaults(self.web3, built_transaction)
Пример #6
0
def build_transaction_for_constructor(abi,
                                      bytecode,
                                      web3,
                                      transaction=None,
                                      *args,
                                      **kwargs):
    """Builds a dictionary with the fields required to make the given transaction

    Don't call this directly, instead use :meth:`Contract.buildTransaction`
    on your contract instance.
    """
    prepared_transaction = prepare_constructor(
        abi,
        bytecode,
        web3,
        args=args,
        kwargs=kwargs,
        transaction=transaction,
    )

    prepared_transaction = fill_transaction_defaults(web3,
                                                     prepared_transaction)

    return prepared_transaction