Code Examples
TypeScript, Python, and cURL examples for common workflows.
Code Examples
TypeScript — Full Trading Workflow
import { Connection, Keypair, VersionedTransaction } from '@solana/web3.js';
import bs58 from 'bs58';
const API_BASE = 'https://api.lavarage.xyz';
const API_KEY = process.env.LAVARAGE_API_KEY!;
const WALLET = Keypair.fromSecretKey(bs58.decode(process.env.WALLET_KEY!));
const connection = new Connection('https://api.mainnet-beta.solana.com');
const headers = {
'x-api-key': API_KEY,
'Content-Type': 'application/json',
};
// 1. Find a SOL/USDC pool
const offers = await fetch(
`${API_BASE}/api/v1/offers?search=SOL&includeTokens=true`
).then(r => r.json());
const pool = offers.find(
(o: any) => o.side === 'LONG' && parseFloat(o.availableForOpen) > 0
);
console.log(`Using pool: ${pool.publicKey} (max ${pool.maxLeverage}x)`);
// 2. Get a quote
const quote = await fetch(`${API_BASE}/api/v1/positions/quote`, {
method: 'POST',
headers,
body: JSON.stringify({
offerPublicKey: pool.publicKey,
userPublicKey: WALLET.publicKey.toBase58(),
collateralAmount: '500000000', // 0.5 SOL
leverage: 2,
side: 'LONG',
slippageBps: 50,
}),
}).then(r => r.json());
console.log(`Expected output: ${quote.outAmount} tokens`);
// 3. Open the position
const { transaction } = await fetch(`${API_BASE}/api/v1/positions/open`, {
method: 'POST',
headers,
body: JSON.stringify({
offerPublicKey: pool.publicKey,
userPublicKey: WALLET.publicKey.toBase58(),
collateralAmount: '500000000',
leverage: 2,
side: 'LONG',
slippageBps: 50,
}),
}).then(r => r.json());
const tx = VersionedTransaction.deserialize(bs58.decode(transaction));
tx.sign([WALLET]);
const sig = await connection.sendTransaction(tx);
console.log(`Opened position: ${sig}`);
// 4. List open positions
const positions = await fetch(
`${API_BASE}/api/v1/positions?status=OPEN&owner=${WALLET.publicKey.toBase58()}`,
{ headers }
).then(r => r.json());
console.log(`Open positions: ${positions.length}`);
// 5. Close when ready
if (positions.length > 0) {
const { transaction: closeTx } = await fetch(
`${API_BASE}/api/v1/positions/close`,
{
method: 'POST',
headers,
body: JSON.stringify({
positionAddress: positions[0].address,
userPublicKey: WALLET.publicKey.toBase58(),
slippageBps: 50,
}),
}
).then(r => r.json());
const closeTxn = VersionedTransaction.deserialize(bs58.decode(closeTx));
closeTxn.sign([WALLET]);
const closeSig = await connection.sendTransaction(closeTxn);
console.log(`Closed position: ${closeSig}`);
}Python — Open a Position
import requests
import base58
from solders.transaction import VersionedTransaction
from solders.keypair import Keypair
API_BASE = "https://api.lavarage.xyz"
API_KEY = "your-api-key"
wallet = Keypair.from_base58_string("your-private-key")
headers = {
"x-api-key": API_KEY,
"Content-Type": "application/json",
}
# Get offers
offers = requests.get(f"{API_BASE}/api/v1/offers?search=SOL&includeTokens=true").json()
pool = next(o for o in offers if o["side"] == "LONG" and float(o["availableForOpen"] or 0) > 0)
# Open position
resp = requests.post(f"{API_BASE}/api/v1/positions/open", headers=headers, json={
"offerPublicKey": pool["publicKey"],
"userPublicKey": str(wallet.pubkey()),
"collateralAmount": "1000000000", # 1 SOL
"leverage": 2,
"side": "LONG",
"slippageBps": 50,
}).json()
tx_bytes = base58.b58decode(resp["transaction"])
tx = VersionedTransaction.from_bytes(tx_bytes)
# Sign and submit with your preferred Solana Python library
print(f"Transaction ready to sign: {len(tx_bytes)} bytes")cURL — Common Operations
List SOL pools:
curl "https://api.lavarage.xyz/api/v1/offers?search=SOL&includeTokens=true&limit=5"Check pool utilization:
curl "https://api.lavarage.xyz/api/v1/offers/utilization"Export trade history as CSV:
curl "https://api.lavarage.xyz/api/v1/positions/export?status=CLOSED&from=2026-01-01T00:00:00Z" \
-H "x-api-key: YOUR_KEY" \
-o trades.csvGet Jito bundle tip:
curl "https://api.lavarage.xyz/api/v1/bundle/tip"Updated 4 days ago