Code Helpers
Contains main business logic for fetching essential code data
Here are the snippets for code helpers that contain the main logic that fetches and updates the data for the code endpoints:
1. code_metrics.py
code_metrics.py
async def get_current_user_weights(wallet_address):
wallet_address = w3.to_checksum_address(wallet_address)
data = await distribution_contract.functions.usersData(wallet_address, 1).call()
weights = int(data[1])
return weights
async def process_batch(batch):
tasks = [get_current_user_weights(row) for row in batch]
return await asyncio.gather(*tasks)
async def get_active_code_contributors(contributors_list):
batch_size = 50
active_contributors = []
total_batches = len(contributors_list) // batch_size + (1 if len(contributors_list) % batch_size else 0)
for i in range(0, len(contributors_list), batch_size):
batch = contributors_list[i:i + batch_size]
logger.info(f"Processing batch {i // batch_size + 1}/{total_batches}")
try:
weights = await process_batch(batch)
for address, weight in zip(batch, weights):
if weight > 0:
active_contributors.append({
'address': address,
'weight': weight
})
except Exception as e:
logger.error(f"Error processing batch: {str(e)}")
continue
return len(active_contributors)
async def get_total_weights_and_contributors():
try:
user_staked_df = get_data_from_db(USER_STAKED_NAME)
except Exception as e:
logger.error(f"Error reading sheets: {str(e)}")
return OrderedDict(), pd.DataFrame()
required_columns = ["Timestamp", "TransactionHash", "BlockNumber", "PoolId", "User", "Amount"]
if not all(col in user_staked_df.columns for col in required_columns):
logger.error(f"DataFrame is missing required columns: {required_columns}")
return OrderedDict(), pd.DataFrame()
try:
user_staked_df["PoolId"] = pd.to_numeric(user_staked_df["PoolId"], errors="coerce")
user_staked_df["Amount"] = pd.to_numeric(user_staked_df["Amount"], errors="coerce")
valid_data = user_staked_df.dropna(subset=["PoolId", "Amount"])
contributor_addresses_list = valid_data[valid_data['PoolId'] == 1]['User'].unique()
unique_contributors = valid_data[valid_data['PoolId'] == 1]['User'].nunique()
total_weights = valid_data[valid_data['PoolId'] == 1]['Amount'].sum()
json_output = {
"total_weights_assigned": total_weights,
"unique_contributors": unique_contributors,
"active_contributors": await get_active_code_contributors(contributor_addresses_list)
}
return json_output
except Exception as e:
logger.exception(f"Error processing data: {str(e)}")
return OrderedDict(), pd.DataFrame()
asyncio.run(get_total_weights_and_contributors())