@alpha-arcade/sdk 0.2.6 → 0.2.8
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 +58 -3
- package/dist/index.cjs +77 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +77 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,24 @@ npm install @alpha-arcade/sdk algosdk @algorandfoundation/algokit-utils
|
|
|
12
12
|
|
|
13
13
|
`algosdk` and `@algorandfoundation/algokit-utils` are peer dependencies.
|
|
14
14
|
|
|
15
|
+
## Getting an API key
|
|
16
|
+
|
|
17
|
+
An API key is **optional**. Without it, you can still fetch markets on-chain, place orders, and use most SDK features. With an API key, you get richer market data, liquidity rewards information, and wallet order lookups, and more.
|
|
18
|
+
|
|
19
|
+
To get an API key:
|
|
20
|
+
|
|
21
|
+
1. Go to [alphaarcade.com](https://alphaarcade.com) and **sign up** with your email or Google account.
|
|
22
|
+
2. Open the **Account** page
|
|
23
|
+
3. Open the **Partners** tab.
|
|
24
|
+
4. Click **Create API key** and copy the key.
|
|
25
|
+
5. Add it to your environment (e.g. a `.env` file in the project root):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
ALPHA_API_KEY=your_api_key_here
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Then pass it when creating the client: `apiKey: process.env.ALPHA_API_KEY`.
|
|
32
|
+
|
|
15
33
|
## Quick Start
|
|
16
34
|
|
|
17
35
|
```typescript
|
|
@@ -53,6 +71,21 @@ const result = await client.createLimitOrder({
|
|
|
53
71
|
console.log(`Order created! Escrow app ID: ${result.escrowAppId}`);
|
|
54
72
|
```
|
|
55
73
|
|
|
74
|
+
## Examples
|
|
75
|
+
|
|
76
|
+
The repo includes runnable examples (use `npx tsx examples/<script>.ts`). Scripts that call the API (e.g. `get-orders.ts`, `get-reward-markets.ts`) need `ALPHA_API_KEY` in your `.env` — see [Getting an API key](#getting-an-api-key). Trading examples also need `TEST_MNEMONIC`.
|
|
77
|
+
|
|
78
|
+
| Script | Description |
|
|
79
|
+
|--------|-------------|
|
|
80
|
+
| `get-orders.ts` | Fetch all open orders for a wallet via the API (`getWalletOrdersFromApi`) |
|
|
81
|
+
| `get-reward-markets.ts` | Fetch reward markets and show liquidity reward info (`getRewardMarkets`) |
|
|
82
|
+
| `get-positions.ts` | List token positions across markets (`getPositions`) |
|
|
83
|
+
| `place-limit-order.ts` | Place a limit order |
|
|
84
|
+
| `place-market-order.ts` | Place a market order |
|
|
85
|
+
| `cancel-order.ts` | Cancel an open order |
|
|
86
|
+
| `split-merge.ts` | Split USDC into YES/NO and merge back |
|
|
87
|
+
| `simple-trading-bot.ts` | Example bot that scans markets and places market orders |
|
|
88
|
+
|
|
56
89
|
## API Reference
|
|
57
90
|
|
|
58
91
|
### AlphaClient
|
|
@@ -71,8 +104,8 @@ new AlphaClient(config: AlphaClientConfig)
|
|
|
71
104
|
| `activeAddress` | `string` | Yes | Your Algorand address |
|
|
72
105
|
| `matcherAppId` | `number` | Yes | Matcher contract app ID (mainnet: `3078581851`) |
|
|
73
106
|
| `usdcAssetId` | `number` | Yes | USDC ASA ID (mainnet: `31566704`) |
|
|
74
|
-
| `apiKey` | `string` | No | Alpha
|
|
75
|
-
| `apiBaseUrl` | `string` | No | API base URL (default: `https://
|
|
107
|
+
| `apiKey` | `string` | No | Alpha API key. If provided, `getLiveMarkets()` and related API methods use the platform for richer data (images, categories, volume, reward markets, wallet orders). If omitted, markets are discovered on-chain. |
|
|
108
|
+
| `apiBaseUrl` | `string` | No | API base URL (default: `https://platform.alphaarcade.com/api`) |
|
|
76
109
|
| `marketCreatorAddress` | `string` | No | Market creator address for on-chain discovery (defaults to Alpha Arcade mainnet) |
|
|
77
110
|
|
|
78
111
|
---
|
|
@@ -211,7 +244,7 @@ if (book.yes.bids.length > 0) {
|
|
|
211
244
|
|
|
212
245
|
#### `getOpenOrders(marketAppId, walletAddress?)`
|
|
213
246
|
|
|
214
|
-
Gets open orders for a wallet on a specific market.
|
|
247
|
+
Gets open orders for a wallet on a specific market (from on-chain data).
|
|
215
248
|
|
|
216
249
|
```typescript
|
|
217
250
|
const orders = await client.getOpenOrders(123456789);
|
|
@@ -222,6 +255,17 @@ for (const order of orders) {
|
|
|
222
255
|
}
|
|
223
256
|
```
|
|
224
257
|
|
|
258
|
+
#### `getWalletOrdersFromApi(walletAddress)`
|
|
259
|
+
|
|
260
|
+
Gets all open orders for a wallet across every live market via the Alpha REST API. Requires `apiKey`. Paginates automatically.
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
const orders = await client.getWalletOrdersFromApi('ALGO_ADDRESS...');
|
|
264
|
+
for (const order of orders) {
|
|
265
|
+
console.log(`Market ${order.marketAppId} | Escrow ${order.escrowAppId} | ${order.quantityFilled / 1e6} filled`);
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
225
269
|
---
|
|
226
270
|
|
|
227
271
|
### Markets
|
|
@@ -259,6 +303,17 @@ const markets = await client.getLiveMarketsFromApi();
|
|
|
259
303
|
const market = await client.getMarketFromApi('uuid-here');
|
|
260
304
|
```
|
|
261
305
|
|
|
306
|
+
#### `getRewardMarkets()`
|
|
307
|
+
|
|
308
|
+
Fetches markets that have liquidity rewards from the Alpha REST API. Requires `apiKey`. Returns the same `Market[]` shape with reward fields populated: `totalRewards`, `rewardsPaidOut`, `rewardsSpreadDistance`, `rewardsMinContracts`, `lastRewardAmount`, `lastRewardTs`.
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
const rewardMarkets = await client.getRewardMarkets();
|
|
312
|
+
for (const m of rewardMarkets) {
|
|
313
|
+
console.log(`${m.title}: $${(m.totalRewards ?? 0) / 1e6} total rewards`);
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
262
317
|
---
|
|
263
318
|
|
|
264
319
|
### Utility Functions
|
package/dist/index.cjs
CHANGED
|
@@ -1427,6 +1427,12 @@ var calculateMatchingOrders = (orderbook, isBuying, isYes, quantity, price, slip
|
|
|
1427
1427
|
}
|
|
1428
1428
|
return matches;
|
|
1429
1429
|
};
|
|
1430
|
+
|
|
1431
|
+
// src/constants.ts
|
|
1432
|
+
var DEFAULT_API_BASE_URL = "https://platform.alphaarcade.com/api";
|
|
1433
|
+
var DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
|
|
1434
|
+
|
|
1435
|
+
// src/modules/orderbook.ts
|
|
1430
1436
|
var getAllCreatedApplications = async (indexerClient, address, limit = 100) => {
|
|
1431
1437
|
let applications = [];
|
|
1432
1438
|
let nextToken;
|
|
@@ -1520,6 +1526,49 @@ var getOpenOrders = async (config, marketAppId, walletAddress) => {
|
|
|
1520
1526
|
owner: o.globalState.owner ?? ""
|
|
1521
1527
|
}));
|
|
1522
1528
|
};
|
|
1529
|
+
var normalizeApiOrder = (raw) => ({
|
|
1530
|
+
escrowAppId: Number(raw.escrowAppId ?? raw.orderId),
|
|
1531
|
+
marketAppId: Number(raw.marketAppId),
|
|
1532
|
+
position: raw.orderPosition ?? 0,
|
|
1533
|
+
side: raw.orderSide === "BUY" ? 1 : 0,
|
|
1534
|
+
price: raw.orderPrice ?? 0,
|
|
1535
|
+
quantity: raw.orderQuantity ?? 0,
|
|
1536
|
+
quantityFilled: raw.orderQuantityFilled ?? 0,
|
|
1537
|
+
slippage: raw.slippage ?? 0,
|
|
1538
|
+
owner: raw.senderWallet ?? ""
|
|
1539
|
+
});
|
|
1540
|
+
var getWalletOrdersFromApi = async (config, walletAddress) => {
|
|
1541
|
+
if (!config.apiKey) {
|
|
1542
|
+
throw new Error("apiKey is required for API-based market fetching. Retrieve an API key from the Alpha Arcade platform via the Account page and pass it to the client.");
|
|
1543
|
+
}
|
|
1544
|
+
const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
1545
|
+
const allOrders = [];
|
|
1546
|
+
let cursor;
|
|
1547
|
+
let hasMore = true;
|
|
1548
|
+
while (hasMore) {
|
|
1549
|
+
const params = new URLSearchParams({ wallet: walletAddress, limit: "300" });
|
|
1550
|
+
if (cursor) {
|
|
1551
|
+
params.set("cursor", cursor);
|
|
1552
|
+
}
|
|
1553
|
+
const url = `${baseUrl}/get-wallet-orders?${params.toString()}`;
|
|
1554
|
+
const response = await fetch(url, { headers: { "x-api-key": config.apiKey } });
|
|
1555
|
+
if (!response.ok) {
|
|
1556
|
+
throw new Error(`Alpha API error: ${response.status} ${response.statusText}`);
|
|
1557
|
+
}
|
|
1558
|
+
const data = await response.json();
|
|
1559
|
+
if (Array.isArray(data)) {
|
|
1560
|
+
allOrders.push(...data.map(normalizeApiOrder));
|
|
1561
|
+
hasMore = false;
|
|
1562
|
+
} else if (data.orders) {
|
|
1563
|
+
allOrders.push(...data.orders.map(normalizeApiOrder));
|
|
1564
|
+
cursor = data.nextCursor ?? void 0;
|
|
1565
|
+
hasMore = data.hasMore === true && !!cursor;
|
|
1566
|
+
} else {
|
|
1567
|
+
hasMore = false;
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return allOrders;
|
|
1571
|
+
};
|
|
1523
1572
|
|
|
1524
1573
|
// src/modules/trading.ts
|
|
1525
1574
|
var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
|
|
@@ -1981,8 +2030,6 @@ var getPositions = async (config, walletAddress) => {
|
|
|
1981
2030
|
};
|
|
1982
2031
|
|
|
1983
2032
|
// src/modules/markets.ts
|
|
1984
|
-
var DEFAULT_API_BASE_URL = "https://partners.alphaarcade.com/api";
|
|
1985
|
-
var DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
|
|
1986
2033
|
var toSeconds = (ts) => ts > 1e10 ? Math.floor(ts / 1e3) : ts;
|
|
1987
2034
|
var groupMultiChoiceMarkets = (flatMarkets) => {
|
|
1988
2035
|
const parentMap = /* @__PURE__ */ new Map();
|
|
@@ -2096,7 +2143,7 @@ var getMarketOnChain = async (config, marketAppId) => {
|
|
|
2096
2143
|
};
|
|
2097
2144
|
var getLiveMarketsFromApi = async (config) => {
|
|
2098
2145
|
if (!config.apiKey) {
|
|
2099
|
-
throw new Error("apiKey is required for API-based market fetching. Use getMarketsOnChain() instead, or
|
|
2146
|
+
throw new Error("apiKey is required for API-based market fetching. Use getMarketsOnChain() instead, or retrieve an API key from the Alpha Arcade platform via the Account page and pass it to the client.");
|
|
2100
2147
|
}
|
|
2101
2148
|
const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
2102
2149
|
const allMarkets = [];
|
|
@@ -2133,7 +2180,7 @@ var getLiveMarketsFromApi = async (config) => {
|
|
|
2133
2180
|
};
|
|
2134
2181
|
var getMarketFromApi = async (config, marketId) => {
|
|
2135
2182
|
if (!config.apiKey) {
|
|
2136
|
-
throw new Error("apiKey is required for API-based market fetching. Use getMarketOnChain() instead, or
|
|
2183
|
+
throw new Error("apiKey is required for API-based market fetching. Use getMarketOnChain() instead, or retrieve an API key from the Alpha Arcade platform via the Account page and pass it to the client.");
|
|
2137
2184
|
}
|
|
2138
2185
|
const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
2139
2186
|
const url = `${baseUrl}/get-market?marketId=${encodeURIComponent(marketId)}`;
|
|
@@ -2150,6 +2197,13 @@ var getMarketFromApi = async (config, marketId) => {
|
|
|
2150
2197
|
}
|
|
2151
2198
|
return market;
|
|
2152
2199
|
};
|
|
2200
|
+
var getRewardMarkets = async (config) => {
|
|
2201
|
+
if (!config.apiKey) {
|
|
2202
|
+
throw new Error("apiKey is required for API-based market fetching. Retrieve an API key from the Alpha Arcade platform via the Account page and pass it to the client.");
|
|
2203
|
+
}
|
|
2204
|
+
const markets = await getLiveMarketsFromApi(config);
|
|
2205
|
+
return markets.filter((m) => m.totalRewards && m.totalRewards > 0);
|
|
2206
|
+
};
|
|
2153
2207
|
var getLiveMarkets = async (config) => {
|
|
2154
2208
|
if (config.apiKey) {
|
|
2155
2209
|
return getLiveMarketsFromApi(config);
|
|
@@ -2175,7 +2229,7 @@ var AlphaClient = class {
|
|
|
2175
2229
|
if (!config.usdcAssetId) throw new Error("usdcAssetId is required");
|
|
2176
2230
|
this.config = {
|
|
2177
2231
|
...config,
|
|
2178
|
-
apiBaseUrl: config.apiBaseUrl ??
|
|
2232
|
+
apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL
|
|
2179
2233
|
};
|
|
2180
2234
|
}
|
|
2181
2235
|
// ============================================
|
|
@@ -2307,6 +2361,15 @@ var AlphaClient = class {
|
|
|
2307
2361
|
async getOpenOrders(marketAppId, walletAddress) {
|
|
2308
2362
|
return getOpenOrders(this.config, marketAppId, walletAddress);
|
|
2309
2363
|
}
|
|
2364
|
+
/**
|
|
2365
|
+
* Gets all open orders for a wallet across every live market using the Alpha REST API.
|
|
2366
|
+
*
|
|
2367
|
+
* @param walletAddress - The wallet address
|
|
2368
|
+
* @returns Array of open orders for the wallet
|
|
2369
|
+
*/
|
|
2370
|
+
async getWalletOrdersFromApi(walletAddress) {
|
|
2371
|
+
return getWalletOrdersFromApi(this.config, walletAddress);
|
|
2372
|
+
}
|
|
2310
2373
|
// ============================================
|
|
2311
2374
|
// Markets
|
|
2312
2375
|
// ============================================
|
|
@@ -2364,6 +2427,14 @@ var AlphaClient = class {
|
|
|
2364
2427
|
async getLiveMarketsFromApi() {
|
|
2365
2428
|
return getLiveMarketsFromApi(this.config);
|
|
2366
2429
|
}
|
|
2430
|
+
/**
|
|
2431
|
+
* Fetches the reward markets from the Alpha REST API (requires API key).
|
|
2432
|
+
*
|
|
2433
|
+
* @returns Array of reward markets
|
|
2434
|
+
*/
|
|
2435
|
+
async getRewardMarkets() {
|
|
2436
|
+
return getRewardMarkets(this.config);
|
|
2437
|
+
}
|
|
2367
2438
|
/**
|
|
2368
2439
|
* Fetches a single market by ID from the Alpha REST API (requires API key).
|
|
2369
2440
|
*
|
|
@@ -2376,6 +2447,7 @@ var AlphaClient = class {
|
|
|
2376
2447
|
};
|
|
2377
2448
|
|
|
2378
2449
|
exports.AlphaClient = AlphaClient;
|
|
2450
|
+
exports.DEFAULT_API_BASE_URL = DEFAULT_API_BASE_URL;
|
|
2379
2451
|
exports.DEFAULT_MARKET_CREATOR_ADDRESS = DEFAULT_MARKET_CREATOR_ADDRESS;
|
|
2380
2452
|
exports.calculateFee = calculateFee;
|
|
2381
2453
|
exports.calculateFeeFromTotal = calculateFeeFromTotal;
|