context-markets-cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,313 @@
1
+ import {
2
+ formatAddress,
3
+ formatDate,
4
+ formatPrice,
5
+ formatVolume,
6
+ truncate
7
+ } from "./chunk-IRVQREUN.js";
8
+ import {
9
+ readClient,
10
+ tradingClient
11
+ } from "./chunk-BUZKJ7FR.js";
12
+ import {
13
+ fail,
14
+ out,
15
+ requireFlag,
16
+ requirePositional
17
+ } from "./chunk-QC6BGRUQ.js";
18
+
19
+ // src/commands/markets.ts
20
+ var HELP = `Usage: context markets <subcommand> [options]
21
+
22
+ Subcommands:
23
+ list Browse markets
24
+ --status <status> Filter by status
25
+ --limit <n> Max results
26
+ --sort-by <field> Sort field
27
+ --sort <asc|desc> Sort direction
28
+ --visibility <value> Visibility filter
29
+ --resolution-status <value> Resolution status filter
30
+ --creator <address> Filter by creator
31
+ --category <slug> Filter by category
32
+ --cursor <token> Pagination cursor
33
+
34
+ search <query> Search markets by text
35
+ --limit <n> Max results (default: 10)
36
+ --offset <n> Offset for pagination
37
+
38
+ get <id> Get a single market by ID
39
+ quotes <id> Current quotes for a market
40
+ orderbook <id> Full orderbook (YES and NO sides)
41
+ --depth <n> Number of price levels
42
+
43
+ simulate <id> Simulate a trade
44
+ --side <yes|no> (required) Trade side
45
+ --amount <n> (required) Trade amount
46
+ --amount-type <usd|contracts> Amount type (default: usd)
47
+ --trader <address> Trader address
48
+
49
+ price-history <id> Price history for a market
50
+ --timeframe <1h|6h|1d|1w|1M|all> Timeframe
51
+
52
+ oracle <id> Oracle info for a market
53
+ oracle-quotes <id> Oracle quotes for a market
54
+ request-oracle-quote <id> Request a new oracle quote
55
+
56
+ activity <id> Activity feed for a market
57
+ --limit <n> Max results
58
+ --cursor <token> Pagination cursor
59
+
60
+ global-activity Global activity feed
61
+ --limit <n> Max results
62
+ --cursor <token> Pagination cursor
63
+
64
+ create <questionId> Create a market from a generated question ID
65
+
66
+ help Show this help text
67
+
68
+ Global options:
69
+ --api-key <key> Context API key (or CONTEXT_API_KEY env)`;
70
+ async function handleMarkets(parsed) {
71
+ const { subcommand, positional, flags } = parsed;
72
+ switch (subcommand) {
73
+ case "list":
74
+ return list(flags);
75
+ case "search":
76
+ return search(positional, flags);
77
+ case "get":
78
+ return get(positional, flags);
79
+ case "quotes":
80
+ return quotes(positional, flags);
81
+ case "orderbook":
82
+ return orderbook(positional, flags);
83
+ case "simulate":
84
+ return simulate(positional, flags);
85
+ case "price-history":
86
+ return priceHistory(positional, flags);
87
+ case "oracle":
88
+ return oracle(positional, flags);
89
+ case "oracle-quotes":
90
+ return oracleQuotes(positional, flags);
91
+ case "request-oracle-quote":
92
+ return requestOracleQuote(positional, flags);
93
+ case "activity":
94
+ return activity(positional, flags);
95
+ case "global-activity":
96
+ return globalActivity(flags);
97
+ case "create":
98
+ return create(positional, flags);
99
+ case "help":
100
+ case void 0:
101
+ console.log(HELP);
102
+ return;
103
+ default:
104
+ fail(`Unknown markets subcommand: "${subcommand}". Run "context markets help" for usage.`);
105
+ }
106
+ }
107
+ async function list(flags) {
108
+ const ctx = readClient(flags);
109
+ const result = await ctx.markets.list({
110
+ status: flags["status"] || void 0,
111
+ sortBy: flags["sort-by"] || void 0,
112
+ sort: flags["sort"] || void 0,
113
+ limit: flags["limit"] ? parseInt(flags["limit"], 10) : void 0,
114
+ cursor: flags["cursor"] || void 0,
115
+ visibility: flags["visibility"] || void 0,
116
+ resolutionStatus: flags["resolution-status"] || void 0,
117
+ creator: flags["creator"] || void 0,
118
+ category: flags["category"] || void 0
119
+ });
120
+ out(result, {
121
+ rows: result.markets || [],
122
+ columns: [
123
+ { key: "shortQuestion", label: "Question", format: (v) => truncate(v, 34) },
124
+ { key: "outcomePrices[1].buyPrice", label: "Yes", format: formatPrice },
125
+ { key: "outcomePrices[0].buyPrice", label: "No", format: formatPrice },
126
+ { key: "volume", label: "Volume", format: formatVolume },
127
+ { key: "status", label: "Status", format: (v) => String(v ?? "\u2014") }
128
+ ],
129
+ numbered: true,
130
+ emptyMessage: "No markets found.",
131
+ cursor: result.cursor || null
132
+ });
133
+ }
134
+ async function search(positional, flags) {
135
+ const q = requirePositional(positional, 0, "query", "context markets search <query>");
136
+ const ctx = readClient(flags);
137
+ const result = await ctx.markets.search({
138
+ q,
139
+ limit: flags["limit"] ? parseInt(flags["limit"], 10) : void 0,
140
+ offset: flags["offset"] ? parseInt(flags["offset"], 10) : void 0
141
+ });
142
+ out(result, {
143
+ rows: result.markets || [],
144
+ columns: [
145
+ { key: "shortQuestion", label: "Question", format: (v) => truncate(v, 34) },
146
+ { key: "outcomePrices[1].currentPrice", label: "Yes", format: formatPrice },
147
+ { key: "outcomePrices[0].currentPrice", label: "No", format: formatPrice },
148
+ { key: "volume", label: "Volume", format: formatVolume },
149
+ { key: "status", label: "Status", format: (v) => String(v ?? "\u2014") }
150
+ ],
151
+ numbered: true,
152
+ emptyMessage: "No markets found."
153
+ });
154
+ }
155
+ async function get(positional, flags) {
156
+ const id = requirePositional(positional, 0, "id", "context markets get <id>");
157
+ const ctx = readClient(flags);
158
+ const market = await ctx.markets.get(id);
159
+ const m = market;
160
+ out(market, {
161
+ detail: [
162
+ ["ID", String(m.id || "\u2014")],
163
+ ["Question", String(m.question || m.shortQuestion || "\u2014")],
164
+ ["Status", String(m.status || "\u2014")],
165
+ ["Yes", m.outcomePrices?.[1] ? `${formatPrice(m.outcomePrices[1].bestBid)} bid / ${formatPrice(m.outcomePrices[1].bestAsk)} ask` : "\u2014"],
166
+ ["No", m.outcomePrices?.[0] ? `${formatPrice(m.outcomePrices[0].bestBid)} bid / ${formatPrice(m.outcomePrices[0].bestAsk)} ask` : "\u2014"],
167
+ ["Volume", formatVolume(m.volume)],
168
+ ["24h Volume", formatVolume(m.volume24h)],
169
+ ["Participants", String(m.participantCount ?? "\u2014")],
170
+ ["Deadline", formatDate(m.deadline)],
171
+ ["Creator", formatAddress(m.creator || m.metadata?.creator)]
172
+ ]
173
+ });
174
+ }
175
+ async function quotes(positional, flags) {
176
+ const id = requirePositional(positional, 0, "id", "context markets quotes <id>");
177
+ const ctx = readClient(flags);
178
+ const result = await ctx.markets.quotes(id);
179
+ const q = result;
180
+ out(result, {
181
+ detail: [
182
+ ["Market", String(q.marketId || "\u2014")],
183
+ ["Yes", `${formatPrice(q.yes?.bid)} bid / ${formatPrice(q.yes?.ask)} ask / ${formatPrice(q.yes?.last)} last`],
184
+ ["No", `${formatPrice(q.no?.bid)} bid / ${formatPrice(q.no?.ask)} ask / ${formatPrice(q.no?.last)} last`],
185
+ ["Spread", q.spread != null ? `${formatPrice(q.spread)}` : "\u2014"]
186
+ ]
187
+ });
188
+ }
189
+ async function orderbook(positional, flags) {
190
+ const id = requirePositional(positional, 0, "id", "context markets orderbook <id>");
191
+ const ctx = readClient(flags);
192
+ const result = await ctx.markets.fullOrderbook(id, {
193
+ depth: flags["depth"] ? parseInt(flags["depth"], 10) : void 0
194
+ });
195
+ const ob = result;
196
+ out(result, {
197
+ detail: [
198
+ ["Market", String(ob.marketId || "\u2014")],
199
+ ["Yes Bids", (ob.yes?.bids || []).map((l) => `${formatPrice(l.price)} \xD7 ${l.size}`).join(", ") || "\u2014"],
200
+ ["Yes Asks", (ob.yes?.asks || []).map((l) => `${formatPrice(l.price)} \xD7 ${l.size}`).join(", ") || "\u2014"],
201
+ ["No Bids", (ob.no?.bids || []).map((l) => `${formatPrice(l.price)} \xD7 ${l.size}`).join(", ") || "\u2014"],
202
+ ["No Asks", (ob.no?.asks || []).map((l) => `${formatPrice(l.price)} \xD7 ${l.size}`).join(", ") || "\u2014"]
203
+ ]
204
+ });
205
+ }
206
+ async function simulate(positional, flags) {
207
+ const id = requirePositional(positional, 0, "id", "context markets simulate <id> --side <yes|no> --amount <n>");
208
+ const side = requireFlag(flags, "side", "context markets simulate <id> --side <yes|no> --amount <n>");
209
+ const amountRaw = requireFlag(flags, "amount", "context markets simulate <id> --side <yes|no> --amount <n>");
210
+ if (side !== "yes" && side !== "no") {
211
+ fail("--side must be 'yes' or 'no'", { received: side });
212
+ }
213
+ const amount = parseFloat(amountRaw);
214
+ if (isNaN(amount) || amount <= 0) {
215
+ fail("--amount must be a positive number", { received: amountRaw });
216
+ }
217
+ const ctx = readClient(flags);
218
+ const result = await ctx.markets.simulate(id, {
219
+ side,
220
+ amount,
221
+ amountType: flags["amount-type"] === "contracts" ? "contracts" : "usd",
222
+ trader: flags["trader"] || void 0
223
+ });
224
+ const sim = result;
225
+ out(result, {
226
+ detail: [
227
+ ["Market", String(sim.marketId || "\u2014")],
228
+ ["Side", String(sim.side || "\u2014")],
229
+ ["Amount", String(sim.amount ?? "\u2014")],
230
+ ["Est. Contracts", String(sim.estimatedContracts ?? "\u2014")],
231
+ ["Avg Price", formatPrice(sim.estimatedAvgPrice)],
232
+ ["Slippage", sim.estimatedSlippage != null ? `${(sim.estimatedSlippage * 100).toFixed(1)}%` : "\u2014"]
233
+ ]
234
+ });
235
+ }
236
+ async function priceHistory(positional, flags) {
237
+ const id = requirePositional(positional, 0, "id", "context markets price-history <id>");
238
+ const ctx = readClient(flags);
239
+ const result = await ctx.markets.priceHistory(id, {
240
+ timeframe: flags["timeframe"] || void 0
241
+ });
242
+ out(result);
243
+ }
244
+ async function oracle(positional, flags) {
245
+ const id = requirePositional(positional, 0, "id", "context markets oracle <id>");
246
+ const ctx = readClient(flags);
247
+ const result = await ctx.markets.oracle(id);
248
+ out(result);
249
+ }
250
+ async function oracleQuotes(positional, flags) {
251
+ const id = requirePositional(positional, 0, "id", "context markets oracle-quotes <id>");
252
+ const ctx = readClient(flags);
253
+ const result = await ctx.markets.oracleQuotes(id);
254
+ out(result);
255
+ }
256
+ async function requestOracleQuote(positional, flags) {
257
+ const id = requirePositional(positional, 0, "id", "context markets request-oracle-quote <id>");
258
+ const ctx = readClient(flags);
259
+ const result = await ctx.markets.requestOracleQuote(id);
260
+ out(result);
261
+ }
262
+ async function activity(positional, flags) {
263
+ const id = requirePositional(positional, 0, "id", "context markets activity <id>");
264
+ const ctx = readClient(flags);
265
+ const result = await ctx.markets.activity(id, {
266
+ limit: flags["limit"] ? parseInt(flags["limit"], 10) : void 0,
267
+ cursor: flags["cursor"] || void 0
268
+ });
269
+ const act = result;
270
+ out(result, {
271
+ rows: act.activity || [],
272
+ columns: [
273
+ { key: "type", label: "Type" },
274
+ { key: "timestamp", label: "Time", format: formatDate }
275
+ ],
276
+ numbered: true,
277
+ emptyMessage: "No activity found.",
278
+ cursor: act.pagination?.cursor || null
279
+ });
280
+ }
281
+ async function globalActivity(flags) {
282
+ const ctx = readClient(flags);
283
+ const result = await ctx.markets.globalActivity({
284
+ limit: flags["limit"] ? parseInt(flags["limit"], 10) : void 0,
285
+ cursor: flags["cursor"] || void 0
286
+ });
287
+ const act = result;
288
+ out(result, {
289
+ rows: act.activity || [],
290
+ columns: [
291
+ { key: "type", label: "Type" },
292
+ { key: "timestamp", label: "Time", format: formatDate }
293
+ ],
294
+ numbered: true,
295
+ emptyMessage: "No activity found.",
296
+ cursor: act.pagination?.cursor || null
297
+ });
298
+ }
299
+ async function create(positional, flags) {
300
+ const questionId = requirePositional(positional, 0, "questionId", "context markets create <questionId>");
301
+ const ctx = tradingClient(flags);
302
+ const result = await ctx.markets.create(questionId);
303
+ const r = result;
304
+ out(result, {
305
+ detail: [
306
+ ["Market ID", String(r.marketId || "\u2014")],
307
+ ["Tx Hash", String(r.txHash || "\u2014")]
308
+ ]
309
+ });
310
+ }
311
+ export {
312
+ handleMarkets as default
313
+ };