context-markets 0.4.0 → 0.5.1
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/LICENSE +21 -0
- package/README.md +38 -160
- package/dist/index.cjs +131 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +70 -22
- package/dist/index.d.ts +70 -22
- package/dist/index.js +124 -71
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Context
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://mainnet.contextcdn.com/ced823d63df9dff0390d9ad0a4e1ad3905dd199a6c50758c18a5c92a203adbd7" alt="Context" width="100%" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">Context SDK</h1>
|
|
6
|
+
<p align="center">TypeScript SDK for trading on <a href="https://context.markets">Context Markets</a> — an AI-powered prediction market platform on Base.</p>
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="https://www.npmjs.com/package/context-markets"><img src="https://img.shields.io/npm/v/context-markets" alt="npm" /></a>
|
|
10
|
+
<a href="https://github.com/contextwtf/context-sdk/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="License: MIT" /></a>
|
|
11
|
+
<a href="https://discord.gg/RVmzZsAyM4"><img src="https://img.shields.io/badge/Discord-Join-7289da" alt="Discord" /></a>
|
|
12
|
+
</p>
|
|
8
13
|
|
|
9
14
|
## Install
|
|
10
15
|
|
|
11
16
|
```bash
|
|
12
17
|
npm install context-markets
|
|
18
|
+
# or
|
|
19
|
+
yarn add context-markets
|
|
20
|
+
# or
|
|
21
|
+
pnpm add context-markets
|
|
13
22
|
```
|
|
14
23
|
|
|
15
24
|
## Quick Start
|
|
@@ -24,16 +33,13 @@ const ctx = new ContextClient();
|
|
|
24
33
|
// Search and list markets
|
|
25
34
|
const { markets } = await ctx.markets.list({ query: "elections", status: "active" });
|
|
26
35
|
|
|
27
|
-
// Get market details
|
|
28
|
-
const market = await ctx.markets.get(markets[0].id);
|
|
29
|
-
|
|
30
36
|
// Get quotes, orderbook, oracle
|
|
31
|
-
const quotes = await ctx.markets.quotes(
|
|
32
|
-
const book = await ctx.markets.orderbook(
|
|
33
|
-
const oracle = await ctx.markets.oracle(
|
|
37
|
+
const quotes = await ctx.markets.quotes(markets[0].id);
|
|
38
|
+
const book = await ctx.markets.orderbook(markets[0].id);
|
|
39
|
+
const oracle = await ctx.markets.oracle(markets[0].id);
|
|
34
40
|
|
|
35
41
|
// Simulate a trade before placing
|
|
36
|
-
const sim = await ctx.markets.simulate(
|
|
42
|
+
const sim = await ctx.markets.simulate(markets[0].id, {
|
|
37
43
|
side: "yes",
|
|
38
44
|
amount: 10,
|
|
39
45
|
amountType: "usd",
|
|
@@ -66,10 +72,6 @@ await ctx.orders.cancel(result.order.nonce);
|
|
|
66
72
|
### Wallet Setup & Deposits
|
|
67
73
|
|
|
68
74
|
```ts
|
|
69
|
-
// Check wallet status
|
|
70
|
-
const status = await ctx.account.status();
|
|
71
|
-
console.log(status.needsApprovals); // true if approvals needed
|
|
72
|
-
|
|
73
75
|
// One-call setup: approves USDC + operator
|
|
74
76
|
await ctx.account.setup();
|
|
75
77
|
|
|
@@ -81,115 +83,16 @@ await ctx.account.gaslessSetup();
|
|
|
81
83
|
await ctx.account.gaslessDeposit(100);
|
|
82
84
|
```
|
|
83
85
|
|
|
84
|
-
### Create a Market
|
|
86
|
+
### Create a Market
|
|
85
87
|
|
|
86
88
|
```ts
|
|
87
|
-
// Submit a question and wait for AI processing
|
|
88
89
|
const submission = await ctx.questions.submitAndWait(
|
|
89
90
|
"Will BTC close above $100k by Dec 31, 2026?"
|
|
90
91
|
);
|
|
91
|
-
|
|
92
|
-
// Create the on-chain market
|
|
93
92
|
const { marketId } = await ctx.markets.create(submission.questions[0].id);
|
|
94
93
|
```
|
|
95
94
|
|
|
96
|
-
Need an API key? Visit [context.markets](https://context.markets) or
|
|
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
|
-
```
|
|
95
|
+
Need an API key? Visit [context.markets](https://context.markets) or join our [Discord](https://discord.gg/RVmzZsAyM4).
|
|
193
96
|
|
|
194
97
|
## Configuration
|
|
195
98
|
|
|
@@ -202,53 +105,28 @@ new ContextClient({
|
|
|
202
105
|
})
|
|
203
106
|
```
|
|
204
107
|
|
|
205
|
-
|
|
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
|
|
108
|
+
The SDK accepts three signer formats: a private key string, a viem `Account` object, or a viem `WalletClient` (for browser wallets).
|
|
223
109
|
|
|
224
|
-
|
|
110
|
+
Prices are in **cents** (1–99). Sizes are in **contracts**. The SDK maps `"yes"` / `"no"` to the correct on-chain outcome index automatically.
|
|
225
111
|
|
|
226
|
-
|
|
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
|
|
112
|
+
## Documentation
|
|
235
113
|
|
|
236
|
-
|
|
114
|
+
- **[Quickstart Guide](https://docs.context.markets/agents/typescript-sdk)** — setup, authentication, and first trade
|
|
115
|
+
- **[API Reference](https://docs.context.markets/agents/typescript-sdk/api-reference)** — full method signatures for all modules
|
|
116
|
+
- **[Best Practices](https://docs.context.markets/agents/typescript-sdk/best-practices)** — patterns, error handling, and tips
|
|
117
|
+
- **[Examples](./examples/)** — runnable scripts for common workflows
|
|
237
118
|
|
|
238
|
-
|
|
239
|
-
|----------|---------|
|
|
240
|
-
| USDC | `0xBbee2756d3169CF7065e5E9C4A5EA9b1D1Fd415e` |
|
|
241
|
-
| Holdings | `0x0a6D61723E8AE8e34734A84075a1b58aB3eEca6a` |
|
|
242
|
-
| Settlement | `0xD91935a82Af48ff79a68134d9Eab8fc9e5d3504D` |
|
|
119
|
+
## Ecosystem
|
|
243
120
|
|
|
244
|
-
|
|
121
|
+
| Package | Description |
|
|
122
|
+
|---------|-------------|
|
|
123
|
+
| **[context-markets](https://github.com/contextwtf/context-sdk)** | TypeScript SDK for trading |
|
|
124
|
+
| **[context-markets-react](https://github.com/contextwtf/context-react)** | React hooks for market data and trading |
|
|
125
|
+
| **[context-markets-mcp](https://github.com/contextwtf/context-mcp)** | MCP server for AI agents |
|
|
126
|
+
| **[context-markets-cli](https://github.com/contextwtf/context-cli)** | CLI for trading from the terminal |
|
|
127
|
+
| **[context-skills](https://github.com/contextwtf/context-skills)** | AI agent skill files |
|
|
128
|
+
| **[context-plugin](https://github.com/contextwtf/context-plugin)** | Claude Code plugin |
|
|
245
129
|
|
|
246
|
-
|
|
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
|
-
```
|
|
130
|
+
## License
|
|
253
131
|
|
|
254
|
-
|
|
132
|
+
MIT — see [LICENSE](./LICENSE) for details.
|