@omen.foundation/game-sdk 1.0.16 → 1.0.18
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 +51 -0
- package/dist/index.d.mts +152 -5
- package/dist/index.d.ts +152 -5
- package/dist/index.js +167 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +159 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -12,6 +12,12 @@ The SDK supports two modes:
|
|
|
12
12
|
npm install @omen.foundation/game-sdk
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
For **real-time updates** (balance & inventory), also install Ably:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install ably
|
|
19
|
+
```
|
|
20
|
+
|
|
15
21
|
## Quick Start
|
|
16
22
|
|
|
17
23
|
### Client Mode (OAuth Authentication)
|
|
@@ -188,6 +194,51 @@ if (sdk.isAuthenticated()) {
|
|
|
188
194
|
await sdk.logout();
|
|
189
195
|
```
|
|
190
196
|
|
|
197
|
+
### Real-Time Updates (Balance & Inventory)
|
|
198
|
+
|
|
199
|
+
So your game stays in sync when the user spends or receives OmenX / NFTs on the website or in another client, subscribe to the per-wallet Ably channel. The SDK provides the channel name, event names, and a subscribe helper with idempotency (eventId dedupe).
|
|
200
|
+
|
|
201
|
+
**Requires:** `ably` installed in your app. Use the same Ably API key as the OmenX frontend (subscribe capability for `wallet:*`).
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { subscribeWalletRealtime, getWalletChannelName, REALTIME_EVENTS } from '@omen.foundation/game-sdk';
|
|
205
|
+
import * as Ably from 'ably';
|
|
206
|
+
|
|
207
|
+
// After user logs in and you have their wallet address:
|
|
208
|
+
const ably = new Ably.Realtime({ key: 'YOUR_ABLY_API_KEY' });
|
|
209
|
+
|
|
210
|
+
const unsubscribe = subscribeWalletRealtime(ably, walletAddress, {
|
|
211
|
+
onBalance: (payload) => {
|
|
212
|
+
// Refetch balances (e.g. call your backend or GetPlayerBalances)
|
|
213
|
+
refetchBalances();
|
|
214
|
+
},
|
|
215
|
+
onInventory: (payload) => {
|
|
216
|
+
// Refetch inventory / NFTs (backend cache is source of truth)
|
|
217
|
+
refetchInventory();
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// When user logs out or wallet changes:
|
|
222
|
+
unsubscribe();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
With React and `ably/react`, pass the client from `useAbly()`:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { useAbly } from 'ably/react';
|
|
229
|
+
import { subscribeWalletRealtime } from '@omen.foundation/game-sdk';
|
|
230
|
+
|
|
231
|
+
// Inside your component, after wallet is available:
|
|
232
|
+
const ably = useAbly();
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
if (!walletAddress || !ably) return;
|
|
235
|
+
return subscribeWalletRealtime(ably, walletAddress, {
|
|
236
|
+
onBalance: () => refetchBalances(),
|
|
237
|
+
onInventory: () => refetchInventory(),
|
|
238
|
+
});
|
|
239
|
+
}, [ably, walletAddress]);
|
|
240
|
+
```
|
|
241
|
+
|
|
191
242
|
## API Reference
|
|
192
243
|
|
|
193
244
|
See the [full documentation](./docs/README.md) for detailed API reference.
|
package/dist/index.d.mts
CHANGED
|
@@ -276,13 +276,15 @@ declare class OmenXServerSDK {
|
|
|
276
276
|
* NFT Template Operations
|
|
277
277
|
*/
|
|
278
278
|
/**
|
|
279
|
-
* Get all NFT templates for
|
|
279
|
+
* Get all NFT templates for the authenticated game (game is derived from API key).
|
|
280
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
280
281
|
*/
|
|
281
|
-
getNftTemplates(
|
|
282
|
+
getNftTemplates(_gameId?: string): Promise<any>;
|
|
282
283
|
/**
|
|
283
|
-
* Get NFT contract address for
|
|
284
|
+
* Get NFT contract address for the authenticated game (game is derived from API key).
|
|
285
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
284
286
|
*/
|
|
285
|
-
getNftContract(
|
|
287
|
+
getNftContract(_gameId?: string): Promise<any>;
|
|
286
288
|
/**
|
|
287
289
|
* Mint NFTs (single or batch)
|
|
288
290
|
*/
|
|
@@ -344,6 +346,151 @@ declare class OmenXServerSDK {
|
|
|
344
346
|
phaseIndex?: number;
|
|
345
347
|
};
|
|
346
348
|
}>;
|
|
349
|
+
/**
|
|
350
|
+
* Quests API (game quests only).
|
|
351
|
+
* Requires scope: quests:read for getQuestsForPlayer; quests:write for assign, progress, complete, claim.
|
|
352
|
+
*/
|
|
353
|
+
/** Get assigned quests for a player. Optional questType filter. */
|
|
354
|
+
getQuestsForPlayer(wallet: string, options?: {
|
|
355
|
+
questType?: 'daily' | 'weekly' | 'monthly' | 'oneTime';
|
|
356
|
+
}): Promise<{
|
|
357
|
+
success: boolean;
|
|
358
|
+
data: {
|
|
359
|
+
quests: any[];
|
|
360
|
+
};
|
|
361
|
+
}>;
|
|
362
|
+
/** Assign quests of a type to a player. Use idempotencyKey for safe retries. */
|
|
363
|
+
assignQuests(wallet: string, questType: 'daily' | 'weekly' | 'monthly' | 'oneTime', count: number, options?: {
|
|
364
|
+
idempotencyKey?: string;
|
|
365
|
+
}): Promise<{
|
|
366
|
+
success: boolean;
|
|
367
|
+
data: {
|
|
368
|
+
assigned: number;
|
|
369
|
+
assignmentIds?: string[];
|
|
370
|
+
};
|
|
371
|
+
}>;
|
|
372
|
+
/** Report progress for a quest step. Quest auto-completes when all steps meet targets. */
|
|
373
|
+
reportQuestProgress(wallet: string, questKey: string, value: number, options?: {
|
|
374
|
+
stepKey?: string;
|
|
375
|
+
}): Promise<{
|
|
376
|
+
success: boolean;
|
|
377
|
+
data?: object;
|
|
378
|
+
}>;
|
|
379
|
+
/** Mark a quest complete (all steps set to targets). */
|
|
380
|
+
completeQuest(wallet: string, questKey: string): Promise<{
|
|
381
|
+
success: boolean;
|
|
382
|
+
data?: object;
|
|
383
|
+
}>;
|
|
384
|
+
/** Claim VIP reward for a completed quest (from game pool). Use idempotencyKey for safe retries. */
|
|
385
|
+
claimQuestReward(wallet: string, questKey: string, options?: {
|
|
386
|
+
idempotencyKey?: string;
|
|
387
|
+
}): Promise<{
|
|
388
|
+
success: boolean;
|
|
389
|
+
data?: {
|
|
390
|
+
pointsGranted?: number;
|
|
391
|
+
};
|
|
392
|
+
}>;
|
|
393
|
+
/**
|
|
394
|
+
* Reset or wipe a player's quest state for this game. Requires quests:write.
|
|
395
|
+
* - wipe: delete all quest assignments for this game for the player.
|
|
396
|
+
* - reset: delete assignments for the specified questKey only (questKey required).
|
|
397
|
+
*/
|
|
398
|
+
resetPlayerQuests(wallet: string, action: 'wipe' | 'reset', questKey?: string): Promise<{
|
|
399
|
+
success: boolean;
|
|
400
|
+
data: {
|
|
401
|
+
action: string;
|
|
402
|
+
deleted: number;
|
|
403
|
+
questKey?: string;
|
|
404
|
+
};
|
|
405
|
+
}>;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Real-time wallet updates (balance & inventory) for game clients.
|
|
410
|
+
* Subscribe to the per-wallet Ably channel so the game stays in sync when the user
|
|
411
|
+
* spends or receives OmenX / NFTs on the website or in another client.
|
|
412
|
+
*
|
|
413
|
+
* Two options:
|
|
414
|
+
* 1. subscribeWalletRealtimeWithOmenXAuth – no API key needed; SDK gets a scoped token from the OmenX platform using your access token.
|
|
415
|
+
* 2. subscribeWalletRealtime – pass your own Ably Realtime instance (e.g. from an API key or your own token).
|
|
416
|
+
*/
|
|
417
|
+
/** Payload for balance and inventory events. Use eventId for idempotency (ignore if already processed). */
|
|
418
|
+
interface RealtimeWalletPayload {
|
|
419
|
+
eventId: string;
|
|
420
|
+
timestamp: number;
|
|
421
|
+
reason?: string;
|
|
422
|
+
}
|
|
423
|
+
/** Ably channel name prefix. Full channel = "wallet:" + normalized wallet address. */
|
|
424
|
+
declare const WALLET_CHANNEL_PREFIX = "wallet:";
|
|
425
|
+
/** Event names published by the OmenX backend on the wallet channel. */
|
|
426
|
+
declare const REALTIME_EVENTS: {
|
|
427
|
+
/** Currency balance changed. Refetch via GetPlayerBalances or your balance API. */
|
|
428
|
+
readonly balance: "balance";
|
|
429
|
+
/** NFT/inventory changed. Refetch via GetPlayerNfts or your inventory API (cache is source of truth). */
|
|
430
|
+
readonly inventory: "inventory";
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Get the Ably channel name for a wallet. Use this when subscribing with your Ably client.
|
|
434
|
+
*/
|
|
435
|
+
declare function getWalletChannelName(walletAddress: string): string;
|
|
436
|
+
/**
|
|
437
|
+
* Minimal Ably Realtime interface so the SDK works with any Ably.Realtime instance.
|
|
438
|
+
* The app provides the real Ably client from AblyProvider or new Ably.Realtime({ key }).
|
|
439
|
+
*/
|
|
440
|
+
interface AblyRealtimeLike {
|
|
441
|
+
channels: {
|
|
442
|
+
get(name: string): {
|
|
443
|
+
subscribe(eventOrCallback: string | ((message: {
|
|
444
|
+
name: string;
|
|
445
|
+
data: unknown;
|
|
446
|
+
}) => void), callback?: (message: {
|
|
447
|
+
name: string;
|
|
448
|
+
data: unknown;
|
|
449
|
+
}) => void): void;
|
|
450
|
+
unsubscribe(eventOrCallback?: string | ((message: {
|
|
451
|
+
name: string;
|
|
452
|
+
data: unknown;
|
|
453
|
+
}) => void), callback?: (message: {
|
|
454
|
+
name: string;
|
|
455
|
+
data: unknown;
|
|
456
|
+
}) => void): void;
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Subscribe to real-time balance and inventory events for a wallet.
|
|
462
|
+
* Call when the user logs in; call the returned unsubscribe when they log out or wallet changes.
|
|
463
|
+
*
|
|
464
|
+
* Uses eventId to dedupe (ignores already-seen events). Refetch in your app on each callback.
|
|
465
|
+
*
|
|
466
|
+
* @param ably - Ably Realtime instance (e.g. from useAbly() or new Ably.Realtime({ key }))
|
|
467
|
+
* @param walletAddress - Current player wallet (0x...)
|
|
468
|
+
* @param callbacks - onBalance and/or onInventory; each receives the payload (eventId, timestamp, reason?)
|
|
469
|
+
* @returns Unsubscribe function
|
|
470
|
+
*/
|
|
471
|
+
declare function subscribeWalletRealtime(ably: AblyRealtimeLike, walletAddress: string, callbacks: {
|
|
472
|
+
onBalance?: (payload: RealtimeWalletPayload) => void;
|
|
473
|
+
onInventory?: (payload: RealtimeWalletPayload) => void;
|
|
474
|
+
}): () => void;
|
|
475
|
+
/** Options for subscribing via OmenX platform token (no Ably API key required). */
|
|
476
|
+
interface SubscribeWalletRealtimeWithOmenXAuthOptions {
|
|
477
|
+
/** Base URL of the OmenX API (e.g. https://api.omen.foundation). No trailing slash. */
|
|
478
|
+
apiBaseUrl: string;
|
|
479
|
+
/** Returns the current OmenX access token (from your OAuth flow). Called when Ably needs to authenticate. */
|
|
480
|
+
getAccessToken: () => Promise<string | null>;
|
|
481
|
+
/** Wallet address (0x...) to subscribe to. Must match the wallet in the access token. */
|
|
482
|
+
walletAddress: string;
|
|
483
|
+
onBalance?: (payload: RealtimeWalletPayload) => void;
|
|
484
|
+
onInventory?: (payload: RealtimeWalletPayload) => void;
|
|
347
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Subscribe to real-time balance and inventory for a wallet using OmenX platform auth.
|
|
488
|
+
* The SDK fetches a short-lived, subscribe-only Ably token from the OmenX API using your
|
|
489
|
+
* access token. No Ably API key or .env is required – ideal for third-party developers.
|
|
490
|
+
*
|
|
491
|
+
* Requires the app to have `ably` installed (peer dependency). Call when the user is
|
|
492
|
+
* logged in; call the returned function on logout or wallet change.
|
|
493
|
+
*/
|
|
494
|
+
declare function subscribeWalletRealtimeWithOmenXAuth(options: SubscribeWalletRealtimeWithOmenXAuthOptions): () => void;
|
|
348
495
|
|
|
349
|
-
export { type ApiCallOptions, type AuthData, type OAuthOptions, OmenXGameSDK, type OmenXGameSDKConfig, OmenXServerSDK, type UserInfo, type VipStatus, type VipTierInfo };
|
|
496
|
+
export { type AblyRealtimeLike, type ApiCallOptions, type AuthData, type OAuthOptions, OmenXGameSDK, type OmenXGameSDKConfig, OmenXServerSDK, REALTIME_EVENTS, type RealtimeWalletPayload, type SubscribeWalletRealtimeWithOmenXAuthOptions, type UserInfo, type VipStatus, type VipTierInfo, WALLET_CHANNEL_PREFIX, getWalletChannelName, subscribeWalletRealtime, subscribeWalletRealtimeWithOmenXAuth };
|
package/dist/index.d.ts
CHANGED
|
@@ -276,13 +276,15 @@ declare class OmenXServerSDK {
|
|
|
276
276
|
* NFT Template Operations
|
|
277
277
|
*/
|
|
278
278
|
/**
|
|
279
|
-
* Get all NFT templates for
|
|
279
|
+
* Get all NFT templates for the authenticated game (game is derived from API key).
|
|
280
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
280
281
|
*/
|
|
281
|
-
getNftTemplates(
|
|
282
|
+
getNftTemplates(_gameId?: string): Promise<any>;
|
|
282
283
|
/**
|
|
283
|
-
* Get NFT contract address for
|
|
284
|
+
* Get NFT contract address for the authenticated game (game is derived from API key).
|
|
285
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
284
286
|
*/
|
|
285
|
-
getNftContract(
|
|
287
|
+
getNftContract(_gameId?: string): Promise<any>;
|
|
286
288
|
/**
|
|
287
289
|
* Mint NFTs (single or batch)
|
|
288
290
|
*/
|
|
@@ -344,6 +346,151 @@ declare class OmenXServerSDK {
|
|
|
344
346
|
phaseIndex?: number;
|
|
345
347
|
};
|
|
346
348
|
}>;
|
|
349
|
+
/**
|
|
350
|
+
* Quests API (game quests only).
|
|
351
|
+
* Requires scope: quests:read for getQuestsForPlayer; quests:write for assign, progress, complete, claim.
|
|
352
|
+
*/
|
|
353
|
+
/** Get assigned quests for a player. Optional questType filter. */
|
|
354
|
+
getQuestsForPlayer(wallet: string, options?: {
|
|
355
|
+
questType?: 'daily' | 'weekly' | 'monthly' | 'oneTime';
|
|
356
|
+
}): Promise<{
|
|
357
|
+
success: boolean;
|
|
358
|
+
data: {
|
|
359
|
+
quests: any[];
|
|
360
|
+
};
|
|
361
|
+
}>;
|
|
362
|
+
/** Assign quests of a type to a player. Use idempotencyKey for safe retries. */
|
|
363
|
+
assignQuests(wallet: string, questType: 'daily' | 'weekly' | 'monthly' | 'oneTime', count: number, options?: {
|
|
364
|
+
idempotencyKey?: string;
|
|
365
|
+
}): Promise<{
|
|
366
|
+
success: boolean;
|
|
367
|
+
data: {
|
|
368
|
+
assigned: number;
|
|
369
|
+
assignmentIds?: string[];
|
|
370
|
+
};
|
|
371
|
+
}>;
|
|
372
|
+
/** Report progress for a quest step. Quest auto-completes when all steps meet targets. */
|
|
373
|
+
reportQuestProgress(wallet: string, questKey: string, value: number, options?: {
|
|
374
|
+
stepKey?: string;
|
|
375
|
+
}): Promise<{
|
|
376
|
+
success: boolean;
|
|
377
|
+
data?: object;
|
|
378
|
+
}>;
|
|
379
|
+
/** Mark a quest complete (all steps set to targets). */
|
|
380
|
+
completeQuest(wallet: string, questKey: string): Promise<{
|
|
381
|
+
success: boolean;
|
|
382
|
+
data?: object;
|
|
383
|
+
}>;
|
|
384
|
+
/** Claim VIP reward for a completed quest (from game pool). Use idempotencyKey for safe retries. */
|
|
385
|
+
claimQuestReward(wallet: string, questKey: string, options?: {
|
|
386
|
+
idempotencyKey?: string;
|
|
387
|
+
}): Promise<{
|
|
388
|
+
success: boolean;
|
|
389
|
+
data?: {
|
|
390
|
+
pointsGranted?: number;
|
|
391
|
+
};
|
|
392
|
+
}>;
|
|
393
|
+
/**
|
|
394
|
+
* Reset or wipe a player's quest state for this game. Requires quests:write.
|
|
395
|
+
* - wipe: delete all quest assignments for this game for the player.
|
|
396
|
+
* - reset: delete assignments for the specified questKey only (questKey required).
|
|
397
|
+
*/
|
|
398
|
+
resetPlayerQuests(wallet: string, action: 'wipe' | 'reset', questKey?: string): Promise<{
|
|
399
|
+
success: boolean;
|
|
400
|
+
data: {
|
|
401
|
+
action: string;
|
|
402
|
+
deleted: number;
|
|
403
|
+
questKey?: string;
|
|
404
|
+
};
|
|
405
|
+
}>;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Real-time wallet updates (balance & inventory) for game clients.
|
|
410
|
+
* Subscribe to the per-wallet Ably channel so the game stays in sync when the user
|
|
411
|
+
* spends or receives OmenX / NFTs on the website or in another client.
|
|
412
|
+
*
|
|
413
|
+
* Two options:
|
|
414
|
+
* 1. subscribeWalletRealtimeWithOmenXAuth – no API key needed; SDK gets a scoped token from the OmenX platform using your access token.
|
|
415
|
+
* 2. subscribeWalletRealtime – pass your own Ably Realtime instance (e.g. from an API key or your own token).
|
|
416
|
+
*/
|
|
417
|
+
/** Payload for balance and inventory events. Use eventId for idempotency (ignore if already processed). */
|
|
418
|
+
interface RealtimeWalletPayload {
|
|
419
|
+
eventId: string;
|
|
420
|
+
timestamp: number;
|
|
421
|
+
reason?: string;
|
|
422
|
+
}
|
|
423
|
+
/** Ably channel name prefix. Full channel = "wallet:" + normalized wallet address. */
|
|
424
|
+
declare const WALLET_CHANNEL_PREFIX = "wallet:";
|
|
425
|
+
/** Event names published by the OmenX backend on the wallet channel. */
|
|
426
|
+
declare const REALTIME_EVENTS: {
|
|
427
|
+
/** Currency balance changed. Refetch via GetPlayerBalances or your balance API. */
|
|
428
|
+
readonly balance: "balance";
|
|
429
|
+
/** NFT/inventory changed. Refetch via GetPlayerNfts or your inventory API (cache is source of truth). */
|
|
430
|
+
readonly inventory: "inventory";
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Get the Ably channel name for a wallet. Use this when subscribing with your Ably client.
|
|
434
|
+
*/
|
|
435
|
+
declare function getWalletChannelName(walletAddress: string): string;
|
|
436
|
+
/**
|
|
437
|
+
* Minimal Ably Realtime interface so the SDK works with any Ably.Realtime instance.
|
|
438
|
+
* The app provides the real Ably client from AblyProvider or new Ably.Realtime({ key }).
|
|
439
|
+
*/
|
|
440
|
+
interface AblyRealtimeLike {
|
|
441
|
+
channels: {
|
|
442
|
+
get(name: string): {
|
|
443
|
+
subscribe(eventOrCallback: string | ((message: {
|
|
444
|
+
name: string;
|
|
445
|
+
data: unknown;
|
|
446
|
+
}) => void), callback?: (message: {
|
|
447
|
+
name: string;
|
|
448
|
+
data: unknown;
|
|
449
|
+
}) => void): void;
|
|
450
|
+
unsubscribe(eventOrCallback?: string | ((message: {
|
|
451
|
+
name: string;
|
|
452
|
+
data: unknown;
|
|
453
|
+
}) => void), callback?: (message: {
|
|
454
|
+
name: string;
|
|
455
|
+
data: unknown;
|
|
456
|
+
}) => void): void;
|
|
457
|
+
};
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Subscribe to real-time balance and inventory events for a wallet.
|
|
462
|
+
* Call when the user logs in; call the returned unsubscribe when they log out or wallet changes.
|
|
463
|
+
*
|
|
464
|
+
* Uses eventId to dedupe (ignores already-seen events). Refetch in your app on each callback.
|
|
465
|
+
*
|
|
466
|
+
* @param ably - Ably Realtime instance (e.g. from useAbly() or new Ably.Realtime({ key }))
|
|
467
|
+
* @param walletAddress - Current player wallet (0x...)
|
|
468
|
+
* @param callbacks - onBalance and/or onInventory; each receives the payload (eventId, timestamp, reason?)
|
|
469
|
+
* @returns Unsubscribe function
|
|
470
|
+
*/
|
|
471
|
+
declare function subscribeWalletRealtime(ably: AblyRealtimeLike, walletAddress: string, callbacks: {
|
|
472
|
+
onBalance?: (payload: RealtimeWalletPayload) => void;
|
|
473
|
+
onInventory?: (payload: RealtimeWalletPayload) => void;
|
|
474
|
+
}): () => void;
|
|
475
|
+
/** Options for subscribing via OmenX platform token (no Ably API key required). */
|
|
476
|
+
interface SubscribeWalletRealtimeWithOmenXAuthOptions {
|
|
477
|
+
/** Base URL of the OmenX API (e.g. https://api.omen.foundation). No trailing slash. */
|
|
478
|
+
apiBaseUrl: string;
|
|
479
|
+
/** Returns the current OmenX access token (from your OAuth flow). Called when Ably needs to authenticate. */
|
|
480
|
+
getAccessToken: () => Promise<string | null>;
|
|
481
|
+
/** Wallet address (0x...) to subscribe to. Must match the wallet in the access token. */
|
|
482
|
+
walletAddress: string;
|
|
483
|
+
onBalance?: (payload: RealtimeWalletPayload) => void;
|
|
484
|
+
onInventory?: (payload: RealtimeWalletPayload) => void;
|
|
347
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Subscribe to real-time balance and inventory for a wallet using OmenX platform auth.
|
|
488
|
+
* The SDK fetches a short-lived, subscribe-only Ably token from the OmenX API using your
|
|
489
|
+
* access token. No Ably API key or .env is required – ideal for third-party developers.
|
|
490
|
+
*
|
|
491
|
+
* Requires the app to have `ably` installed (peer dependency). Call when the user is
|
|
492
|
+
* logged in; call the returned function on logout or wallet change.
|
|
493
|
+
*/
|
|
494
|
+
declare function subscribeWalletRealtimeWithOmenXAuth(options: SubscribeWalletRealtimeWithOmenXAuthOptions): () => void;
|
|
348
495
|
|
|
349
|
-
export { type ApiCallOptions, type AuthData, type OAuthOptions, OmenXGameSDK, type OmenXGameSDKConfig, OmenXServerSDK, type UserInfo, type VipStatus, type VipTierInfo };
|
|
496
|
+
export { type AblyRealtimeLike, type ApiCallOptions, type AuthData, type OAuthOptions, OmenXGameSDK, type OmenXGameSDKConfig, OmenXServerSDK, REALTIME_EVENTS, type RealtimeWalletPayload, type SubscribeWalletRealtimeWithOmenXAuthOptions, type UserInfo, type VipStatus, type VipTierInfo, WALLET_CHANNEL_PREFIX, getWalletChannelName, subscribeWalletRealtime, subscribeWalletRealtimeWithOmenXAuth };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var Ably = require('ably');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var Ably__default = /*#__PURE__*/_interopDefault(Ably);
|
|
8
|
+
|
|
3
9
|
// src/oauth.ts
|
|
4
10
|
var OAuthFlow = class {
|
|
5
11
|
constructor(config) {
|
|
@@ -630,17 +636,19 @@ var OmenXServerSDK = class {
|
|
|
630
636
|
* NFT Template Operations
|
|
631
637
|
*/
|
|
632
638
|
/**
|
|
633
|
-
* Get all NFT templates for
|
|
639
|
+
* Get all NFT templates for the authenticated game (game is derived from API key).
|
|
640
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
634
641
|
*/
|
|
635
|
-
async getNftTemplates(
|
|
636
|
-
const response = await this.apiCall(
|
|
642
|
+
async getNftTemplates(_gameId) {
|
|
643
|
+
const response = await this.apiCall("/v1/nfts/templates");
|
|
637
644
|
return response.json();
|
|
638
645
|
}
|
|
639
646
|
/**
|
|
640
|
-
* Get NFT contract address for
|
|
647
|
+
* Get NFT contract address for the authenticated game (game is derived from API key).
|
|
648
|
+
* gameId is optional and ignored by the API; included for backward compatibility.
|
|
641
649
|
*/
|
|
642
|
-
async getNftContract(
|
|
643
|
-
const response = await this.apiCall(
|
|
650
|
+
async getNftContract(_gameId) {
|
|
651
|
+
const response = await this.apiCall("/v1/nfts/contract");
|
|
644
652
|
return response.json();
|
|
645
653
|
}
|
|
646
654
|
/**
|
|
@@ -715,9 +723,162 @@ var OmenXServerSDK = class {
|
|
|
715
723
|
});
|
|
716
724
|
return response.json();
|
|
717
725
|
}
|
|
726
|
+
/**
|
|
727
|
+
* Quests API (game quests only).
|
|
728
|
+
* Requires scope: quests:read for getQuestsForPlayer; quests:write for assign, progress, complete, claim.
|
|
729
|
+
*/
|
|
730
|
+
/** Get assigned quests for a player. Optional questType filter. */
|
|
731
|
+
async getQuestsForPlayer(wallet, options) {
|
|
732
|
+
const q = options?.questType ? `?questType=${options.questType}` : "";
|
|
733
|
+
const response = await this.apiCall(`/v1/quests/players/${encodeURIComponent(wallet)}${q}`);
|
|
734
|
+
return response.json();
|
|
735
|
+
}
|
|
736
|
+
/** Assign quests of a type to a player. Use idempotencyKey for safe retries. */
|
|
737
|
+
async assignQuests(wallet, questType, count, options) {
|
|
738
|
+
const response = await this.apiCall(`/v1/quests/players/${encodeURIComponent(wallet)}/assign`, {
|
|
739
|
+
method: "POST",
|
|
740
|
+
body: { questType, count },
|
|
741
|
+
headers: options?.idempotencyKey ? { "Idempotency-Key": options.idempotencyKey } : void 0
|
|
742
|
+
});
|
|
743
|
+
return response.json();
|
|
744
|
+
}
|
|
745
|
+
/** Report progress for a quest step. Quest auto-completes when all steps meet targets. */
|
|
746
|
+
async reportQuestProgress(wallet, questKey, value, options) {
|
|
747
|
+
const response = await this.apiCall("/v1/quests/progress", {
|
|
748
|
+
method: "POST",
|
|
749
|
+
body: { wallet, questKey, value, stepKey: options?.stepKey }
|
|
750
|
+
});
|
|
751
|
+
return response.json();
|
|
752
|
+
}
|
|
753
|
+
/** Mark a quest complete (all steps set to targets). */
|
|
754
|
+
async completeQuest(wallet, questKey) {
|
|
755
|
+
const response = await this.apiCall("/v1/quests/complete", {
|
|
756
|
+
method: "POST",
|
|
757
|
+
body: { wallet, questKey }
|
|
758
|
+
});
|
|
759
|
+
return response.json();
|
|
760
|
+
}
|
|
761
|
+
/** Claim VIP reward for a completed quest (from game pool). Use idempotencyKey for safe retries. */
|
|
762
|
+
async claimQuestReward(wallet, questKey, options) {
|
|
763
|
+
const response = await this.apiCall("/v1/quests/claim", {
|
|
764
|
+
method: "POST",
|
|
765
|
+
body: { wallet, questKey },
|
|
766
|
+
headers: options?.idempotencyKey ? { "Idempotency-Key": options.idempotencyKey } : void 0
|
|
767
|
+
});
|
|
768
|
+
return response.json();
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Reset or wipe a player's quest state for this game. Requires quests:write.
|
|
772
|
+
* - wipe: delete all quest assignments for this game for the player.
|
|
773
|
+
* - reset: delete assignments for the specified questKey only (questKey required).
|
|
774
|
+
*/
|
|
775
|
+
async resetPlayerQuests(wallet, action, questKey) {
|
|
776
|
+
const body = { action };
|
|
777
|
+
if (action === "reset" && questKey) body.questKey = questKey;
|
|
778
|
+
const response = await this.apiCall("/v1/quests/players/" + encodeURIComponent(wallet) + "/reset", {
|
|
779
|
+
method: "POST",
|
|
780
|
+
body
|
|
781
|
+
});
|
|
782
|
+
return response.json();
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
var WALLET_CHANNEL_PREFIX = "wallet:";
|
|
786
|
+
var REALTIME_EVENTS = {
|
|
787
|
+
/** Currency balance changed. Refetch via GetPlayerBalances or your balance API. */
|
|
788
|
+
balance: "balance",
|
|
789
|
+
/** NFT/inventory changed. Refetch via GetPlayerNfts or your inventory API (cache is source of truth). */
|
|
790
|
+
inventory: "inventory"
|
|
718
791
|
};
|
|
792
|
+
var MAX_SEEN_IDS = 200;
|
|
793
|
+
function normalizeWallet(wallet) {
|
|
794
|
+
return (wallet || "").trim().toLowerCase();
|
|
795
|
+
}
|
|
796
|
+
function getWalletChannelName(walletAddress) {
|
|
797
|
+
return WALLET_CHANNEL_PREFIX + normalizeWallet(walletAddress);
|
|
798
|
+
}
|
|
799
|
+
function subscribeWalletRealtime(ably, walletAddress, callbacks) {
|
|
800
|
+
const normalized = normalizeWallet(walletAddress);
|
|
801
|
+
if (!normalized || !/^0x[a-fa-f0-9]{40}$/.test(normalized)) {
|
|
802
|
+
return () => {
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
806
|
+
const handleMessage = (message) => {
|
|
807
|
+
const data = message.data;
|
|
808
|
+
if (!data || typeof data !== "object" || typeof data.eventId !== "string") return;
|
|
809
|
+
if (seenIds.has(data.eventId)) return;
|
|
810
|
+
if (seenIds.size >= MAX_SEEN_IDS) {
|
|
811
|
+
const first = seenIds.values().next().value;
|
|
812
|
+
if (first != null) seenIds.delete(first);
|
|
813
|
+
}
|
|
814
|
+
seenIds.add(data.eventId);
|
|
815
|
+
if (message.name === REALTIME_EVENTS.balance && callbacks.onBalance) {
|
|
816
|
+
callbacks.onBalance(data);
|
|
817
|
+
} else if (message.name === REALTIME_EVENTS.inventory && callbacks.onInventory) {
|
|
818
|
+
callbacks.onInventory(data);
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
const channel = ably.channels.get(getWalletChannelName(normalized));
|
|
822
|
+
channel.subscribe(handleMessage);
|
|
823
|
+
return () => {
|
|
824
|
+
channel.unsubscribe(handleMessage);
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
function subscribeWalletRealtimeWithOmenXAuth(options) {
|
|
828
|
+
const { apiBaseUrl, getAccessToken, walletAddress, onBalance, onInventory } = options;
|
|
829
|
+
const normalized = (walletAddress || "").trim().toLowerCase();
|
|
830
|
+
if (!normalized || !/^0x[a-fa-f0-9]{40}$/.test(normalized)) {
|
|
831
|
+
return () => {
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
const base = apiBaseUrl.replace(/\/$/, "");
|
|
835
|
+
const authCallback = (_tokenParams, callback) => {
|
|
836
|
+
getAccessToken().then((token) => {
|
|
837
|
+
if (!token) {
|
|
838
|
+
callback("Not authenticated", null);
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
return fetch(`${base}/api/sdk/ably-token`, {
|
|
842
|
+
method: "POST",
|
|
843
|
+
headers: {
|
|
844
|
+
Authorization: `Bearer ${token}`,
|
|
845
|
+
"Content-Type": "application/json"
|
|
846
|
+
}
|
|
847
|
+
});
|
|
848
|
+
}).then((res) => {
|
|
849
|
+
if (!res) return;
|
|
850
|
+
if (!res.ok) {
|
|
851
|
+
return res.json().then((body) => {
|
|
852
|
+
callback(body && body.message || res.statusText || "Request failed", null);
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
return res.json().then((tokenRequest) => {
|
|
856
|
+
callback(null, tokenRequest);
|
|
857
|
+
});
|
|
858
|
+
}).catch((err) => {
|
|
859
|
+
callback(err instanceof Error ? err.message : String(err), null);
|
|
860
|
+
});
|
|
861
|
+
};
|
|
862
|
+
const ably = new Ably__default.default.Realtime({
|
|
863
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
864
|
+
authCallback
|
|
865
|
+
});
|
|
866
|
+
const unsubscribe = subscribeWalletRealtime(ably, walletAddress, {
|
|
867
|
+
onBalance,
|
|
868
|
+
onInventory
|
|
869
|
+
});
|
|
870
|
+
return () => {
|
|
871
|
+
unsubscribe();
|
|
872
|
+
ably.close();
|
|
873
|
+
};
|
|
874
|
+
}
|
|
719
875
|
|
|
720
876
|
exports.OmenXGameSDK = OmenXGameSDK;
|
|
721
877
|
exports.OmenXServerSDK = OmenXServerSDK;
|
|
878
|
+
exports.REALTIME_EVENTS = REALTIME_EVENTS;
|
|
879
|
+
exports.WALLET_CHANNEL_PREFIX = WALLET_CHANNEL_PREFIX;
|
|
880
|
+
exports.getWalletChannelName = getWalletChannelName;
|
|
881
|
+
exports.subscribeWalletRealtime = subscribeWalletRealtime;
|
|
882
|
+
exports.subscribeWalletRealtimeWithOmenXAuth = subscribeWalletRealtimeWithOmenXAuth;
|
|
722
883
|
//# sourceMappingURL=index.js.map
|
|
723
884
|
//# sourceMappingURL=index.js.map
|