Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
deploy_test_token.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.core.contracts import Interface, send_contract_call
6 from zeth.core.utils import EtherValue, get_zeth_dir
7 from zeth.core.constants import SOL_COMPILER_VERSION
8 from zeth.cli.utils import load_eth_address, load_eth_private_key, \
9  get_eth_network, open_web3_from_network
10 from zeth.cli.constants import ETH_ADDRESS_DEFAULT, \
11  ETH_NETWORK_FILE_DEFAULT, ETH_NETWORK_DEFAULT
12 from click import command, argument, option
13 from os.path import join
14 from solcx import compile_files, set_solc_version
15 from typing import Optional, Any
16 
17 
18 @command()
19 @option(
20  "--eth-addr",
21  help=f"Address or address filename (default: {ETH_ADDRESS_DEFAULT})")
22 @option("--eth-private-key", help="Sender's eth private key file")
23 @option(
24  "--eth-network",
25  default=None,
26  help="Ethereum RPC endpoint, network or config file "
27  f"(default: '{ETH_NETWORK_FILE_DEFAULT}' if it exists, otherwise "
28  f"'{ETH_NETWORK_DEFAULT}')")
29 @argument("mint_amount", type=int)
30 @argument("recipient_address")
32  eth_network: Optional[str],
33  eth_addr: Optional[str],
34  eth_private_key: Optional[str],
35  mint_amount: int,
36  recipient_address: str) -> None:
37  """
38  Deploy a simple ERC20 token for testing, and mint some for a specific
39  address. Print the token address.
40  """
41  eth_addr = load_eth_address(eth_addr)
42  eth_private_key_data = load_eth_private_key(eth_private_key)
43  recipient_address = load_eth_address(recipient_address)
44  web3 = open_web3_from_network(get_eth_network(eth_network))
45  token_instance = deploy_token(
46  web3, eth_addr, eth_private_key_data, 4000000) \
47  # pylint: disable=no-member
48  mint_tx_hash = mint_token(
49  web3,
50  token_instance,
51  recipient_address,
52  eth_addr,
53  eth_private_key_data,
54  EtherValue(mint_amount, 'ether'))
55  web3.eth.waitForTransactionReceipt(mint_tx_hash) # pylint: disable=no-member
56 
57  print(token_instance.address)
58 
59 
60 def compile_token() -> Interface:
61  """
62  Compile the testing ERC20 token contract
63  """
64 
65  zeth_dir = get_zeth_dir()
66  allowed_path = join(
67  zeth_dir,
68  "zeth_contracts/contracts")
69  path_to_token = join(
70  zeth_dir,
71  "zeth_contracts/contracts",
72  "ERC20Mintable.sol")
73  # Compilation
74  set_solc_version(SOL_COMPILER_VERSION)
75  compiled_sol = compile_files([path_to_token], allow_paths=allowed_path)
76  token_interface = compiled_sol[path_to_token + ":ERC20Mintable"]
77  return token_interface
78 
79 
81  web3: Any,
82  deployer_address: str,
83  deployer_private_key: Optional[bytes],
84  deployment_gas: Optional[int]) -> Any:
85  """
86  Deploy the testing ERC20 token contract
87  """
88  token_interface = compile_token()
89  token = web3.eth.contract(
90  abi=token_interface['abi'], bytecode=token_interface['bin'])
91  constructor_call = token.constructor()
92  tx_hash = send_contract_call(
93  web3=web3,
94  call=constructor_call,
95  sender_eth_addr=deployer_address,
96  sender_eth_private_key=deployer_private_key,
97  value=None,
98  gas=deployment_gas)
99  tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
100 
101  token = web3.eth.contract(
102  address=tx_receipt.contractAddress,
103  abi=token_interface['abi'],
104  )
105  return token
106 
107 
109  web3: Any,
110  token_instance: Any,
111  spender_address: str,
112  deployer_address: str,
113  deployer_private_key: Optional[bytes],
114  token_amount: EtherValue) -> bytes:
115  mint_call = token_instance.functions.mint(spender_address, token_amount.wei)
116  return send_contract_call(
117  web3=web3,
118  call=mint_call,
119  sender_eth_addr=deployer_address,
120  sender_eth_private_key=deployer_private_key)
121 
122 
123 if __name__ == "__main__":
124  deploy_test_token() # pylint: disable=no-value-for-parameter
zeth.cli.utils.get_eth_network
NetworkConfig get_eth_network(Optional[str] eth_network)
Definition: utils.py:84
test_commands.deploy_test_token.compile_token
Interface compile_token()
Definition: deploy_test_token.py:60
zeth.cli.utils.load_eth_address
str load_eth_address(Optional[str] eth_addr)
Definition: utils.py:444
zeth.core.utils.get_zeth_dir
str get_zeth_dir()
Definition: utils.py:249
zeth.core.constants
Definition: constants.py:1
zeth.cli.utils
Definition: utils.py:1
test_commands.deploy_test_token.mint_token
bytes mint_token(Any web3, Any token_instance, str spender_address, str deployer_address, Optional[bytes] deployer_private_key, EtherValue token_amount)
Definition: deploy_test_token.py:108
test_commands.deploy_test_token.deploy_token
Any deploy_token(Any web3, str deployer_address, Optional[bytes] deployer_private_key, Optional[int] deployment_gas)
Definition: deploy_test_token.py:80
zeth.core.contracts.compile_files
Any compile_files(List[str] files, **Any kwargs)
Definition: contracts.py:122
zeth.cli.constants
Definition: constants.py:1
zeth.core.utils
Definition: utils.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.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
test_commands.deploy_test_token.deploy_test_token
None deploy_test_token(Optional[str] eth_network, Optional[str] eth_addr, Optional[str] eth_private_key, int mint_amount, str recipient_address)
Definition: deploy_test_token.py:31