Пример #1
0
def fund_eth_address(eth_network: Optional[str], eth_addr: Optional[str],
                     source_addr: Optional[str], amount: int) -> None:
    """
    Fund an address. If no source address is given, the first hosted account on
    the RPC host is used.
    """
    eth_addr = load_eth_address(eth_addr)
    network = get_eth_network(eth_network)
    web3 = open_web3_from_network(network)

    if not source_addr:
        # Use the first hosted address.
        source_addr = web3.eth.accounts[0]  # pylint: disable=no-member

        if network.name == "autonity-helloworld":
            # The Autonity helloworld network supplies hosted accounts, secured
            # with the password 'test'. Attempt to unlock it.
            # pylint: disable=import-outside-toplevel, no-member
            from web3.middleware import geth_poa_middleware  # type: ignore
            web3.middleware_stack.inject(geth_poa_middleware, layer=0)
            web3.personal.unlockAccount(source_addr, "test")

    print(f"eth_addr = {eth_addr}")
    print(f"source_addr = {source_addr}")
    print(f"amount = {amount}")

    web3.eth.sendTransaction({  # pylint: disable=no-member
        "from": source_addr,
        "to": eth_addr,
        "value": EtherValue(amount).wei
    })
Пример #2
0
def token_approve(
        ctx: Context,
        value: str,
        eth_addr: str,
        eth_private_key: str,
        wait: bool,
        instance_file: str) -> None:
    """
    Approve the mixer to spend some amount of ERC20/223 tokens
    """
    approve_value = EtherValue(value)
    eth_addr = load_eth_address(eth_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    web3 = open_web3_from_network(get_eth_network(ctx.obj["eth_network"]))
    mixer_desc = load_mixer_description(instance_file)
    if not mixer_desc.token:
        raise ClickException("no token for mixer {mixer_desc.mixer.address}")

    token_instance = mixer_desc.token.instantiate(web3)
    approve_call = token_instance.functions.approve(
        mixer_desc.mixer.address,
        approve_value.wei)
    tx_hash = send_contract_call(
        web3, approve_call, eth_addr, eth_private_key_data)

    if wait:
        web3.eth.waitForTransactionReceipt(tx_hash)  # pylint: disable=no-member
    else:
        print(tx_hash.hex())
Пример #3
0
def eth_send(
        ctx: Context,
        dest_addr: str,
        eth_private_key: Optional[str],
        eth_addr: Optional[str],
        amount: str) -> None:
    """
    Send Ether from the local eth-addr to a destination address.
    """
    dest_addr = load_eth_address(dest_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    eth_addr = load_eth_address(eth_addr)
    eth_network = get_eth_network(ctx.obj["eth_network"])
    web3 = open_web3_from_network(eth_network)

    if eth_private_key_data is None:
        raise ClickException("hosted accounts are not supported")

    print(f"eth_addr = {eth_addr}")
    print(f"dest_addr = {dest_addr}")
    print(f"amount = {amount}")

    # pylint: disable=no-member
    send_tx_desc = {
        "from": eth_addr,
        "to": dest_addr,
        "value": EtherValue(amount).wei,
        "gasPrice": web3.eth.gasPrice,
        "nonce": web3.eth.getTransactionCount(eth_addr)
    }
    send_tx_desc["gas"] = web3.eth.estimateGas(send_tx_desc)

    signed_tx = web3.eth.account.signTransaction(
        send_tx_desc, eth_private_key_data)

    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    # pylint: enable=no-member

    print(tx_hash.hex())