context-markets 0.4.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.
- package/README.md +254 -0
- package/dist/index.cjs +1262 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5033 -0
- package/dist/index.d.ts +5033 -0
- package/dist/index.js +1228 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Context SDK
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/context-markets)
|
|
4
|
+
|
|
5
|
+
TypeScript SDK for trading on [Context Markets](https://context.markets) — an AI-powered prediction market platform on Base.
|
|
6
|
+
|
|
7
|
+
For the full quickstart guide, API reference, and developer docs, visit [docs.context.markets](https://docs.context.markets).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install context-markets
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
### Read Market Data (no auth)
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { ContextClient } from "context-markets";
|
|
21
|
+
|
|
22
|
+
const ctx = new ContextClient();
|
|
23
|
+
|
|
24
|
+
// Search and list markets
|
|
25
|
+
const { markets } = await ctx.markets.list({ query: "elections", status: "active" });
|
|
26
|
+
|
|
27
|
+
// Get market details
|
|
28
|
+
const market = await ctx.markets.get(markets[0].id);
|
|
29
|
+
|
|
30
|
+
// Get quotes, orderbook, oracle
|
|
31
|
+
const quotes = await ctx.markets.quotes(market.id);
|
|
32
|
+
const book = await ctx.markets.orderbook(market.id);
|
|
33
|
+
const oracle = await ctx.markets.oracle(market.id);
|
|
34
|
+
|
|
35
|
+
// Simulate a trade before placing
|
|
36
|
+
const sim = await ctx.markets.simulate(market.id, {
|
|
37
|
+
side: "yes",
|
|
38
|
+
amount: 10,
|
|
39
|
+
amountType: "usd",
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Place an Order (requires signer)
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { ContextClient } from "context-markets";
|
|
47
|
+
|
|
48
|
+
const ctx = new ContextClient({
|
|
49
|
+
apiKey: process.env.CONTEXT_API_KEY!,
|
|
50
|
+
signer: { privateKey: process.env.CONTEXT_PRIVATE_KEY! as `0x${string}` },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Place a limit order: buy 10 YES contracts at 45¢
|
|
54
|
+
const result = await ctx.orders.create({
|
|
55
|
+
marketId: "0x...",
|
|
56
|
+
outcome: "yes",
|
|
57
|
+
side: "buy",
|
|
58
|
+
priceCents: 45,
|
|
59
|
+
size: 10,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Cancel it
|
|
63
|
+
await ctx.orders.cancel(result.order.nonce);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Wallet Setup & Deposits
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// Check wallet status
|
|
70
|
+
const status = await ctx.account.status();
|
|
71
|
+
console.log(status.needsApprovals); // true if approvals needed
|
|
72
|
+
|
|
73
|
+
// One-call setup: approves USDC + operator
|
|
74
|
+
await ctx.account.setup();
|
|
75
|
+
|
|
76
|
+
// Deposit USDC into Holdings contract
|
|
77
|
+
await ctx.account.deposit(100); // 100 USDC
|
|
78
|
+
|
|
79
|
+
// Or use gasless (no ETH needed):
|
|
80
|
+
await ctx.account.gaslessSetup();
|
|
81
|
+
await ctx.account.gaslessDeposit(100);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Create a Market from a Question
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
// Submit a question and wait for AI processing
|
|
88
|
+
const submission = await ctx.questions.submitAndWait(
|
|
89
|
+
"Will BTC close above $100k by Dec 31, 2026?"
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Create the on-chain market
|
|
93
|
+
const { marketId } = await ctx.markets.create(submission.questions[0].id);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Need an API key? Visit [context.markets](https://context.markets) or reach out on Discord.
|
|
97
|
+
|
|
98
|
+
## API Reference
|
|
99
|
+
|
|
100
|
+
### `ctx.markets`
|
|
101
|
+
|
|
102
|
+
| Method | Description |
|
|
103
|
+
|--------|-------------|
|
|
104
|
+
| `list(params?)` | Search/filter markets |
|
|
105
|
+
| `get(id)` | Get market details |
|
|
106
|
+
| `quotes(marketId)` | Get bid/ask/last per outcome |
|
|
107
|
+
| `orderbook(marketId, params?)` | Get bid/ask ladder |
|
|
108
|
+
| `fullOrderbook(marketId)` | Combined yes + no orderbooks |
|
|
109
|
+
| `simulate(marketId, params)` | Simulate a trade (slippage, avg price) |
|
|
110
|
+
| `priceHistory(marketId, params?)` | Price time-series data |
|
|
111
|
+
| `oracle(marketId)` | Oracle resolution summary |
|
|
112
|
+
| `oracleQuotes(marketId)` | List oracle quotes |
|
|
113
|
+
| `requestOracleQuote(marketId)` | Request a new oracle quote |
|
|
114
|
+
| `activity(marketId, params?)` | Market event feed |
|
|
115
|
+
| `globalActivity(params?)` | Platform-wide activity feed |
|
|
116
|
+
| `create(questionId)` | Create on-chain market from question |
|
|
117
|
+
|
|
118
|
+
### `ctx.questions`
|
|
119
|
+
|
|
120
|
+
| Method | Description |
|
|
121
|
+
|--------|-------------|
|
|
122
|
+
| `submit(question)` | Submit a question for AI processing |
|
|
123
|
+
| `getSubmission(submissionId)` | Poll submission status |
|
|
124
|
+
| `submitAndWait(question, options?)` | Submit and poll until complete (default ~90s timeout) |
|
|
125
|
+
|
|
126
|
+
### `ctx.orders` (requires signer for writes)
|
|
127
|
+
|
|
128
|
+
| Method | Auth | Description |
|
|
129
|
+
|--------|------|-------------|
|
|
130
|
+
| `list(params?)` | — | Query orders with filters |
|
|
131
|
+
| `listAll(params?)` | — | Paginate through all matching orders |
|
|
132
|
+
| `mine(marketId?)` | signer | Your orders (shorthand for list with your address) |
|
|
133
|
+
| `allMine(marketId?)` | signer | Paginate all your orders |
|
|
134
|
+
| `get(id)` | — | Get single order by ID |
|
|
135
|
+
| `recent(params?)` | — | Recent orders within time window |
|
|
136
|
+
| `simulate(params)` | — | Simulate order fill (levels, fees, collateral) |
|
|
137
|
+
| `create(req)` | signer | Place a signed limit order |
|
|
138
|
+
| `createMarket(req)` | signer | Place a signed market order |
|
|
139
|
+
| `cancel(nonce)` | signer | Cancel by nonce |
|
|
140
|
+
| `cancelReplace(cancelNonce, newOrder)` | signer | Atomic cancel + new order |
|
|
141
|
+
| `bulkCreate(orders)` | signer | Place multiple orders |
|
|
142
|
+
| `bulkCancel(nonces)` | signer | Cancel multiple orders |
|
|
143
|
+
| `bulk(creates, cancelNonces)` | signer | Mixed creates + cancels in one call |
|
|
144
|
+
|
|
145
|
+
### `ctx.portfolio`
|
|
146
|
+
|
|
147
|
+
| Method | Description |
|
|
148
|
+
|--------|-------------|
|
|
149
|
+
| `get(address?, params?)` | Positions across markets (defaults to signer) |
|
|
150
|
+
| `claimable(address?)` | Positions eligible for claim after resolution |
|
|
151
|
+
| `stats(address?)` | Portfolio value, P&L, prediction count |
|
|
152
|
+
| `balance(address?)` | USDC + outcome token balances |
|
|
153
|
+
| `tokenBalance(address, tokenAddress)` | Single token balance |
|
|
154
|
+
|
|
155
|
+
### `ctx.account` (requires signer)
|
|
156
|
+
|
|
157
|
+
| Method | Description |
|
|
158
|
+
|--------|-------------|
|
|
159
|
+
| `status()` | Check ETH balance, USDC allowance, operator approval |
|
|
160
|
+
| `setup()` | Approve USDC + operator in one call |
|
|
161
|
+
| `mintTestUsdc(amount?)` | Mint testnet USDC (default: 1000) |
|
|
162
|
+
| `deposit(amount)` | Deposit USDC into Holdings |
|
|
163
|
+
| `withdraw(amount)` | Withdraw USDC from Holdings |
|
|
164
|
+
| `mintCompleteSets(marketId, amount)` | Mint YES+NO token pairs |
|
|
165
|
+
| `burnCompleteSets(marketId, amount)` | Burn pairs to recover USDC |
|
|
166
|
+
| `gaslessSetup()` | Approve operator via signature relay (no ETH needed) |
|
|
167
|
+
| `gaslessDeposit(amount)` | Deposit via Permit2 signature relay (no ETH needed) |
|
|
168
|
+
|
|
169
|
+
## Pricing
|
|
170
|
+
|
|
171
|
+
Prices are in **cents** (1-99). Sizes are in **contracts**. The SDK handles on-chain encoding internally.
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
45¢ = 45% probability = 0.45 USDC per contract
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The SDK maps `outcome: "yes"` / `outcome: "no"` to the correct on-chain `outcomeIndex` for you.
|
|
178
|
+
|
|
179
|
+
## Signer Options
|
|
180
|
+
|
|
181
|
+
The SDK accepts three signer formats:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
// Private key (most common for scripts/bots)
|
|
185
|
+
new ContextClient({ signer: { privateKey: "0x..." } })
|
|
186
|
+
|
|
187
|
+
// Viem Account object
|
|
188
|
+
new ContextClient({ signer: { account: viemAccount } })
|
|
189
|
+
|
|
190
|
+
// Viem WalletClient (for browser wallets)
|
|
191
|
+
new ContextClient({ signer: { walletClient: viemWalletClient } })
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Configuration
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
new ContextClient({
|
|
198
|
+
apiKey: "ctx_pk_...", // Required for authenticated endpoints
|
|
199
|
+
baseUrl: "https://...", // Override API base URL
|
|
200
|
+
rpcUrl: "https://...", // Override RPC URL for on-chain reads
|
|
201
|
+
signer: { privateKey: "0x..." } // Required for order signing & wallet ops
|
|
202
|
+
})
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Examples
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
npx tsx examples/basic-read.ts
|
|
209
|
+
CONTEXT_API_KEY=... CONTEXT_PRIVATE_KEY=0x... npx tsx examples/place-order.ts
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
| Example | Description |
|
|
213
|
+
|---------|-------------|
|
|
214
|
+
| `basic-read.ts` | Search markets, read quotes/orderbook/oracle (no auth) |
|
|
215
|
+
| `place-order.ts` | Place, query, and cancel orders |
|
|
216
|
+
| `setup-trader-wallet.ts` | Check + auto-approve wallet for trading |
|
|
217
|
+
| `deposit-usdc.ts` | Deposit USDC into Holdings contract |
|
|
218
|
+
| `audit-book.ts` | Audit all active orderbooks and open orders |
|
|
219
|
+
| `watch-markets.ts` | Poll and watch price changes on active markets |
|
|
220
|
+
| `batch-markets.ts` | Fetch quotes, orderbooks, and oracle data in parallel |
|
|
221
|
+
|
|
222
|
+
## Code Generation
|
|
223
|
+
|
|
224
|
+
Types and endpoints are auto-generated from the [OpenAPI spec](https://api-testnet.context.markets/v2/openapi.json):
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
bun run generate # Regenerate from production spec
|
|
228
|
+
bun scripts/generate-api.ts URL # Regenerate from a custom spec URL
|
|
229
|
+
bun run generate:check # Regenerate + verify no drift (CI)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Generated files live in `src/generated/` and are committed to git. SDK types in `src/types.ts` are aliases to the generated schemas, so they stay in sync automatically.
|
|
233
|
+
|
|
234
|
+
## Network
|
|
235
|
+
|
|
236
|
+
Currently targeting **Base Sepolia** (chain ID 84532) testnet.
|
|
237
|
+
|
|
238
|
+
| Contract | Address |
|
|
239
|
+
|----------|---------|
|
|
240
|
+
| USDC | `0xBbee2756d3169CF7065e5E9C4A5EA9b1D1Fd415e` |
|
|
241
|
+
| Holdings | `0x0a6D61723E8AE8e34734A84075a1b58aB3eEca6a` |
|
|
242
|
+
| Settlement | `0xD91935a82Af48ff79a68134d9Eab8fc9e5d3504D` |
|
|
243
|
+
|
|
244
|
+
## Development
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
bun install # Install dependencies
|
|
248
|
+
bun run build # Build ESM + CJS + types
|
|
249
|
+
bun run typecheck # Type check
|
|
250
|
+
bun run test # Run unit tests
|
|
251
|
+
bun run generate # Regenerate from OpenAPI spec
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Requires Node 18+.
|