Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
Classes | Functions | Variables
zeth.core.contracts Namespace Reference

Classes

class  InstanceDescription
 

Functions

int get_block_number (Any web3)
 
None install_sol ()
 
Any compile_files (List[str] files, **Any kwargs)
 
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)
 
Any local_contract_call (Any call, str sender_eth_addr, Optional[EtherValue] value=None, Optional[int] gas=None)
 
Iterator[Any] get_event_logs (Any web3, Any instance, str event_name, int start_block, int end_block, Optional[int] batch_size)
 
Iterator[Any] get_event_logs_from_tx_receipt (Any instance, str event_name, Any tx_receipt)
 

Variables

int SYNC_BLOCKS_PER_BATCH = 1000
 
 Interface = Dict[str, Any]
 

Function Documentation

◆ compile_files()

Any zeth.core.contracts.compile_files ( List[str]  files,
**Any  kwargs 
)
Wrapper around solcx which ensures the required version of the compiler is
used.

Definition at line 122 of file contracts.py.

122 def compile_files(files: List[str], **kwargs: Any) -> Any:
123  """
124  Wrapper around solcx which ensures the required version of the compiler is
125  used.
126  """
127  solcx.set_solc_version(SOL_COMPILER_VERSION)
128  return solcx.compile_files(files, optimize=True, **kwargs)
129 
130 
Here is the caller graph for this function:

◆ get_block_number()

int zeth.core.contracts.get_block_number ( Any  web3)

Definition at line 114 of file contracts.py.

114 def get_block_number(web3: Any) -> int:
115  return web3.eth.blockNumber
116 
117 
Here is the caller graph for this function:

◆ get_event_logs()

Iterator[Any] zeth.core.contracts.get_event_logs ( Any  web3,
Any  instance,
str  event_name,
int  start_block,
int  end_block,
Optional[int]  batch_size 
)
Query the attached node for all events emitted by the given contract
instance, with the given name. Yields an iterator of event-specific objects
to be decoded by the caller.

Definition at line 179 of file contracts.py.

179 def get_event_logs(
180  web3: Any,
181  instance: Any,
182  event_name: str,
183  start_block: int,
184  end_block: int,
185  batch_size: Optional[int]) -> Iterator[Any]:
186  """
187  Query the attached node for all events emitted by the given contract
188  instance, with the given name. Yields an iterator of event-specific objects
189  to be decoded by the caller.
190  """
191 
192  # It is possible to achieve this via the contract interface, with code of
193  # the form:
194  #
195  # event = instance.events[event_name]
196  # filter = event.createFilter(fromBlock=start_block, toBlock=to_block)
197  # logs = web3.eth.getFilterLogs(filter)
198  #
199  # However, this creates filters on the host node, which may not be
200  # permitted in all configurations. Hence, the code here iterates manually,
201  # skpping events with other topics, from the same contract.
202 
203  contract_address = instance.address
204  contract_event = instance.events[event_name]()
205 
206  event_abi = find_matching_event_abi(instance.abi, event_name=event_name)
207  log_topic = event_abi_to_log_topic(cast(Dict[str, Any], event_abi))
208 
209  batch_size = batch_size or SYNC_BLOCKS_PER_BATCH
210  while start_block <= end_block:
211  # Filters are *inclusive* wrt "toBlock", hence the -1 here, and +1 to
212  # set start_block before iterating.
213  to_block = min(start_block + batch_size - 1, end_block)
214  filter_params = {
215  'fromBlock': start_block,
216  'toBlock': to_block,
217  'address': contract_address,
218  }
219  logs = web3.eth.getLogs(filter_params)
220  for log in logs:
221  if log_topic == log['topics'][0]:
222  yield contract_event.processLog(log)
223  start_block = to_block + 1
224 
225 

◆ get_event_logs_from_tx_receipt()

Iterator[Any] zeth.core.contracts.get_event_logs_from_tx_receipt ( Any  instance,
str  event_name,
Any  tx_receipt 
)
Query a transaction receipt for all events emitted by the given contract
instance with a given event name. Yields an iterator of event-specific
objects to be decoded by the caller. This function intentionally avoids
connecting to a node, or creating host-side filters.

Definition at line 226 of file contracts.py.

227  instance: Any,
228  event_name: str,
229  tx_receipt: Any) -> Iterator[Any]:
230  """
231  Query a transaction receipt for all events emitted by the given contract
232  instance with a given event name. Yields an iterator of event-specific
233  objects to be decoded by the caller. This function intentionally avoids
234  connecting to a node, or creating host-side filters.
235  """
236  contract_address = instance.address
237  contract_event = instance.events[event_name]()
238 
239  event_abi = find_matching_event_abi(instance.abi, event_name=event_name)
240  log_topic = event_abi_to_log_topic(cast(Dict[str, Any], event_abi))
241  for log in tx_receipt.logs:
242  if log.address == contract_address and log_topic == log['topics'][0]:
243  yield contract_event.processLog(log)
Here is the caller graph for this function:

◆ install_sol()

None zeth.core.contracts.install_sol ( )

Definition at line 118 of file contracts.py.

118 def install_sol() -> None:
119  solcx.install_solc(SOL_COMPILER_VERSION)
120 
121 

◆ local_contract_call()

Any zeth.core.contracts.local_contract_call ( Any  call,
str  sender_eth_addr,
Optional[EtherValue]   value = None,
Optional[int]   gas = None 
)
Make a contract call locally on the RPC host and return the result. Does
not create a transaction.

Definition at line 162 of file contracts.py.

163  call: Any,
164  sender_eth_addr: str,
165  value: Optional[EtherValue] = None,
166  gas: Optional[int] = None) -> Any:
167  """
168  Make a contract call locally on the RPC host and return the result. Does
169  not create a transaction.
170  """
171  tx_desc: Dict[str, Union[str, int]] = {'from': sender_eth_addr}
172  if value:
173  tx_desc["value"] = value.wei
174  if gas:
175  tx_desc["gas"] = gas
176  return call.call(tx_desc)
177 
178 

◆ send_contract_call()

bytes zeth.core.contracts.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 
)
Broadcast a transaction for a contract call, handling the difference
between hosted keys (sender_eth_private_key is None) and local keys
(sender_eth_private_key is not None). Returns the hash of the broadcast
transaction.

Definition at line 131 of file contracts.py.

132  web3: Any,
133  call: Any,
134  sender_eth_addr: str,
135  sender_eth_private_key: Optional[bytes] = None,
136  value: Optional[EtherValue] = None,
137  gas: Optional[int] = None) -> bytes:
138  """
139  Broadcast a transaction for a contract call, handling the difference
140  between hosted keys (sender_eth_private_key is None) and local keys
141  (sender_eth_private_key is not None). Returns the hash of the broadcast
142  transaction.
143  """
144  tx_desc: Dict[str, Union[str, int]] = {'from': sender_eth_addr}
145  if value:
146  tx_desc["value"] = value.wei
147  if gas:
148  tx_desc["gas"] = gas
149  if sender_eth_private_key:
150  tx_desc["gasPrice"] = web3.eth.gasPrice
151  tx_desc["nonce"] = web3.eth.getTransactionCount(sender_eth_addr)
152  transaction = call.buildTransaction(tx_desc)
153  signed_tx = web3.eth.account.signTransaction(
154  transaction, sender_eth_private_key)
155  print(f"send_contract_call: size={len(signed_tx.rawTransaction)}")
156  return web3.eth.sendRawTransaction(signed_tx.rawTransaction)
157 
158  # Hosted path
159  return call.transact(tx_desc)
160 
161 
Here is the caller graph for this function:

Variable Documentation

◆ Interface

zeth.core.contracts.Interface = Dict[str, Any]

Definition at line 18 of file contracts.py.

◆ SYNC_BLOCKS_PER_BATCH

int zeth.core.contracts.SYNC_BLOCKS_PER_BATCH = 1000

Definition at line 16 of file contracts.py.

zeth.core.contracts.install_sol
None install_sol()
Definition: contracts.py:118
zeth.core.contracts.compile_files
Any compile_files(List[str] files, **Any kwargs)
Definition: contracts.py:122
zeth.core.contracts.get_block_number
int get_block_number(Any web3)
Definition: contracts.py:114
zeth.core.contracts.get_event_logs_from_tx_receipt
Iterator[Any] get_event_logs_from_tx_receipt(Any instance, str event_name, Any tx_receipt)
Definition: contracts.py:226
zeth.core.contracts.get_event_logs
Iterator[Any] get_event_logs(Any web3, Any instance, str event_name, int start_block, int end_block, Optional[int] batch_size)
Definition: contracts.py:179
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
zeth.core.contracts.local_contract_call
Any local_contract_call(Any call, str sender_eth_addr, Optional[EtherValue] value=None, Optional[int] gas=None)
Definition: contracts.py:162