Solana: How to track the price or market cap of spl tokens?

Monitoring the Price or Market Cap of SOL (Solana) Tokens with Python

As a Solana-focused bot, you are probably interested in monitoring the price and market cap of newly minted SOL tokens. In this article, we will explore how to monitor these metrics using Python and interact with the Solana API.

Prerequisites:

  • You have set up a Solana node (e.g. Solana Mainnet or Testnet) and created an account on PumpFun.
  • You have installed the pumpfun library (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.

import os

import requests

from dotenv import load_dotenv






Load environment variables from .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 market cap data for a Solana token"""

url = f"

headers = {

"Authorization": os.getenv("PUMPFUN_API_TOKEN"),

"Content-Type": "application/json",

}

response = requests.get(url, headers=headers)

return response.json()

def main():

token = input("Enter the SOL token you want to track: ")

data = get_token_data(token)


Get market cap data

market_cap_data = get_market_cap_data(token)

print(f"Market cap: {market_cap_data['totalSupply']}")

if __name__ == "__main__":

main()

Step 2: Set up Solana environment Node

Make sure your Solana node is running and accessible from your Python script. You can do this by adding a solana environment variable to your .env file:

PUMPFUN_API_TOKEN=your_api_token_here

Replace your_api_token_here with your actual PumpFun API token.

Step 3: Run the script

Save the script and run it using Python. When prompted, enter the SOL token you want to track.

python solana_market_cap.py

This will print the market cap data for the specified SOL token.

Tips and variations:

  • To get price data instead of market cap, update the url in the get_token_data() function to point to the Solana price feed.
  • Consider adding error handling and logging to make your script more robust.
  • You can use this script as a starting point and add more features, such as token ID mapping or filtering.

By following these steps, you have successfully created a Python script that tracks the price and market cap of newly minted SOL tokens using the PumpFun API.