forked from fgandila/snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_guild_unbound_tokens.py
More file actions
117 lines (93 loc) · 4.06 KB
/
Copy pathupdate_guild_unbound_tokens.py
File metadata and controls
117 lines (93 loc) · 4.06 KB
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
111
112
113
114
115
116
117
#!/usr/bin/env python3
import json
import sys
from pathlib import Path
from typing import Dict, Any
from multiversx_sdk import Address
from utils.contract_data_fetchers import DataFetcher
from utils.utils_chain import hex_to_string, WrapperAddress
from utils.logger import get_logger
from config import DEFAULT_PROXY
logger = get_logger(__name__)
class GuildContractDataFetcher(DataFetcher):
"""Data fetcher for guild contracts"""
def __init__(self, contract_address: WrapperAddress, proxy_url: str):
super().__init__(contract_address, proxy_url)
self.view_handler_map = {
"getUnbondTokenId": self._get_hex_view,
}
def load_guild_data(file_path: str) -> Dict[str, Any]:
"""Load the existing guild data from JSON file"""
try:
with open(file_path, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Error loading guild data: {e}")
return {}
def save_guild_data(file_path: str, data: Dict[str, Any]) -> bool:
"""Save the updated guild data to JSON file"""
try:
with open(file_path, 'w') as f:
json.dump(data, f, indent=2)
return True
except Exception as e:
logger.error(f"Error saving guild data: {e}")
return False
def get_unbound_token_for_guild(guild_address: str, proxy_url: str) -> str:
"""Query a guild address for its unbound token ID"""
try:
# Skip if guild_address is the same as the token identifier (invalid address)
if guild_address.startswith("UTKFARM-"):
logger.warning(f"Invalid guild address (token identifier): {guild_address}")
return ""
logger.info(f"Querying guild address: {guild_address}")
# Create guild data fetcher
data_fetcher = GuildContractDataFetcher(WrapperAddress(guild_address), proxy_url)
# Get unbound token ID as hex
unbound_token_hex = data_fetcher.get_data("getUnbondTokenId")
if unbound_token_hex:
# Convert hex to readable token identifier
unbound_token = hex_to_string(unbound_token_hex)
logger.info(f"Found unbound token: {unbound_token} for guild {guild_address}")
return unbound_token
else:
logger.warning(f"No unbound token found for guild {guild_address}")
return ""
except Exception as e:
logger.error(f"Error querying guild {guild_address}: {e}")
return ""
def update_guild_unbound_tokens():
"""Main function to update all guild entries with unbound tokens"""
guild_data_file = "utk_guilds_data.json"
proxy_url = DEFAULT_PROXY
logger.info("Starting guild unbound token update process")
# Load existing guild data
guild_data = load_guild_data(guild_data_file)
if not guild_data:
logger.error("Failed to load guild data")
return False
updated_count = 0
# Process each guild
for token_id, guild_info in guild_data.items():
guild_address = guild_info.get("guild_address", "")
if not guild_address:
logger.warning(f"No guild address found for {token_id}")
continue
logger.info(f"Processing {token_id} with guild address {guild_address}")
# Get unbound token for this guild
unbound_token = get_unbound_token_for_guild(guild_address, proxy_url)
# Update the guild info with unbound token
guild_info["unbound_token"] = unbound_token
updated_count += 1
logger.info(f"Updated {token_id}: unbound_token = '{unbound_token}'")
# Save updated data
if save_guild_data(guild_data_file, guild_data):
logger.info(f"Successfully updated {updated_count} guild entries")
print(f"✅ Updated {updated_count} guild entries with unbound token information")
return True
else:
logger.error("Failed to save updated guild data")
return False
if __name__ == "__main__":
success = update_guild_unbound_tokens()
sys.exit(0 if success else 1)