

import requests


 
from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)

old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
 
 

print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
 
url = "https://solana-mainnet.g.alchemy.com/v2/WLI0ohbUlvbsJVmoIvB1wTSwUA8qP5qS"

getBalance_payload = {
    "id": 1,
    "jsonrpc": "2.0",
    "method": "getBalance",
    "params": []
}
headers = {
    "accept": "application/json",
    "content-type": "application/json"
}
getTokenAccountsByOwner_USDC_payload = {
    "id": 1,
    "jsonrpc": "2.0",
    "method": "getTokenAccountsByOwner",
    "params": ["" ,
                { "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" }, 
                { "encoding": "jsonParsed" }
               ]
}

getTokenAccountsByOwner_USDT_payload = {
    "id": 1,
    "jsonrpc": "2.0",
    "method": "getTokenAccountsByOwner",
    "params": ["" ,
                { "mint": "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" }, 
                { "encoding": "jsonParsed" }
               ]
}





df =  pd.read_excel( baseclass.dalao_merge_defined_path / f"ori_merge_defined.xlsx", dtype=object)
df= df.astype({
    "eth":float,
    "usdc":float,
    "usdt":float,
})
df = df.drop_duplicates(subset=["dalaoaddress"])
df =df.reset_index(drop=True)

df_not_getbalance =df[   (df["eth"]<0.5) & ( df["usdc"]==-1 ) ]
arr_str_dalaoaddress = df_not_getbalance['dalaoaddress'].tolist()
 
for idx , str_dalaoaddress in  enumerate(arr_str_dalaoaddress):
    print(f"str_dalaoaddress= {str_dalaoaddress}")
    getTokenAccountsByOwner_USDC_payload["params"][0] =str_dalaoaddress
    response = requests.post(url, json=getTokenAccountsByOwner_USDC_payload, headers=headers)

    if response.status_code != 200:
        continue
        
    response_data = response.json()
    # print(response_data)
    usdc_value = -1
    if len(response_data["result"]["value"])==0:
        usdc_value = 0
    elif len(response_data["result"]["value"])==1:
        usdc_value = response_data["result"]["value"][0]["account"]["data"]["parsed"]["info"]["tokenAmount"]["uiAmount"]  
    elif len(response_data["result"]["value"])>1:
        usdc_value = -1
        
    df.loc[ df["dalaoaddress"] ==str_dalaoaddress , "usdc"] = usdc_value
    print(f"str_dalaoaddress= {str_dalaoaddress} usdc_value= {usdc_value}")
    if idx%5==0:
        df.to_excel( baseclass.dalao_merge_defined_path / f"ori_merge_defined.xlsx", index=False)
   
 
 
df.to_excel( baseclass.dalao_merge_defined_path / f"ori_merge_defined.xlsx", index=False)

 
df_not_getbalance = df[(df["eth"]<0.5)& ( df["usdc"] <100 )&(df["usdt"] ==-1 ) ]
arr_str_dalaoaddress = df_not_getbalance['dalaoaddress'].tolist()
 
print(f"arr_str_dalaoaddress_len= {len(arr_str_dalaoaddress)}")
 
for idx , str_dalaoaddress in  enumerate(arr_str_dalaoaddress):
    print(f"str_dalaoaddress= {str_dalaoaddress}")
    getTokenAccountsByOwner_USDT_payload["params"][0] =str_dalaoaddress
    response = requests.post(url, json=getTokenAccountsByOwner_USDT_payload, headers=headers)

    if response.status_code != 200:
        continue
        
    response_data = response.json()
 
    usdt_value = -1
    if len(response_data["result"]["value"])==0:
        usdt_value = 0
    elif len(response_data["result"]["value"])==1:
        usdt_value = response_data["result"]["value"][0]["account"]["data"]["parsed"]["info"]["tokenAmount"]["uiAmount"]  
    elif len(response_data["result"]["value"])>1:
        usdt_value = -1
        
    df.loc[ df["dalaoaddress"] ==str_dalaoaddress , "usdt"] = usdt_value
    print(f"str_dalaoaddress= {str_dalaoaddress} usdt_value={usdt_value}")
    if idx%5==0:
        df.to_excel( baseclass.dalao_merge_defined_path / f"ori_merge_defined.xlsx", index=False)
   
 
 
df.to_excel( baseclass.dalao_merge_defined_path / f"ori_merge_defined.xlsx", index=False)

 
 