-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdexscreener.py
68 lines (51 loc) · 2.38 KB
/
dexscreener.py
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
import requests, json, os, sys
def getBaseToken(token_address):
url = f"https://api.dexscreener.com/latest/dex/pairs/solana/{token_address}"
response = requests.get(url).json()
return response['pair']['baseToken']['address']
def get_price(token_address):
"""
USDT and USDC prices will be excluded
"""
url = f"https://api.dexscreener.com/latest/dex/tokens/{token_address}"
exclude = ['EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB']
response = requests.get(url).json()
if token_address not in exclude:
for pair in response['pairs']:
if pair['quoteToken']['address'] == 'So11111111111111111111111111111111111111112':
return float(pair['priceUsd'])
else:
return response['pairs'][0]['priceUsd']
return None
"""Common addresses like usdc and usdt will be excluded as we know their symbols"""
def getSymbol(token):
# usdc and usdt
exclude = ['EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB']
if token not in exclude:
url = f"https://api.dexscreener.com/latest/dex/tokens/{token}"
Token_Symbol = ""
Sol_symbol=""
try:
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
resp = response.json()
print("Response:",resp['pairs'][0]['baseToken']['symbol'])
for pair in resp['pairs']:
quoteToken = pair['quoteToken']['symbol']
if quoteToken == 'SOL':
Token_Symbol = pair['baseToken']['symbol']
Sol_symbol = quoteToken
return Token_Symbol, Sol_symbol
else:
print(f"[getSymbol] Request failed with status code {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"[getSymbol] error occurred: {e}")
except:
a = 1
return Token_Symbol, Sol_symbol
else:
if token == 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v':
return "USDC", "SOL"
elif token == 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v':
return "USDT", "SOL"