Created
February 14, 2025 06:21
-
-
Save 0xdeepmehta/8f69098b92d4ca276d3996d6ea36d1b0 to your computer and use it in GitHub Desktop.
This script downloads all trade positions for a given wallet address from Jupiter Perps API # and saves them to a CSV file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# =========================================== | |
# Jupiter Perps Trade History Downloader | |
# =========================================== | |
# | |
# This script downloads all trade positions for a given wallet address from Jupiter Perps API | |
# and saves them to a CSV file. | |
# | |
# How to run: | |
# ---------- | |
# bash download_jup_trade.sh <wallet_address> | |
# OR | |
# sh download_jup_trade.sh <wallet_address> | |
# | |
# Example: | |
# bash download_jup_trade.sh AnonCFDi2Whm3TjaVa6Hih6UNAsqr9aex21AhHJjE5TQ | |
# | |
# Requirements: | |
# - jq (JSON processor) | |
# - curl (HTTP client) | |
# =========================================== | |
# Show usage if no wallet address provided | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <wallet_address>" | |
echo "Example: $0 AnonCFDi2Whm3TjaVa6Hih6UNAsqr9aex21AhHJjE5TQ" | |
exit 1 | |
fi | |
# Configuration | |
WALLET_ADDRESS="$1" | |
API_BASE_URL="https://perps-api.jup.ag/v1/trades" | |
OUTPUT_FILE="trades_${WALLET_ADDRESS:0:8}.csv" # Use first 8 chars of wallet address in filename | |
BATCH_SIZE=10 | |
# Verify jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "Error: jq is not installed. Please install jq to use this script." | |
echo "Install using: sudo apt-get install jq (Ubuntu/Debian)" | |
echo " or: brew install jq (macOS)" | |
exit 1 | |
fi | |
# Create CSV header | |
echo "mint,positionName,side,action,orderType,collateralUsdDelta,price,size,fee,pnl,txHash,createdTime,updatedTime" > "$OUTPUT_FILE" | |
# Get total count from initial request | |
echo "Fetching trade count for wallet: $WALLET_ADDRESS" | |
INITIAL_RESPONSE=$(curl -s "${API_BASE_URL}?walletAddress=${WALLET_ADDRESS}&start=0&end=${BATCH_SIZE}") | |
# Check if the API request was successful | |
if [ -z "$INITIAL_RESPONSE" ]; then | |
echo "Error: Failed to get response from API" | |
exit 1 | |
fi | |
TOTAL_COUNT=$(echo "$INITIAL_RESPONSE" | jq -r '.count') | |
# Validate count | |
if [ -z "$TOTAL_COUNT" ] || [ "$TOTAL_COUNT" = "null" ]; then | |
echo "Error: Invalid response from API. Please verify the wallet address." | |
exit 1 | |
fi | |
echo "Total trades to download: $TOTAL_COUNT" | |
# Function to process each batch | |
process_batch() { | |
local start=$1 | |
local end=$2 | |
echo "Downloading trades $start to $end..." | |
# Make API request | |
response=$(curl -s "${API_BASE_URL}?walletAddress=${WALLET_ADDRESS}&start=$start&end=$end") | |
# Check if response is valid | |
if [ -z "$response" ] || ! echo "$response" | jq -e . >/dev/null 2>&1; then | |
echo "Error: Invalid response for batch $start-$end" | |
return 1 | |
fi | |
# Extract and format data as CSV | |
echo "$response" | jq -r '.dataList[] | [ | |
.mint, | |
.positionName, | |
.side, | |
.action, | |
.orderType, | |
.collateralUsdDelta, | |
.price, | |
.size, | |
.fee, | |
(.pnl // "null"), | |
.txHash, | |
.createdTime, | |
.updatedTime | |
] | @csv' >> "$OUTPUT_FILE" | |
} | |
# Download all trades in batches | |
for ((i=0; i<TOTAL_COUNT; i+=BATCH_SIZE)); do | |
end=$((i + BATCH_SIZE)) | |
if [ $end -gt $TOTAL_COUNT ]; then | |
end=$TOTAL_COUNT | |
fi | |
process_batch $i $end | |
# Add a small delay to avoid rate limiting | |
sleep 1 | |
done | |
echo "Download complete. Data saved to $OUTPUT_FILE" | |
# Print summary | |
DOWNLOADED_ROWS=$(wc -l < "$OUTPUT_FILE") | |
ACTUAL_ROWS=$((DOWNLOADED_ROWS - 1)) # Subtract 1 for header | |
echo "Downloaded $ACTUAL_ROWS trades" | |
# Verify data integrity | |
if [ $ACTUAL_ROWS -eq $TOTAL_COUNT ]; then | |
echo "✅ Successfully downloaded all trades" | |
else | |
echo "⚠️ Warning: Number of downloaded trades ($ACTUAL_ROWS) differs from expected count ($TOTAL_COUNT)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment