|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import json |
| 4 | +from decimal import Decimal |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +API_KEY = os.getenv("ALCHEMY_API_KEY") |
| 8 | +BASE_URL = f"https://eth-mainnet.g.alchemy.com/v2/{API_KEY}" |
| 9 | +HEADERS = {"accept": "application/json", "content-type": "application/json"} |
| 10 | + |
| 11 | + |
| 12 | +def set_api_key(key: str): |
| 13 | + """ |
| 14 | + params: |
| 15 | + key: API key |
| 16 | + returns: |
| 17 | + None |
| 18 | + """ |
| 19 | + os.environ["ALCHEMY_API_KEY"] = key |
| 20 | + |
| 21 | + |
| 22 | +def get_current_block() -> int: |
| 23 | + """ |
| 24 | + params: |
| 25 | + None |
| 26 | + returns: |
| 27 | + the current max block (INT) |
| 28 | + """ |
| 29 | + payload = {"id": 1, "jsonrpc": "2.0", "method": "eth_blockNumber"} |
| 30 | + response = requests.post(BASE_URL, json=payload, headers=HEADERS) |
| 31 | + response = json.loads(response.text) |
| 32 | + result = int(response["result"], 16) |
| 33 | + return result |
| 34 | + |
| 35 | + |
| 36 | +def get_asset_transfers(node_address: str, block_start: int, block_end: int) -> list: |
| 37 | + """ |
| 38 | + params: |
| 39 | + node_address: wallet address associated with the OCR node |
| 40 | + block_start: INT |
| 41 | + block_end: INT |
| 42 | + returns: A dictionary, internal/external asset transfers for the address |
| 43 | + """ |
| 44 | + from_block = str(hex(block_start)) |
| 45 | + to_block = str(hex(block_end)) |
| 46 | + address = node_address.lower() |
| 47 | + payload = { |
| 48 | + "id": 1, |
| 49 | + "jsonrpc": "2.0", |
| 50 | + "method": "alchemy_getAssetTransfers", |
| 51 | + "params": [ |
| 52 | + { |
| 53 | + "fromBlock": from_block, |
| 54 | + "toBlock": to_block, |
| 55 | + "fromAddress": address, |
| 56 | + "category": ["external", "internal"], |
| 57 | + "excludeZeroValue": False, |
| 58 | + } |
| 59 | + ], |
| 60 | + } |
| 61 | + response = requests.post(BASE_URL, json=payload, headers=HEADERS) |
| 62 | + response = json.loads(response.text) |
| 63 | + result = response["result"] |
| 64 | + transfers = result["transfers"] |
| 65 | + return transfers |
| 66 | + |
| 67 | + |
| 68 | +def get_transaction_receipt(transaction_hash: str): |
| 69 | + """ |
| 70 | + params: |
| 71 | + transaction_hash: transaction hash to search for |
| 72 | + returns: |
| 73 | + transaction receipt data |
| 74 | + """ |
| 75 | + payload = { |
| 76 | + "id": 1, |
| 77 | + "jsonrpc": "2.0", |
| 78 | + "method": "eth_getTransactionReceipt", |
| 79 | + "params": [transaction_hash], |
| 80 | + } |
| 81 | + response = requests.post(BASE_URL, json=payload, headers=HEADERS) |
| 82 | + response = json.loads(response.text) |
| 83 | + result = response["result"] |
| 84 | + return result |
| 85 | + |
| 86 | + |
| 87 | +def get_events(contract_address, event_signature, block_start, block_end) -> dict: |
| 88 | + """ |
| 89 | + params: |
| 90 | + contract_address: LIST off addresses for which to search for events |
| 91 | + event_signature: signature of the event to search for (TOPIC 0) |
| 92 | + block_start: INT |
| 93 | + block_end: INT |
| 94 | + returns: A dictionary, result[block] = block_date |
| 95 | + """ |
| 96 | + from_block = str(hex(block_start)) |
| 97 | + to_block = str(hex(block_end)) |
| 98 | + payload = { |
| 99 | + "id": 1, |
| 100 | + "jsonrpc": "2.0", |
| 101 | + "method": "eth_getLogs", |
| 102 | + "params": [ |
| 103 | + { |
| 104 | + "address": contract_address, |
| 105 | + "fromBlock": from_block, |
| 106 | + "toBlock": to_block, |
| 107 | + "topics": [event_signature], |
| 108 | + } |
| 109 | + ], |
| 110 | + } |
| 111 | + response = requests.post(BASE_URL, json=payload, headers=HEADERS) |
| 112 | + response = json.loads(response.text) |
| 113 | + result = response.get("result") |
| 114 | + return result |
| 115 | + |
| 116 | + |
| 117 | +def get_block_datetime(blocks=None, block_start=None, block_end=None) -> dict: |
| 118 | + """ |
| 119 | + params: |
| 120 | + block_start as an INT |
| 121 | + block_end as an INT |
| 122 | + returns: |
| 123 | + A dictionary, result[block] = block_date |
| 124 | + """ |
| 125 | + blocks = list(range(block_start, block_end)) if blocks is None else blocks |
| 126 | + result = {} |
| 127 | + for block in blocks: |
| 128 | + payload = { |
| 129 | + "id": 1, |
| 130 | + "jsonrpc": "2.0", |
| 131 | + "method": "eth_getBlockByNumber", |
| 132 | + "params": [hex(block), False], |
| 133 | + } |
| 134 | + response = requests.post(BASE_URL, json=payload, headers=HEADERS) |
| 135 | + result_raw = json.loads(response.text)["result"] |
| 136 | + block = int(result_raw["number"], 16) |
| 137 | + block_date = datetime.fromtimestamp(int(result_raw["timestamp"], 16)) |
| 138 | + result[block] = block_date |
| 139 | + return result |
0 commit comments