-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipt.py
49 lines (41 loc) · 1.64 KB
/
receipt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from .oracles import get_token_price_cg
from dataclasses import dataclass
from hexbytes import HexBytes
@dataclass
class TransactionReceipt:
"""
Dataclass to model the important receipt return parameters from the Web3.eth.wait_for_transaction_receipt()
https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.wait_for_transaction_receipt
"""
transactionHash: HexBytes # Access the string by using transactionHash.hex()
blockHash: HexBytes # Access the string by using blockHash.hex()
blockNumber: int
contractAddress: str
cumulativeGasUsed: int
effectiveGasPrice: int
gasSpendUSD: str
fromAddress: str
toAddress: str
status: int
transactionIndex: int
type: str
def build_receipt(d: dict) -> TransactionReceipt:
"""
Prepare the Web3 transaction receipt dictionary to map to the TransactionReceipt class
:param d: Transaction receipt JSON data as returned by Web3.eth.wait_for_transaction_receipt():
https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.wait_for_transaction_receipt
:return: TransactionReceipt class to model the transaction
"""
# Account for "from" being unable to map:
if "from" in d.keys():
d['fromAddress'] = d.pop("from")
if "to" in d.keys():
d['toAddress'] = d.pop("to")
# Calculate gas price in USD
if "gasUsed" in d.keys():
d['gasSpendUSD'] = (d.pop('gasUsed') * 0.000000001) * get_token_price_cg("ETH") # ETH per GWEI
# Clean logs - will not be used:
for key in d.copy().keys():
if key.startswith("logs"):
d.pop(key)
return TransactionReceipt(**d)