-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_gamma_streaming.cpp
More file actions
138 lines (120 loc) · 4.9 KB
/
Copy path02_gamma_streaming.cpp
File metadata and controls
138 lines (120 loc) · 4.9 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Example 02: Gamma API — Auto-pagination, streaming, and filtered fetching.
//
// Three patterns for scanning large result sets:
//
// get_all_markets/events — fetch everything into memory, auto-paginated
// for_each_market/event — stream one-by-one, zero extra allocation
// get_markets_where/events_where — fetch only items matching a predicate
//
// Run: ./02_gamma_streaming
#include <polymarket/gamma/gamma_client.hpp>
#include <cstdio>
#include <cmath>
using namespace polymarket;
int main() {
GammaClient gamma;
// ── 1. get_all_markets — fetch all active markets into memory ─────────
// Auto-paginates: fetches pages of `limit` until no more results.
// max_pages=2 → cap at 2 pages (200 markets). 0 = unlimited.
printf("=== get_all_markets (max 2 pages of 100) ===\n");
{
MarketsFilter f;
f.active = true;
f.closed = false;
f.order = "volume";
f.ascending = false;
auto result = gamma.get_all_markets(f, /*limit=*/100, /*delay_ms=*/0, /*max_pages=*/2);
if (!result) {
printf("Error: %s\n", result.error().message().c_str());
} else {
printf(" Fetched %zu markets\n", result->data.size());
// Print top 5
for (int i = 0; i < 5 && i < (int)result->data.size(); ++i) {
auto& m = result->data[i];
printf(" %d. $%-10.0f %.*s\n",
i + 1, m.volume_num,
(int)m.question.size(), m.question.data());
}
}
}
// ── 2. for_each_market — stream markets without storing all in memory ──
// Callback fires for each market as it arrives.
// Useful when you just want to aggregate or print — no big allocation.
printf("\n=== for_each_market (streaming, max 1 page of 20) ===\n");
{
MarketsFilter f;
f.active = true;
f.closed = false;
f.order = "liquidity";
f.ascending = false;
double total_volume = 0;
int count = 0;
auto r = gamma.for_each_market(f, [&](const GammaMarket& m) {
total_volume += m.volume_num;
++count;
}, /*limit=*/20, /*delay_ms=*/0, /*max_pages=*/1);
if (!r)
printf("Error: %s\n", r.error().message().c_str());
else
printf(" Streamed %d markets, total volume $%.0f\n", count, total_volume);
}
// ── 3. get_markets_where — fetch only markets matching a predicate ─────
// Paginates automatically and keeps only items where pred returns true.
// No need to filter manually after fetching.
printf("\n=== get_markets_where (liquidity > $50k, 1 page) ===\n");
{
MarketsFilter f;
f.active = true;
f.closed = false;
f.order = "liquidity";
f.ascending = false;
auto result = gamma.get_markets_where(f, [](const GammaMarket& m) {
return m.liquidity_num > 50000.0;
}, /*limit=*/100, /*delay_ms=*/0, /*max_pages=*/1);
if (!result) {
printf("Error: %s\n", result.error().message().c_str());
} else {
printf(" Found %zu markets with liquidity > $50k\n", result->data.size());
for (auto& m : result->data)
printf(" liq=$%-8.0f %.*s\n",
m.liquidity_num,
(int)m.question.size(), m.question.data());
}
}
// ── 4. for_each_event — stream events, stop early with return false ───
// Return false from the callback to stop iteration immediately.
printf("\n=== for_each_event (stop after finding 3 featured) ===\n");
{
EventsFilter f;
f.active = true;
f.closed = false;
f.featured = true;
int found = 0;
gamma.for_each_event(f, [&](const GammaEvent& e) {
printf(" %.*s — $%.0f\n",
(int)e.title.size(), e.title.data(), e.volume);
return ++found < 3; // return false stops iteration
}, /*limit=*/100);
}
// ── 5. get_events_where — multi-page filtered event fetch ─────────────
printf("\n=== get_events_where (volume > $1M, max 2 pages) ===\n");
{
EventsFilter f;
f.active = true;
f.closed = false;
auto result = gamma.get_events_where(f, [](const GammaEvent& e) {
return e.volume > 1'000'000.0;
}, /*limit=*/100, /*delay_ms=*/0, /*max_pages=*/2);
if (!result) {
printf("Error: %s\n", result.error().message().c_str());
} else {
printf(" Found %zu events with volume > $1M\n", result->data.size());
for (auto& e : result->data)
printf(" $%-12.0f %.*s\n",
e.volume,
(int)e.title.size(), e.title.data());
}
}
printf("\nDone.\n");
return 0;
}