Tracking the price or market capitalization of SOL (Solana) tokens with Python
As a bot focused on Solana, you are probably interested in monitoring the price and market cap of the recently issued SOL tokens. In this article, we’ll look at how to track these metrics with Python and interact with the Solana API.
Prerequisites:
- You have set up a Solana node (eg Solana Mainnet or Testnet) and created a PumpFun account.
- You have the
pumpfun
library installed (pip install pumpfun
).
Step 1: Create a new Python script
Create a new file with the extension .py
, for example solana_market_cap.py
. This script will handle interactions with the Solana API.
Python
import os
import requests
from dotenv import load_dotenv
Load variables from the .env file
load_dotenv()
def get_token_data(token):
« » »Get token data from PumpFun » » »
url = f »
headers = {
« Authorization »: os.getenv(« PUMPFUN_API_TOKEN »),
« Content-Type »: « application/json »,
}
response = requests.get(url, headers=headers)
return response.json()
def get_market_cap_data(token):
« » »Get Solana token market capitalization data » » »
url = f »
headers = {
« Authorization »: os.getenv(« PUMPFUN_API_TOKEN »),
« Content-Type »: « application/json »,
}
response = requests.get(url, headers=headers)
return response.json()
definition of main():
token = input(« Enter the SOL token you want to track: « )
data = get_token_data(token)
Get market capitalization data
market_cap_data = get_market_cap_data(token)
print(f »Market cap: {market_cap_data[‘totalSupply’]} »)
if __name__ == « __main__ »:
main()
Step 2: Configure the Solana Host Environment
Make sure your Solana node is running and accessible from your Python script. This can be done by adding the solana
environment variable to the .env
file:
bash
PUMPFUN_API_TOKEN=your_API_token_here
Replace your_api_token_here
with the actual API token from PumpFun.
Step 3: Run the Script
Save the script and run it with Python. When prompted, enter the SOL token you want to track.
python solana_market_cap.py
«
This will print the market capitalization data for the specified SOL token.
Tips and Options:
- To get price data instead of market cap, update theurl
in the
get_token_data()` function to point to Solana’s price channel.
- Consider adding error handling and logging to make your script more reliable.
- You can use this script as a starting point and add more features such as token ID matching or filtering.
After completing these steps, you have successfully created a Python script that tracks the price and market cap of recently issued SOL tokens using the PumpFun API.