Skip to content

cosmic-markets/cosmic-rust-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cosmic Rust SDK πŸš€

License: MIT Rust Solana Cosmic Markets

A comprehensive Rust SDK for accessing Cosmic Markets APIs, providing seamless integration with Solana DeFi protocols including Drift Protocol, Jupiter, Kamino, and more.

✨ Features

  • 🎯 Drift Protocol V2 - Markets, positions, orders, PnL history, and analytics
  • πŸ”„ Jupiter Integration - DCA orders, limit orders, and token analytics
  • πŸ’§ Kamino Finance - Swap data and analytics
  • πŸ“Š OHLC Data - Historical price data for tokens
  • πŸ‘€ Trader Profiles - Trader scoring and performance metrics
  • πŸ” Token Search - Comprehensive token discovery
  • πŸͺΆ Minimal Dependencies - Only 5 dependencies for a lightweight footprint

πŸ“¦ Installation

Add this to your Cargo.toml:

[dependencies]
cosmic-rust-sdk = { git = "https://github.com/cosmic-markets/cosmic-rust-sdk", rev = "034d488" }

Note: For local development, use path = "." instead of the git dependency.

πŸš€ Quick Start

Basic Usage

use cosmic_rust_sdk::Cosmic;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize client (no API key = rate limits apply)
    let client = Cosmic::new(None)?;

    // Fetch all Drift markets
    let markets = client.drift.get_all_markets(Default::default()).await?;
    println!("πŸ“Š Found {} markets", markets.content.len());

    Ok(())
}

With API Key (Recommended)

use cosmic_rust_sdk::Cosmic;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize with API key for higher rate limits
    let client = Cosmic::new(Some("YOUR_API_KEY".to_string()))?;
    
    // Your code here...
    Ok(())
}

πŸ“š Usage Examples

Fetch Perpetual Positions

use cosmic_rust_sdk::Cosmic;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Cosmic::new(None)?;
    
    let wallet = "EXYq2HaS5nTarjzNHVYfcHbAbbLDJTPYkn1Jb5Q6bGz4";
    let positions = client.drift.get_user_perp_positions(wallet).await?;

    println!("πŸ“ˆ Found {} active positions", positions.len());
    
    for pos in positions {
        if let Some(market) = &pos.market_name {
            println!("Market: {}", market);
            println!("  Size: {} {}", 
                pos.base_asset_amount.unwrap_or(0.0),
                pos.base_asset_symbol.unwrap_or_default()
            );
            println!("  Unrealized PnL: ${:.2}", pos.unrealized_pnl.unwrap_or(0.0));
        }
    }

    Ok(())
}

Query Jupiter DCA Orders

use cosmic_rust_sdk::Cosmic;
use cosmic_rust_sdk::api::jupiter::GetDcaOrdersRequest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Cosmic::new(None)?;

    let request = GetDcaOrdersRequest {
        page: Some(1),
        size: Some(10),
        ..Default::default()
    };

    let orders = client.jupiter.get_orders(request).await?;
    println!("πŸ”„ Found {} DCA orders", orders.content.len());

    Ok(())
}

Fetch Market Data

use cosmic_rust_sdk::Cosmic;
use cosmic_rust_sdk::api::drift::GetDriftMarketsRequest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Cosmic::new(None)?;

    let request = GetDriftMarketsRequest {
        size: Some(5),
        ..Default::default()
    };

    let markets = client.drift.get_all_markets(request).await?;
    
    for market in markets.content {
        println!("Market: {}", market.market_name.unwrap_or_default());
        println!("  Index: {}", market.market_index.unwrap_or(0));
        println!("  Type: {}", market.market_type.unwrap_or_default());
    }

    Ok(())
}

πŸ”‘ Authentication

To avoid strict rate limits, use an API key. You can purchase one at cosmic.markets/api/docs.

let client = Cosmic::new(Some("YOUR_API_KEY".to_string()))?;

πŸ“– API Documentation

🎯 Example Programs

Run these examples to see the SDK in action:

Example Command Description
Perp Positions cargo run --example perp_positions Retrieves active perpetual futures positions for a wallet
PnL History cargo run --example pnl_history Analyzes settled PnL over time for a specific market
DEX Volume cargo run --example dex_volume Tracks volume across Drift and Jupiter protocols
Limit Orders cargo run --example limit_orders Inspects recent limit order activity on Jupiter
DCA Orders cargo run --example dca_orders Inspects recent DCA order activity

πŸ§ͺ Running Tests

When running tests without a paid API key, use single-threaded execution to avoid rate limits:

cargo test -- --test-threads=1

With a paid API key, you can run tests in parallel:

cargo test

🧩 Modules

Module Description
drift Drift Protocol V2 - Markets, positions, orders, history, and analytics
jupiter Jupiter aggregator - DCA orders, limit orders, and token analytics
kamino Kamino Finance - Swap data and analytics
ohlc Historical OHLC price data for tokens
trader Trader profiling, scoring, and performance metrics
search Token search and discovery capabilities

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ”— Links

About

Rust SDK for the Cosmic Markets API. Solana DeFi market data.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages