First Trade
A complete walkthrough of placing your first trade on Realm, from setup to closing your position.
Prerequisites
Complete Example
This example covers the full trading flow: connecting, getting funds, checking the market, placing an order, and monitoring your position.
1import { RealmClient, RealmWallet } from '@empyrealm/sdk';23async function main() {4 // ═══════════════════════════════════════════════════════════════════════════5 // 1. SETUP6 // ═══════════════════════════════════════════════════════════════════════════78 const client = new RealmClient('https://api.realm.software');9 const wallet = await RealmWallet.generate();10 await client.connect(wallet);1112 console.log('✅ Connected');13 console.log(' Address:', wallet.address);1415 // ═══════════════════════════════════════════════════════════════════════════16 // 2. GET FUNDS (Faucet → Deposit)17 // ═══════════════════════════════════════════════════════════════════════════1819 // Claim from faucet (testnet only)20 await client.claimFaucet();21 console.log('✅ Claimed faucet');2223 // Deposit to exchange24 await client.deposit({ token: 'RLM', amount: 5000 });25 await client.deposit({ token: 'USDC', amount: 10000 });26 console.log('✅ Deposited to exchange');2728 // Check balance29 const balance = await client.getBalance();30 console.log(' RLM Exchange:', balance.rlmExchange);31 console.log(' USDC Exchange:', balance.usdcExchange);3233 // ═══════════════════════════════════════════════════════════════════════════34 // 3. CHECK MARKET35 // ═══════════════════════════════════════════════════════════════════════════3637 const markets = await client.getMarkets();38 const rlmPerp = markets.find(m => m.symbol === 'RLM-PERP');39 console.log('\n📊 RLM-PERP Market');40 console.log(' Index Price:', rlmPerp?.indexPrice);41 console.log(' Mark Price:', rlmPerp?.markPrice);42 console.log(' 24h Volume:', rlmPerp?.volume24h);4344 const orderbook = await client.getOrderbook('RLM-PERP');45 console.log(' Best Bid:', orderbook.bids[0]?.price);46 console.log(' Best Ask:', orderbook.asks[0]?.price);4748 // ═══════════════════════════════════════════════════════════════════════════49 // 4. PLACE ORDER50 // ═══════════════════════════════════════════════════════════════════════════5152 const order = await client.placeOrder({53 market: 'RLM-PERP',54 side: 'buy',55 type: 'limit',56 price: orderbook.asks[0]?.price ?? 100, // Match best ask57 size: 1.0,58 timeInForce: 'GTC',59 });6061 console.log('\n📝 Order Placed');62 console.log(' Order ID:', order.orderId);63 console.log(' Status:', order.status);64 console.log(' Filled:', order.filledSize, '/', order.size);6566 // ═══════════════════════════════════════════════════════════════════════════67 // 5. CHECK POSITION68 // ═══════════════════════════════════════════════════════════════════════════6970 const positions = await client.getPositions();71 const position = positions.find(p => p.market === 'RLM-PERP');7273 if (position) {74 console.log('\n📈 Position');75 console.log(' Side:', position.side);76 console.log(' Size:', position.size);77 console.log(' Entry Price:', position.entryPrice);78 console.log(' Mark Price:', position.markPrice);79 console.log(' Unrealized PnL:', position.unrealizedPnl);80 }8182 // ═══════════════════════════════════════════════════════════════════════════83 // 6. CLOSE POSITION (Optional)84 // ═══════════════════════════════════════════════════════════════════════════8586 if (position) {87 await client.placeOrder({88 market: 'RLM-PERP',89 side: 'sell',90 type: 'market',91 size: position.size,92 reduceOnly: true,93 });94 console.log('\n✅ Position closed');95 }96}9798main().catch(console.error);
Understanding the Flow
Step 1: Setup
Create a client and wallet, then connect. The connect() call authenticates your session using your Dilithium-3 keypair. All subsequent requests are automatically signed.
Step 2: Get Funds
On testnet, claim tokens from the faucet. Then deposit from L1 (your wallet) to the Exchange (trading account). This two-layer system protects your funds.
Step 3: Check Market
Before trading, check the market conditions. The orderbook shows available liquidity at each price level.
Step 4: Place Order
Submit a limit order. The order is signed with your private key and submitted to the blockchain. Execution is deterministic and final within 25ms.
Step 5: Check Position
If your order filled, you now have a position. Monitor your unrealized PnL and manage risk with stop-loss orders.
Conditional Orders
Protect your positions with stop-loss, take-profit, and trailing stop orders:
// Stop-Loss Orderawait client.placeOrder({market: 'RLM-PERP',side: 'sell',type: 'stop_market',size: 1.0,triggerPrice: 90.00, // Trigger when price falls to 90triggerDirection: 'below',reduceOnly: true,});// Take-Profit Orderawait client.placeOrder({market: 'RLM-PERP',side: 'sell',type: 'take_profit',size: 1.0,triggerPrice: 120.00, // Trigger when price rises to 120triggerDirection: 'above',reduceOnly: true,});// Trailing Stop (5% trail)await client.placeOrder({market: 'RLM-PERP',side: 'sell',type: 'trailing_stop',size: 1.0,trailPercent: 5.0, // 5% trailing distancereduceOnly: true,});
Next Steps
- Order Types — All order types explained
- Margin & Leverage — Understanding margin trading
- PlaceOrder API — Full API reference
- Real-time Streaming — Subscribe to updates