Spaces:
No application file
No application file
Download src/common/web3.py from metasoccer/collab: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/metasoccer/collab/resolve/main/src/common/web3.py
- Command line
-
hf download hf://spaces/metasoccer/collab/src/common/web3.py
-
curl -L -o web3.py https://huggingface.co/spaces/metasoccer/collab/resolve/main/src/common/web3.py
3.13 kB
| import os | |
| import time | |
| from datetime import datetime | |
| import requests | |
| from eth_abi import decode | |
| from eth_utils import function_abi_to_4byte_selector | |
| from web3 import Web3 | |
| api_key = os.environ["POLYGONSCAN_API_KEY"] | |
| w3 = Web3() | |
| def decode_transaction_input(input_data: str, abi: list) -> dict: | |
| if not input_data or not abi: | |
| return None | |
| # Get the function signature hash | |
| function_signature_hash = input_data[:10] | |
| # Find the ABI entry for the function | |
| function_abi = next( | |
| ( | |
| entry | |
| for entry in abi | |
| if entry["type"] == "function" | |
| and w3.keccak( | |
| text=f"{entry['name']}({','.join([arg['type'] for arg in entry['inputs']])})" | |
| ).hex()[:10] | |
| == function_signature_hash | |
| ), | |
| None, | |
| ) | |
| if function_abi: | |
| # Decode the input data | |
| types = [input["type"] for input in function_abi["inputs"]] | |
| decoded = decode(types, bytes.fromhex(input_data[10:])) | |
| return dict(zip([input["name"] for input in function_abi["inputs"]], decoded)) | |
| else: | |
| return None | |
| def fetch_transactions( | |
| contract_address: str, | |
| method_id, | |
| method_abi, | |
| start_block: int, | |
| end_block: int, | |
| ) -> list: | |
| url = "https://api.polygonscan.com/api" | |
| all_transactions = [] | |
| while start_block <= end_block: | |
| params = { | |
| "module": "account", | |
| "action": "txlist", | |
| "address": contract_address, | |
| "startblock": start_block, | |
| "endblock": end_block, | |
| "sort": "asc", | |
| "apikey": api_key, | |
| } | |
| response = requests.get(url, params=params) | |
| if response.status_code == 200: | |
| transactions = response.json().get("result", []) | |
| if not transactions: | |
| break # Break if no more transactions | |
| for tx in transactions: | |
| if tx.get("methodId", "") == method_id: | |
| tx["decoded_input"] = decode_transaction_input( | |
| tx.get("input"), method_abi | |
| ) | |
| all_transactions.append(tx) | |
| # Update start_block for next iteration | |
| start_block = int(transactions[-1]["blockNumber"]) + 1 | |
| else: | |
| print("Failed to fetch data:", response.status_code) | |
| break | |
| return all_transactions | |
| def get_current_block_number() -> int: | |
| current_timestamp = int(time.mktime(datetime.now().timetuple())) | |
| url = "https://api.polygonscan.com/api" | |
| params = { | |
| "module": "block", | |
| "action": "getblocknobytime", | |
| "timestamp": current_timestamp, | |
| "closest": "before", | |
| "apikey": api_key, | |
| } | |
| response = requests.get(url, params=params) | |
| if response.status_code == 200: | |
| data = response.json() | |
| return data.get("result") | |
| else: | |
| print("Failed to fetch current block number:", response.status_code) | |
| return None | |
| def get_method_id(function_abi): | |
| method_id = function_abi_to_4byte_selector(function_abi) | |
| return "0x" + method_id.hex() | |