Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
eth_send.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
4 #
5 # SPDX-License-Identifier: LGPL-3.0+
6 
7 from zeth.cli.constants import ETH_ADDRESS_DEFAULT, ETH_PRIVATE_KEY_FILE_DEFAULT
8 from zeth.cli.utils import \
9  get_eth_network, load_eth_address, EtherValue, open_web3_from_network, \
10  load_eth_private_key
11 from click import command, option, pass_context, argument, ClickException, Context
12 from typing import Optional
13 
14 FUND_AMOUNT_DEFAULT = 1000000
15 
16 
17 @command()
18 @option(
19  "--eth-addr",
20  help=f"Source address or filename (default: {ETH_ADDRESS_DEFAULT})")
21 @option(
22  "--eth-private-key",
23  help=f"Source private key file (default: {ETH_PRIVATE_KEY_FILE_DEFAULT})")
24 @option(
25  "--amount",
26  type=str,
27  default=FUND_AMOUNT_DEFAULT,
28  help=f"Amount to fund in Ether (default: {FUND_AMOUNT_DEFAULT})")
29 @argument("dest-addr")
30 @pass_context
32  ctx: Context,
33  dest_addr: str,
34  eth_private_key: Optional[str],
35  eth_addr: Optional[str],
36  amount: str) -> None:
37  """
38  Send Ether from the local eth-addr to a destination address.
39  """
40  dest_addr = load_eth_address(dest_addr)
41  eth_private_key_data = load_eth_private_key(eth_private_key)
42  eth_addr = load_eth_address(eth_addr)
43  eth_network = get_eth_network(ctx.obj["eth_network"])
44  web3 = open_web3_from_network(eth_network)
45 
46  if eth_private_key_data is None:
47  raise ClickException("hosted accounts are not supported")
48 
49  print(f"eth_addr = {eth_addr}")
50  print(f"dest_addr = {dest_addr}")
51  print(f"amount = {amount}")
52 
53  # pylint: disable=no-member
54  send_tx_desc = {
55  "from": eth_addr,
56  "to": dest_addr,
57  "value": EtherValue(amount).wei,
58  "gasPrice": web3.eth.gasPrice,
59  "nonce": web3.eth.getTransactionCount(eth_addr)
60  }
61  send_tx_desc["gas"] = web3.eth.estimateGas(send_tx_desc)
62 
63  signed_tx = web3.eth.account.signTransaction(
64  send_tx_desc, eth_private_key_data)
65 
66  tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
67  # pylint: enable=no-member
68 
69  print(tx_hash.hex())
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_send.eth_send
None eth_send(Context ctx, str dest_addr, Optional[str] eth_private_key, Optional[str] eth_addr, str amount)
Definition: eth_send.py:31
zeth.cli.constants
Definition: constants.py:1
zeth.cli.utils.load_eth_private_key
Optional[bytes] load_eth_private_key(Optional[str] private_key_file)
Definition: utils.py:465
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