-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_order_placement.cpp
More file actions
317 lines (291 loc) · 14.1 KB
/
07_order_placement.cpp
File metadata and controls
317 lines (291 loc) · 14.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Example 07: Authenticated CLOB — balance, order placement, cancellation.
//
// Demonstrates all order types: GTC, GTD, FOK, FAK, and batch placement.
// Orders use prices far from the market so they rest in the book safely.
//
// Required env vars:
// export POLY_PRIVATE_KEY="0x..."
// export POLY_API_KEY="..."
// export POLY_SECRET="..."
// export POLY_PASSPHRASE="..."
// export POLY_PROXY_WALLET="0x..."
//
// Optional — pin a specific market (token IDs from polymarket.com or Gamma API):
// export POLY_TOKEN_YES="123..."
// export POLY_TOKEN_NO="456..."
//
// Run: ./07_order_placement
#include <polymarket/gamma/gamma_client.hpp>
#include <polymarket/clob/authenticated_client.hpp>
#include <polymarket/types/enums.hpp>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace polymarket;
static std::string require_env(const char* name) {
const char* v = std::getenv(name);
if (!v || *v == '\0') {
fprintf(stderr, "Missing env var: %s\n", name);
std::exit(1);
}
return v;
}
int main() {
// ── Credentials ───────────────────────────────────────────────────────────
const auto private_key = require_env("POLY_PRIVATE_KEY");
const auto proxy_wallet = require_env("POLY_PROXY_WALLET");
Credentials creds;
creds.apiKey = require_env("POLY_API_KEY");
creds.secret = require_env("POLY_SECRET");
creds.passphrase = require_env("POLY_PASSPHRASE");
Signer signer(private_key);
printf("Wallet: %s\n", signer.address_hex().c_str());
// ClobClient syncs clock offset against /time on construction —
// so HMAC timestamps are correct even when local clock is skewed.
AuthenticatedClobClient clob(signer, creds, proxy_wallet);
// ── 1. Balance ────────────────────────────────────────────────────────────
// signature_type must match how the proxy wallet was created:
// EOA=0 PolyProxy=1 PolyGnosisSafe=2 (most Polymarket wallets are type 2)
printf("\n=== Balance ===\n");
{
auto r = clob.balance_allowance("COLLATERAL", {}, SignatureType::PolyGnosisSafe);
if (!r) {
printf(" Error: %s\n", r.error().message().c_str());
} else {
// balance is in USDC micros (6 decimals)
long long raw = r->balance.empty() ? 0LL : std::stoll(r->balance);
printf(" USDC balance: %.2f\n", raw / 1e6);
for (auto& [addr, amt] : r->allowances)
printf(" allowance[%.10s...]: %s\n", addr.c_str(), amt.c_str());
}
}
// ── 2. Pick a market ──────────────────────────────────────────────────────
// Set POLY_TOKEN_YES / POLY_TOKEN_NO to trade a specific market.
// Without them, picks the most liquid active market automatically.
printf("\n=== Market ===\n");
std::string token_yes, token_no;
const char* env_yes = std::getenv("POLY_TOKEN_YES");
const char* env_no = std::getenv("POLY_TOKEN_NO");
if (env_yes && *env_yes && env_no && *env_no) {
token_yes = env_yes;
token_no = env_no;
printf(" Using pinned tokens from env\n");
} else {
GammaClient gamma;
MarketsFilter f;
f.limit = 1; f.active = true; f.closed = false;
f.order = "liquidity"; f.ascending = false;
auto markets = gamma.get_markets(f);
if (!markets || markets->data.empty()) {
fprintf(stderr, "Failed to get market\n"); return 1;
}
auto& m = markets->data[0];
printf(" %.*s\n", (int)m.question.size(), m.question.data());
printf(" (set POLY_TOKEN_YES / POLY_TOKEN_NO to pin a specific market)\n");
// Parse token IDs from '["id1","id2"]'
std::string raw(m.clob_token_ids);
size_t pos = 0, n = 0;
while (pos < raw.size() && n < 2) {
auto p1 = raw.find('"', pos);
if (p1 == std::string::npos) break;
auto p2 = raw.find('"', p1 + 1);
if (p2 == std::string::npos) break;
(n == 0 ? token_yes : token_no) = raw.substr(p1 + 1, p2 - p1 - 1);
pos = p2 + 1; ++n;
}
}
printf(" token_yes: %.20s...\n", token_yes.c_str());
printf(" token_no: %.20s...\n", token_no.c_str());
// ── 3. GTC — Good Till Cancelled ─────────────────────────────────────────
// Passive limit order. Sits in the book until filled or manually cancelled.
// Price at 0.10 — far from market, won't fill accidentally.
printf("\n=== Place GTC order (BUY Yes @ 0.10, size 5) ===\n");
std::string gtc_order_id;
{
auto order = clob.build_order(token_yes, "0.10", "5", Side::Buy, OrderType::GTC);
if (!order) {
printf(" Build error: %s\n", order.error().message().c_str());
} else {
auto r = clob.place_order(*order);
if (!r) {
printf(" Place error: %s\n", r.error().message().c_str());
} else {
printf(" success=%s status=%s id=%s\n",
r->success ? "yes" : "no",
r->status.c_str(), r->orderID.c_str());
if (!r->errorMsg.empty())
printf(" errorMsg: %s\n", r->errorMsg.c_str());
if (r->success) gtc_order_id = r->orderID;
}
}
}
// ── 4. Get order by ID ────────────────────────────────────────────────────
if (!gtc_order_id.empty()) {
printf("\n=== Get order by ID ===\n");
auto r = clob.get_order(gtc_order_id);
if (!r) {
printf(" Error: %s\n", r.error().message().c_str());
} else {
printf(" id: %s\n", r->id.c_str());
printf(" status: %s\n", r->status.c_str());
printf(" side: %s\n", r->side.c_str());
printf(" price: %s\n", r->price.c_str());
printf(" size_orig: %s\n", r->original_size.c_str());
printf(" size_matched: %s\n", r->size_matched.c_str());
printf(" outcome: %s\n", r->outcome.c_str());
}
}
// ── 5. GTD — Good Till Date ───────────────────────────────────────────────
// Like GTC but auto-cancels at a unix timestamp.
// Polymarket requires expiration >= now + 60s (security threshold).
printf("\n=== Place GTD order (BUY Yes @ 0.10, expires in 5 min) ===\n");
std::string gtd_order_id;
{
uint64_t expiry = static_cast<uint64_t>(std::time(nullptr)) + 360; // now + 6 min
auto order = clob.build_order(token_yes, "0.10", "5", Side::Buy,
OrderType::GTD, expiry);
if (!order) {
printf(" Build error: %s\n", order.error().message().c_str());
} else {
auto r = clob.place_order(*order);
if (!r) {
printf(" Place error: %s\n", r.error().message().c_str());
} else {
printf(" success=%s status=%s id=%s\n",
r->success ? "yes" : "no",
r->status.c_str(), r->orderID.c_str());
if (!r->errorMsg.empty())
printf(" errorMsg: %s\n", r->errorMsg.c_str());
if (r->success) gtd_order_id = r->orderID;
}
}
}
// ── 6. FOK — Fill or Kill ─────────────────────────────────────────────────
// Aggressive. Must fill entirely right now or gets cancelled entirely.
// At 0.01 the price is too far from market — order will be cancelled.
printf("\n=== Place FOK order (BUY Yes @ 0.01, size 5) ===\n");
{
auto order = clob.build_order(token_yes, "0.01", "5", Side::Buy, OrderType::FOK);
if (!order) {
printf(" Build error: %s\n", order.error().message().c_str());
} else {
auto r = clob.place_order(*order);
if (!r) {
printf(" Place error: %s\n", r.error().message().c_str());
} else {
printf(" success=%s status=%s\n",
r->success ? "yes" : "no", r->status.c_str());
if (!r->errorMsg.empty())
printf(" errorMsg: %s\n", r->errorMsg.c_str());
if (!r->tradeIds.empty())
printf(" matched trade: %s\n", r->tradeIds[0].c_str());
}
}
}
// ── 7. FAK — Fill and Kill ────────────────────────────────────────────────
// Fills as much as possible right now, cancels the unfilled remainder.
// Partial fills are accepted (unlike FOK which requires full fill).
// At 0.01 nothing will fill, so the whole order is cancelled.
printf("\n=== Place FAK order (BUY Yes @ 0.01, size 5) ===\n");
{
auto order = clob.build_order(token_yes, "0.01", "5", Side::Buy, OrderType::FAK);
if (!order) {
printf(" Build error: %s\n", order.error().message().c_str());
} else {
auto r = clob.place_order(*order);
if (!r) {
printf(" Place error: %s\n", r.error().message().c_str());
} else {
printf(" success=%s status=%s\n",
r->success ? "yes" : "no", r->status.c_str());
if (!r->errorMsg.empty())
printf(" errorMsg: %s\n", r->errorMsg.c_str());
}
}
}
// ── 8. Batch placement ────────────────────────────────────────────────────
// POST /orders — place multiple orders in a single HTTP request.
// Each element of the response array corresponds to one submitted order.
printf("\n=== Place 2 orders in one request (BUY Yes + BUY No @ 0.10) ===\n");
std::vector<std::string> batch_ids;
{
auto o1 = clob.build_order(token_yes, "0.10", "5", Side::Buy);
auto o2 = clob.build_order(token_no, "0.10", "5", Side::Buy);
if (o1 && o2) {
auto r = clob.place_orders({*o1, *o2});
if (!r) {
printf(" Error: %s\n", r.error().message().c_str());
} else {
for (auto& resp : *r) {
printf(" success=%s status=%-10s id=%s\n",
resp.success ? "yes" : "no ",
resp.status.c_str(), resp.orderID.c_str());
if (!resp.errorMsg.empty())
printf(" errorMsg: %s\n", resp.errorMsg.c_str());
if (resp.success && !resp.orderID.empty())
batch_ids.push_back(resp.orderID);
}
}
}
}
// ── 9. List open orders ───────────────────────────────────────────────────
printf("\n=== Open orders ===\n");
{
auto r = clob.get_orders();
if (!r) {
printf(" Error: %s\n", r.error().message().c_str());
} else {
printf(" count=%zu next_cursor=%s\n",
r->data.size(), r->next_cursor.c_str());
for (int i = 0; i < 10 && i < (int)r->data.size(); ++i) {
auto& o = r->data[i];
printf(" %s %-4s @%s size=%s status=%s\n",
o.id.substr(0, 8).c_str(),
o.side.c_str(), o.price.c_str(),
o.original_size.c_str(), o.status.c_str());
}
}
}
// ── 10. Cancel orders ─────────────────────────────────────────────────────
if (!gtc_order_id.empty()) {
printf("\n=== Cancel GTC order ===\n");
auto r = clob.cancel_order(gtc_order_id);
if (!r) printf(" Error: %s\n", r.error().message().c_str());
else printf(" Cancelled: %zu not_cancelled: %zu\n",
r->canceled.size(), r->not_canceled.size());
}
if (!batch_ids.empty()) {
printf("\n=== Cancel batch orders ===\n");
auto r = clob.cancel_orders(batch_ids);
if (!r) printf(" Error: %s\n", r.error().message().c_str());
else printf(" Cancelled: %zu\n", r->canceled.size());
}
// cancel_all cleans up whatever is still open (GTD + anything else)
printf("\n=== Cancel all remaining open orders ===\n");
{
auto r = clob.cancel_all();
if (!r) printf(" Error: %s\n", r.error().message().c_str());
else printf(" Cancelled: %zu\n", r->canceled.size());
}
// ── 11. Trade history ─────────────────────────────────────────────────────
printf("\n=== My recent trades (first page) ===\n");
{
auto r = clob.get_trades();
if (!r) {
printf(" Error: %s\n", r.error().message().c_str());
} else {
printf(" total=%zu next_cursor=%s\n",
r->data.size(), r->next_cursor.c_str());
for (int i = 0; i < 5 && i < (int)r->data.size(); ++i) {
auto& t = r->data[i];
printf(" %-4s size=%-10s @%s status=%s trader_side=%s\n",
t.side.c_str(), t.size.c_str(), t.price.c_str(),
t.status.c_str(), t.trader_side.c_str());
}
}
}
cleanse_credentials(creds);
return 0;
}