Example #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
    })
Example #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())
Example #3
0
 def get_web3(self) -> Any:
     """
     Create and cache web3 connection.
     """
     if not self._web3:
         self._web3 = open_web3_from_network(
             get_eth_network(self.eth_network))
     return self._web3
Example #4
0
def dummy_app(
        ctx: Context,
        eth_network: Optional[str],
        eth_addr: Optional[str],
        eth_private_key: Optional[str]) -> None:
    ctx.obj = {
        "eth_network": get_eth_network(eth_network),
        "eth_addr": eth_addr,
        "eth_private_key": eth_private_key,
    }
Example #5
0
def eth_get_balance(ctx: Any, eth_addr: Optional[str], wei: bool) -> None:
    """
    Command to get the balance of specific addresses. Support multiple queries
    per invocation (outputs one per line), for efficiency.
    """
    eth_addr = load_eth_address(eth_addr)
    web3 = open_web3_from_network(get_eth_network(ctx.obj["eth_network"]))
    balance_wei = web3.eth.getBalance(eth_addr)  # pylint: disable=no-member
    if wei:
        print(balance_wei)
    else:
        print(EtherValue(balance_wei, "wei").ether())
Example #6
0
 def setUpClass() -> None:
     web3: Any = open_web3_from_network(get_eth_network(None))
     contracts_dir = get_contracts_dir()
     contract_instance_desc = InstanceDescription.deploy(
         web3,
         join(contracts_dir, "TestGroth16BLS12_377.sol"),
         "TestGroth16BLS12_377",
         web3.eth.accounts[0],  # pylint: disable=no-member
         None,
         500000,
         {"allow_paths": contracts_dir})
     global CONTRACT_INSTANCE  # pylint: disable=global-statement
     CONTRACT_INSTANCE = contract_instance_desc.instantiate(web3)
Example #7
0
def eth_gen_network_config(
        eth_network: str,
        eth_rpc_endpoint: Optional[str],
        eth_rpc_certificate: Optional[str],
        eth_rpc_insecure: bool,
        output_file: str) -> None:
    """
    Generate a network config file. ETH_NETWORK is a network name or
    pre-existing network config file.

    Examples:

    \b
        # Write default config for "ganache" to the default file
        $ zeth_helper eth-gen-network-config ganache

    \b
        # Write "geth" config with a custom endpoint to default file
        $ zeth_helper eth-gen-network-config geth \\
            --eth-rpc-endpoint http://localhost:8080

    \b
        # Write a custom https endpoint to file, specifying the certificate
        $ zeth_helper eth-gen-network-config \\
            my-network \\
            --eth-rpc-endpoint https://rpc.my-network.io:8545 \\
            --eth-rpc-certificate rpc.my-network.io.crt

    \b
        # Write default network and endpoint to file "default-network"
        $ zeth_helper eth-gen-network-config --output-file default-network
    """

    if eth_rpc_endpoint is not None:
        network = NetworkConfig(
            name=eth_network,
            endpoint=eth_rpc_endpoint,
            certificate=eth_rpc_certificate,
            insecure=eth_rpc_insecure)
    else:
        network = get_eth_network(eth_network)

    network_json = network.to_json()
    print(f"network: {network_json}")

    with open(output_file, "w") as eth_network_f:
        eth_network_f.write(network_json)
    print(f"written to \"{output_file}\"")
Example #8
0
def main() -> int:
    web3: Any = open_web3_from_network(get_eth_network(None))
    contracts_dir = get_contracts_dir()
    contract_instance_desc = InstanceDescription.deploy(
        web3,
        join(contracts_dir, "MiMC_test.sol"),
        "MiMC_test",
        web3.eth.accounts[0],  # pylint: disable=no-member
        None,
        500000,
        {"allow_paths": contracts_dir})
    contract_instance = contract_instance_desc.instantiate(web3)

    test_mimc7(contract_instance)
    test_mimc31(contract_instance)

    print("========================================")
    print("==              PASSED                ==")
    print("========================================")
    return 0
Example #9
0
def deploy_test_token(eth_network: Optional[str], eth_addr: Optional[str],
                      eth_private_key: Optional[str], mint_amount: int,
                      recipient_address: str) -> None:
    """
    Deploy a simple ERC20 token for testing, and mint some for a specific
    address. Print the token address.
    """
    eth_addr = load_eth_address(eth_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    recipient_address = load_eth_address(recipient_address)
    web3 = open_web3_from_network(get_eth_network(eth_network))
    token_instance = deploy_token(
        web3, eth_addr, eth_private_key_data, 4000000) \
        # pylint: disable=no-member

    mint_tx_hash = mint_token(web3, token_instance, recipient_address,
                              eth_addr, eth_private_key_data,
                              EtherValue(mint_amount, 'ether'))
    web3.eth.waitForTransactionReceipt(mint_tx_hash)
    print(token_instance.address)
Example #10
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())