Skip to content

Instantly share code, notes, and snippets.

@jasparw
Last active April 17, 2024 16:51
Show Gist options
  • Save jasparw/adf94a75a4723d8b50a7d32ec75ac541 to your computer and use it in GitHub Desktop.
Save jasparw/adf94a75a4723d8b50a7d32ec75ac541 to your computer and use it in GitHub Desktop.
Coinpaprika gather and sort
'''There's four string variables needed to be entered for code to run
ENTER_DEX_URL
ENTER_API_KEY
ENTER_SOLANA_RPC_URL
ENTER_TEXT_FILE_NAME
enter these things and it should be fine.
Also remember to change the limit of coins that are pulled. I've set it to 10 for testing purposes, just remove it completely for a full run.
Full run might take quite a while depending on how many tokens are present. It has to gather all from coinpaprika individually and then run that through the rpc also individually to get decimals. around 600 from jupiter took about 7-10 minutes.
Remember to check output after for cases of contract address showing None or decimals showing unknown. This will happen when it can't find any info available
And, lastly, remember to install required libraries
requests and jsonrpcclient both need to be installed, rest are standard
use
pip install requests
pip install jsonrpcclient'''
import requests
import json
from jsonrpcclient import request, parse, Ok
import logging
dex_url = 'ENTER_DEX_URL' #enter the coinpaprika dex url. Use api-pro link.
# jup link - https://api-pro.coinpaprika.com/v1/exchanges/jupiter/markets
# raydium link - https://api-pro.coinpaprika.com/v1/exchanges/raydium/markets
get_indiviual_data = 'https://api-pro.coinpaprika.com/v1/coins/<id>'
api_key = 'ENTER_API_KEY' # enter coinpaprika pro api key here
solana_rpc_url = "ENTER_SOLANA_RPC_URL" # enter solana rpc, required to get decimals. I used quicknode, was free.
headers = {
'Authorization': f'{api_key}'
}
def get_token_list(dex_url):
response = requests.get(dex_url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(response.status_code)
def get_detailed_data(currency_ids):
detailed_data = []
for data in currency_ids:
url = get_indiviual_data.replace('<id>', data)
response = requests.get(url, headers=headers)
if response.status_code == 200:
detailed_data.append(response.json())
else:
print(f"Failed to get data for {data}, status code: {response.status_code}")
return detailed_data
def get_currency_ids():
coins = get_token_list(dex_url)
if coins is not None:
currency_ids = []
for coin in coins[:10]: #this specifies how many coins to iterate through for testing purposes. Leave empty to run fully. Set to 10 by default
id = coin.get('base_currency_id')
id2 = coin.get('quote_currency_id')
if id not in currency_ids:
currency_ids.append(id)
else:
if id2 not in currency_ids:
currency_ids.append(id2)
return currency_ids
def get_needed_info_from_detailed_data(detailed_data):
data = detailed_data
needed_info = []
for entry in data:
entry_data = {
'id': entry['id'],
'symbol': entry['symbol'],
'solana_contract': None
}
if 'contracts' in entry and isinstance(entry['contracts'], list):
for contract in entry['contracts']:
if contract['platform'] == 'sol-solana':
entry_data['solana_contract'] = contract['contract']
break
entry_data['blockchain'] = 'solana'
needed_info.append(entry_data)
return needed_info
def add_decimals(needed_info):
data = needed_info
for dec in data:
ca = dec['solana_contract']
response = requests.post(solana_rpc_url,json=request("getTokenSupply", params=([ca])))
if response.status_code == 200:
token_info = response.json()
try:
decimal = token_info['result']['value']['decimals']
dec['decimal'] = decimal
except:
continue
else:
'Unable to get decimals'
return data
def sort_data(data):
formatted = []
for entry in data:
token_id = entry['id']
blockchain = entry['blockchain']
symbol = entry['symbol']
contract_address = entry['solana_contract']
decimals = entry['decimal'] if 'decimal' in entry else 'unknown'
entry_str = f"('{token_id}','{blockchain}','{symbol}','{contract_address}',{decimals}),\n"
formatted.append(entry_str)
return formatted
def main():
currency_ids = get_currency_ids()
detailed_data = get_detailed_data(currency_ids)
needed_info = get_needed_info_from_detailed_data(detailed_data)
all_info = add_decimals(needed_info)
formatted_data = sort_data(all_info)
output = ''.join(formatted_data)
with open('ENTER_TEXT_FILE_NAME.txt', 'w') as f: #enter the name of the text file you want to save output to. remember to create one first and have it in same directory.
f.write(output)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment