First Trade

A complete walkthrough of placing your first trade on Realm, from setup to closing your position.

Prerequisites

Make sure you've completed the Installation guide and have the SDK installed.

Complete Example

This example covers the full trading flow: connecting, getting funds, checking the market, placing an order, and monitoring your position.

first-trade.tstypescript
1import { RealmClient, RealmWallet } from '@empyrealm/sdk';
2
3async function main() {
4 // ═══════════════════════════════════════════════════════════════════════════
5 // 1. SETUP
6 // ═══════════════════════════════════════════════════════════════════════════
7
8 const client = new RealmClient('https://api.realm.software');
9 const wallet = await RealmWallet.generate();
10 await client.connect(wallet);
11
12 console.log('✅ Connected');
13 console.log(' Address:', wallet.address);
14
15 // ═══════════════════════════════════════════════════════════════════════════
16 // 2. GET FUNDS (Faucet → Deposit)
17 // ═══════════════════════════════════════════════════════════════════════════
18
19 // Claim from faucet (testnet only)
20 await client.claimFaucet();
21 console.log('✅ Claimed faucet');
22
23 // Deposit to exchange
24 await client.deposit({ token: 'RLM', amount: 5000 });
25 await client.deposit({ token: 'USDC', amount: 10000 });
26 console.log('✅ Deposited to exchange');
27
28 // Check balance
29 const balance = await client.getBalance();
30 console.log(' RLM Exchange:', balance.rlmExchange);
31 console.log(' USDC Exchange:', balance.usdcExchange);
32
33 // ═══════════════════════════════════════════════════════════════════════════
34 // 3. CHECK MARKET
35 // ═══════════════════════════════════════════════════════════════════════════
36
37 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);
43
44 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);
47
48 // ═══════════════════════════════════════════════════════════════════════════
49 // 4. PLACE ORDER
50 // ═══════════════════════════════════════════════════════════════════════════
51
52 const order = await client.placeOrder({
53 market: 'RLM-PERP',
54 side: 'buy',
55 type: 'limit',
56 price: orderbook.asks[0]?.price ?? 100, // Match best ask
57 size: 1.0,
58 timeInForce: 'GTC',
59 });
60
61 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);
65
66 // ═══════════════════════════════════════════════════════════════════════════
67 // 5. CHECK POSITION
68 // ═══════════════════════════════════════════════════════════════════════════
69
70 const positions = await client.getPositions();
71 const position = positions.find(p => p.market === 'RLM-PERP');
72
73 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 }
81
82 // ═══════════════════════════════════════════════════════════════════════════
83 // 6. CLOSE POSITION (Optional)
84 // ═══════════════════════════════════════════════════════════════════════════
85
86 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}
97
98main().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:

typescript
// Stop-Loss Order
await client.placeOrder({
market: 'RLM-PERP',
side: 'sell',
type: 'stop_market',
size: 1.0,
triggerPrice: 90.00, // Trigger when price falls to 90
triggerDirection: 'below',
reduceOnly: true,
});
// Take-Profit Order
await client.placeOrder({
market: 'RLM-PERP',
side: 'sell',
type: 'take_profit',
size: 1.0,
triggerPrice: 120.00, // Trigger when price rises to 120
triggerDirection: '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 distance
reduceOnly: true,
});

Next Steps