Spaces:
Sleeping
Sleeping
import os | |
import json | |
import requests | |
import pandas as pd | |
from dotenv import load_dotenv | |
# Load environment variables | |
load_dotenv() | |
ETHERSCAN_API_KEY = os.getenv("API_KEY_ETHERSCAN") | |
TOKENS_TO_WATCH = "ressources/dict_tokens_addr.json" # Path to the file with tokens to watch | |
INFLUENCERS_FILE = "ressources/dict_influencers_addr.json" # Path to the file with influencers' addresses | |
def fetch_token_balance(address, token_contract): | |
url = f"https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress={token_contract}&address={address}&tag=latest&apikey={ETHERSCAN_API_KEY}" | |
response = requests.get(url) | |
if response.status_code == 200: | |
data = response.json() | |
if data["status"] == "1": | |
balance = int(data["result"]) | |
return balance | |
else: | |
print(f"Error fetching token balance: {data.get('message')}") | |
else: | |
print(f"Error: HTTP {response.status_code}") | |
return 0 | |
def main(): | |
# Load tokens and influencers | |
with open(TOKENS_TO_WATCH, "r") as file: | |
tokens_to_watch = json.load(file) | |
with open(INFLUENCERS_FILE, "r") as file: | |
influencers = json.load(file) | |
balances = [] | |
for name, address in influencers.items(): | |
for token_name, token_contract in tokens_to_watch.items(): | |
balance = fetch_token_balance(address, token_contract) | |
if balance > 0: # Optionally check if balance is above a certain threshold | |
balances.append({ | |
"Influencer": name, | |
"Token": token_name, | |
"Balance": balance / (10 ** 18) # Convert from Wei | |
}) | |
# Save to CSV | |
df = pd.DataFrame(balances) | |
df.to_csv("output/influencers_token_balances.csv", index=False) | |
if __name__ == "__main__": | |
main() | |