I am trying to create an async code that listens for new message events in telegram, fetch some information from the message then call and API. The problem is that I receive incorrect responses from the API upon calling. The api verifies a specific contract address (a hash) and returns the source code of the contract if it is available.
from telethon import TelegramClient, events
import re
from config import CHAINS
import aiohttp
client = TelegramClient("telegram", api_id, api_hash)
def extract_info(input_string):
# Define regular expressions to extract information
chain_pattern = r'New (.*?) token'
token_name_pattern = r'Basescan: (.*?)n'
supply_pattern = r'Total supply: (.*?)n'
symbol_pattern = r'Symbol: (.*?)n'
contract_pattern = r'Contract: (.*?)n'
balance_pattern = r'Deployer balance: (.*?)n'
# Initialize the dictionary to store extracted information
final = {}
# Extract information using regular expressions
final['chain'] = re.search(chain_pattern, input_string).group(1)
final['token_name'] = re.search(token_name_pattern, input_string).group(1)
final['supply'] = re.search(supply_pattern, input_string).group(1)
final['token_symbol'] = re.search(symbol_pattern, input_string).group(1)
final['contract'] = re.search(contract_pattern, input_string).group(1)
final['deployer_balance'] = re.search(balance_pattern, input_string).group(1)
return final
@client.on(events.NewMessage(chats=["basetokencreations"]))
async def handler(event):
msg = event.message
final = extract_info(msg.message)
query_params = {
"module": "contract",
"action": "getsourcecode",
"address": final['contract'],
"apikey": CHAINS[chain]['API_KEY'],
}
async with aiohttp.ClientSession() as session:
async with session.get(CHAINS[chain]["ENDPOINT"], params=query_params) as response:
token_contract_source_code = await response.json()
print(final, token_contract_source_code)
if __name__ == "__main__":
chain="base"
client.start()
client.run_until_disconnected()
Upon running this code, like 50% of contract with available code, returns wrong results:
{'status': '1', 'message': 'OK', 'result': [{'SourceCode': '', 'ABI': 'Contract source code not verified', 'ContractName': '', 'CompilerVersion': '', 'OptimizationUsed': '', 'Runs': '', 'ConstructorArguments': '', 'EVMVersion': 'Default', 'Library': '', 'LicenseType': 'Unknown', 'Proxy': '0', 'Implementation': '', 'SwarmSource': ''}]}
If I run the same contract and hit the same api with the same parameters in synchronous mode, results are always correct. What is going on?