-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtx_get_details.py
executable file
·79 lines (61 loc) · 1.95 KB
/
tx_get_details.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
#
# This script analyzes transaction stats by sampling a random subset of tx
#
import os
import random
from web3 import Web3
URL = os.getenv("ALCHEMY_URL")
web3 = Web3(Web3.WebsocketProvider(URL))
NUM_TX = 20000
YEAR = os.getenv("YEAR")
if YEAR is None or len(YEAR) == 0:
YEAR = "2023"
POOL = os.getenv("POOL")
if POOL is None or len(POOL) == 0:
POOL = "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc" # v2 USDC/ETH
POOL = POOL.lower()
DECIMALS = os.getenv("DECIMALS")
if DECIMALS is None or len(DECIMALS) == 0:
DECIMALS = 6 # for USDC
DECIMALS = int(DECIMALS)
VERSION = os.getenv("VERSION")
if VERSION is None or len(VERSION) == 0:
VERSION = 2
print(f"using pool {POOL} on Uniswap v{VERSION}, year {YEAR}, token0 decimals {DECIMALS}")
def load_csv(filename):
result = []
with open(filename) as f:
f.readline() # skip the header
for line in f.readlines():
fields = line.strip().split(",")
if len(fields) == 0:
continue
result.append(fields)
return result
def tx_get_details(hash):
receipt = web3.eth.get_transaction_receipt(hash)
dst = receipt["to"]
sender = receipt["from"]
gas = receipt.gasUsed
gas_price = receipt.effectiveGasPrice
cost = web3.from_wei(gas * gas_price, "ether")
print(dst, gas)
return dst, sender, gas, gas_price, cost, hash
def main():
random.seed(12345) # make it repeatable
filename = f"tx-v{VERSION}-{YEAR}-{POOL}.csv"
txs = load_csv(filename)
tx_subset = random.sample(txs, NUM_TX)
with open(f"tx-details-v{VERSION}-{YEAR}-{POOL}.csv", "w") as outf:
for tx, in tx_subset:
try:
result = tx_get_details(tx)
except Exception as ex:
print("Exception:", ex)
continue
s = ",".join([str(u) for u in result])
outf.write(s)
outf.write("\n")
if __name__ == "__main__":
main()