Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
token_approve.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 INSTANCE_FILE_DEFAULT
6 from zeth.cli.utils import load_eth_address, load_eth_private_key, \
7  get_eth_network, open_web3_from_network, load_mixer_description, EtherValue
8 from zeth.core.contracts import send_contract_call
9 from click import command, argument, option, pass_context, ClickException, Context
10 
11 
12 @command()
13 @argument("value")
14 @option("--eth-addr", help="Sender eth address or address filename")
15 @option("--eth-private-key", help="Sender eth private key")
16 @option("--wait", is_flag=True, help="Wait for transaction to complete")
17 @option(
18  "--instance-file",
19  default=INSTANCE_FILE_DEFAULT,
20  help=f"Instance file (default={INSTANCE_FILE_DEFAULT})")
21 @pass_context
23  ctx: Context,
24  value: str,
25  eth_addr: str,
26  eth_private_key: str,
27  wait: bool,
28  instance_file: str) -> None:
29  """
30  Approve the mixer to spend some amount of ERC20/223 tokens
31  """
32  approve_value = EtherValue(value)
33  eth_addr = load_eth_address(eth_addr)
34  eth_private_key_data = load_eth_private_key(eth_private_key)
35  web3 = open_web3_from_network(get_eth_network(ctx.obj["eth_network"]))
36  mixer_desc = load_mixer_description(instance_file)
37  if not mixer_desc.token:
38  raise ClickException("no token for mixer {mixer_desc.mixer.address}")
39 
40  token_instance = mixer_desc.token.instantiate(web3)
41  approve_call = token_instance.functions.approve(
42  mixer_desc.mixer.address,
43  approve_value.wei)
44  tx_hash = send_contract_call(
45  web3, approve_call, eth_addr, eth_private_key_data)
46 
47  if wait:
48  web3.eth.waitForTransactionReceipt(tx_hash) # pylint: disable=no-member
49  else:
50  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.token_approve.token_approve
None token_approve(Context ctx, str value, str eth_addr, str eth_private_key, bool wait, str instance_file)
Definition: token_approve.py:22
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.core.contracts
Definition: contracts.py:1
zeth.cli.utils.load_mixer_description
MixerDescription load_mixer_description(str mixer_desc_file)
Definition: utils.py:212
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
zeth.core.contracts.send_contract_call
bytes send_contract_call(Any web3, Any call, str sender_eth_addr, Optional[bytes] sender_eth_private_key=None, Optional[EtherValue] value=None, Optional[int] gas=None)
Definition: contracts.py:131