Skip to main content
Deriverse uses Supabase for persistent storage of wallet data, trade history, and user annotations. These services provide type-safe interfaces for interacting with the database.

SupabaseWalletService

Manages user wallet data including connection information and sync status.

saveWallet

Save or update a wallet in the database using upsert logic.
async saveWallet(params: SaveWalletParams): Promise<UserWallet>
params
SaveWalletParams
required
Wallet connection parameters
address
string
required
Wallet address (blockchain address)
network
string
required
Network identifier (e.g., ‘solana-mainnet’)
provider
string
Wallet provider name (defaults to ‘manual’)
method
'manual' | 'wallet_connect'
Connection method (defaults to ‘manual’)
UserWallet
object
The saved wallet record
id
string
Database record ID
wallet_address
string
Blockchain wallet address
network
string
Network identifier
wallet_provider
string
Wallet provider name
connection_method
string
Connection method used
last_synced_at
string | null
ISO timestamp of last sync
created_at
string
ISO timestamp of creation
updated_at
string
ISO timestamp of last update

getWallet

Retrieve a wallet by its address.
async getWallet(address: string): Promise<UserWallet | null>
address
string
required
Wallet address to lookup
result
UserWallet | null
Returns the wallet record or null if not found

updateSyncTime

Update the last_synced_at timestamp for a wallet.
async updateSyncTime(address: string): Promise<void>
address
string
required
Wallet address to update

getRecentWallets

Get recently synced wallets for displaying in UI.
async getRecentWallets(limit: number = 5): Promise<UserWallet[]>
limit
number
default:5
Maximum number of wallets to return
result
UserWallet[]
Array of wallets ordered by most recently synced

isDataStale

Check if wallet data needs refreshing based on time threshold.
isDataStale(wallet: UserWallet, hoursThreshold: number = 24): boolean
wallet
UserWallet
required
Wallet object to check
hoursThreshold
number
default:24
Hours before data is considered stale
result
boolean
Returns true if data is older than threshold or never synced

getTimeSinceSync

Get human-readable string for time since last sync.
getTimeSinceSync(wallet: UserWallet): string
wallet
UserWallet
required
Wallet object to format
result
string
Human-readable time (e.g., “5 minutes ago”, “2 hours ago”, “Never synced”)

SupabaseTradeService

Manages trade data persistence and analytics.

saveTrades

Save multiple trades to the database using upsert logic.
async saveTrades(walletAddress: string, trades: Trade[]): Promise<SaveTradesResult>
walletAddress
string
required
Wallet address that owns these trades
trades
Trade[]
required
Array of Trade objects to save
SaveTradesResult
object
Operation result summary
saved
number
Number of trades saved (upserted)
updated
number
Number of trades updated (always 0 for Supabase upsert)
errors
number
Number of errors encountered

getTrades

Retrieve all trades for a wallet.
async getTrades(walletAddress: string, limit?: number): Promise<Trade[]>
walletAddress
string
required
Wallet address to query
limit
number
Optional limit on number of trades returned
result
Trade[]
Array of trades ordered by closed_at descending (most recent first)

getTradeCount

Get total number of trades for a wallet.
async getTradeCount(walletAddress: string): Promise<number>
walletAddress
string
required
Wallet address to count
result
number
Total trade count

deleteTrades

Delete all trades for a wallet.
async deleteTrades(walletAddress: string): Promise<number>
walletAddress
string
required
Wallet address to delete trades for
result
number
Number of trades deleted

getAnalytics

Get aggregated analytics summary for a wallet.
async getAnalytics(walletAddress: string): Promise<AnalyticsSummary>
walletAddress
string
required
Wallet address to analyze
AnalyticsSummary
object
Analytics summary object
totalTrades
number
Total number of trades
totalPnl
number
Cumulative profit and loss
winRate
number
Win rate percentage (0-100)
totalFees
number
Total fees paid

SupabaseAnnotationService

Manages user annotations and notes for trades.

getAnnotation

Fetch annotation for a specific trade.
async getAnnotation(tradeId: string): Promise<TradeAnnotation | null>
tradeId
string
required
Trade ID to fetch annotation for
result
TradeAnnotation | null
Returns annotation or null if not found

saveAnnotation

Save or update annotation for a trade.
async saveAnnotation(annotation: TradeAnnotation, walletAddress?: string): Promise<TradeAnnotation>
annotation
TradeAnnotation
required
Annotation object to save
tradeId
string
required
Trade ID this annotation belongs to
notes
string
User notes about the trade
tags
string[]
Tags for categorizing the trade
lessonsLearned
string
Lessons learned from the trade
walletAddress
string
Optional wallet address for additional validation
result
TradeAnnotation
The saved annotation with timestamps
id
string
Database record ID
tradeId
string
Associated trade ID
notes
string
User notes
tags
string[]
Tag array
lessonsLearned
string
Lessons learned text
createdAt
Date
Creation timestamp
updatedAt
Date
Last update timestamp

getAnnotationsForWallet

Get all annotations for a wallet’s trades.
async getAnnotationsForWallet(walletAddress: string): Promise<Record<string, TradeAnnotation>>
walletAddress
string
required
Wallet address to fetch annotations for
result
Record<string, TradeAnnotation>
Dictionary mapping trade IDs to their annotations

Example Usage

import { SupabaseWalletService } from '@/services/SupabaseWalletService';
import { SupabaseTradeService } from '@/services/SupabaseTradeService';
import { SupabaseAnnotationService } from '@/services/SupabaseAnnotationService';

// Initialize services
const walletService = new SupabaseWalletService();
const tradeService = new SupabaseTradeService();
const annotationService = new SupabaseAnnotationService();

// Save a wallet
const wallet = await walletService.saveWallet({
  address: '7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU',
  network: 'solana-mainnet',
  provider: 'phantom',
  method: 'wallet_connect'
});

// Get analytics
const analytics = await tradeService.getAnalytics(wallet.wallet_address);
console.log(`Win rate: ${analytics.winRate}%`);

// Save annotation
const annotation = await annotationService.saveAnnotation({
  tradeId: 'trade-123',
  notes: 'Good entry, should have taken profit earlier',
  tags: ['btc', 'long'],
  lessonsLearned: 'Set tighter stop losses on volatile markets'
}, wallet.wallet_address);

Build docs developers (and LLMs) love