Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
eth_fund.py
Go to the documentation of this file.
1 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 #
3 # SPDX-License-Identifier: LGPL-3.0+
4 
5 from zeth.cli.constants import ETH_ADDRESS_DEFAULT
6 from zeth.cli.utils import \
7  get_eth_network, load_eth_address, EtherValue, open_web3_from_network
8 from click import command, option, pass_context, Context
9 from typing import Optional
10 
11 
12 FUND_AMOUNT_DEFAULT = 1000000
13 
14 
15 @command()
16 @option(
17  "--eth-addr",
18  help=f"Address or address filename (default: {ETH_ADDRESS_DEFAULT})")
19 @option("--source-addr", help="Address or address filename (optional)")
20 @option(
21  "--amount",
22  type=int,
23  default=FUND_AMOUNT_DEFAULT,
24  help=f"Amount to fund (default: {FUND_AMOUNT_DEFAULT})")
25 @pass_context
27  ctx: Context,
28  eth_addr: Optional[str],
29  source_addr: Optional[str],
30  amount: int) -> None:
31  """
32  Fund an address. If no source address is given, the first hosted account on
33  the RPC host is used. This command should only be used in test environments
34  such as ganache or autonity-helloworld.
35  """
36  eth_addr = load_eth_address(eth_addr)
37  eth_network = get_eth_network(ctx.obj["eth_network"])
38  web3 = open_web3_from_network(eth_network)
39 
40  if not source_addr:
41  # Use the first hosted address.
42  source_addr = web3.eth.accounts[0] # pylint: disable=no-member
43 
44  if eth_network.name == "autonity-helloworld":
45  # The Autonity helloworld network supplies hosted accounts, secured
46  # with the password 'test'. Attempt to unlock it.
47  # pylint: disable=import-outside-toplevel, no-member
48  from web3.middleware import geth_poa_middleware # type: ignore
49  web3.middleware_onion.inject(geth_poa_middleware, layer=0)
50  web3.personal.unlockAccount(source_addr, "test")
51 
52  source_addr = load_eth_address(source_addr)
53  print(f"eth_addr = {eth_addr}")
54  print(f"source_addr = {source_addr}")
55  print(f"amount = {amount}")
56 
57  web3.eth.sendTransaction({ # pylint: disable=no-member
58  "from": source_addr,
59  "to": eth_addr,
60  "value": EtherValue(amount).wei
61  })
zeth.cli.utils.get_eth_network
NetworkConfig get_eth_network(Optional[str] eth_network)
Definition: utils.py:84
zeth.cli.utils.load_eth_address
str load_eth_address(Optional[str] eth_addr)
Definition: utils.py:444
zeth.cli.utils
Definition: utils.py:1
zeth.helper.eth_fund.eth_fund
None eth_fund(Context ctx, Optional[str] eth_addr, Optional[str] source_addr, int amount)
Definition: eth_fund.py:26
zeth.cli.constants
Definition: constants.py:1
zeth.cli.utils.open_web3_from_network
Any open_web3_from_network(NetworkConfig eth_net)
Definition: utils.py:114
zeth.core.utils.EtherValue
Definition: utils.py:46