-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy path01-basic-usage.ts
More file actions
70 lines (61 loc) · 3.16 KB
/
01-basic-usage.ts
File metadata and controls
70 lines (61 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Example 1: Basic SDK Usage
*
* This example demonstrates:
* - Getting trending markets from Gamma API
* - Getting market details from unified API (Gamma + CLOB)
* - Getting orderbook data
*
* Run: npx ts-node examples/01-basic-usage.ts
*/
import { PolymarketSDK } from '../src/index.js';
async function main() {
console.log('=== Polymarket SDK Basic Usage ===\n');
const sdk = new PolymarketSDK();
// 1. Get trending markets
console.log('1. Fetching trending markets...');
const trendingMarkets = await sdk.gammaApi.getTrendingMarkets(5);
console.log(` Found ${trendingMarkets.length} trending markets:\n`);
for (const market of trendingMarkets) {
console.log(` - ${market.question}`);
console.log(` Slug: ${market.slug}`);
console.log(` Volume: $${market.volume.toLocaleString()}`);
console.log(` 24h Volume: $${market.volume24hr?.toLocaleString() || 'N/A'}`);
console.log(` Prices: Yes=${market.outcomePrices[0]?.toFixed(2)}, No=${market.outcomePrices[1]?.toFixed(2)}`);
console.log('');
}
// 2. Get unified market details (Gamma + CLOB merged)
if (trendingMarkets.length > 0) {
const firstMarket = trendingMarkets[0];
console.log(`2. Getting unified market details for: ${firstMarket.slug}`);
const unifiedMarket = await sdk.getMarket(firstMarket.slug);
console.log(` Question: ${unifiedMarket.question}`);
console.log(` Condition ID: ${unifiedMarket.conditionId}`);
const yesToken = unifiedMarket.tokens.find(t => t.outcome === 'Yes');
const noToken = unifiedMarket.tokens.find(t => t.outcome === 'No');
console.log(` YES Token ID: ${yesToken?.tokenId}`);
console.log(` NO Token ID: ${noToken?.tokenId}`);
console.log(` YES Price: ${yesToken?.price.toFixed(4)}`);
console.log(` NO Price: ${noToken?.price.toFixed(4)}`);
console.log(` Source: ${unifiedMarket.source}`);
console.log('');
// 3. Get orderbook
console.log('3. Getting orderbook...');
const orderbook = await sdk.getOrderbook(unifiedMarket.conditionId);
console.log(` YES Best Bid: ${orderbook.yes.bid.toFixed(4)} (size: ${orderbook.yes.bidSize.toFixed(2)})`);
console.log(` YES Best Ask: ${orderbook.yes.ask.toFixed(4)} (size: ${orderbook.yes.askSize.toFixed(2)})`);
console.log(` YES Spread: ${(orderbook.yes.spread * 100).toFixed(2)}%`);
console.log('');
console.log(` NO Best Bid: ${orderbook.no.bid.toFixed(4)} (size: ${orderbook.no.bidSize.toFixed(2)})`);
console.log(` NO Best Ask: ${orderbook.no.ask.toFixed(4)} (size: ${orderbook.no.askSize.toFixed(2)})`);
console.log(` NO Spread: ${(orderbook.no.spread * 100).toFixed(2)}%`);
console.log('');
console.log(` Ask Sum (YES+NO): ${orderbook.summary.askSum.toFixed(4)}`);
console.log(` Bid Sum (YES+NO): ${orderbook.summary.bidSum.toFixed(4)}`);
console.log(` Long Arb Profit: ${(orderbook.summary.longArbProfit * 100).toFixed(3)}%`);
console.log(` Short Arb Profit: ${(orderbook.summary.shortArbProfit * 100).toFixed(3)}%`);
console.log(` Imbalance Ratio: ${orderbook.summary.imbalanceRatio.toFixed(2)}`);
}
console.log('\n=== Done ===');
}
main().catch(console.error);