Spaces:
No application file

File size: 3,127 Bytes
c4c8ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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()