@opensea/sdk 11.8.0 → 12.0.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.
Files changed (72) hide show
  1. package/README.md +46 -15
  2. package/lib/api/api.d.ts +9 -0
  3. package/lib/api/api.js +15 -2
  4. package/lib/api/api.js.map +1 -1
  5. package/lib/api/collections.d.ts +7 -0
  6. package/lib/api/collections.js +10 -1
  7. package/lib/api/collections.js.map +1 -1
  8. package/lib/api/fetcher.d.ts +5 -2
  9. package/lib/api/types.d.ts +20 -64
  10. package/lib/api/types.js.map +1 -1
  11. package/lib/api/walletAuth.d.ts +95 -8
  12. package/lib/api/walletAuth.js +75 -6
  13. package/lib/api/walletAuth.js.map +1 -1
  14. package/lib/index.d.ts +1 -1
  15. package/lib/orders/attribution.d.ts +18 -0
  16. package/lib/orders/attribution.js +29 -0
  17. package/lib/orders/attribution.js.map +1 -0
  18. package/lib/orders/privateListings.js +8 -0
  19. package/lib/orders/privateListings.js.map +1 -1
  20. package/lib/orders/types.d.ts +16 -28
  21. package/lib/orders/types.js.map +1 -1
  22. package/lib/scopes.d.ts +1 -0
  23. package/lib/scopes.js +1 -0
  24. package/lib/scopes.js.map +1 -1
  25. package/lib/sdk/fulfillment.js +7 -3
  26. package/lib/sdk/fulfillment.js.map +1 -1
  27. package/lib/stream/client.d.ts +80 -0
  28. package/lib/stream/client.js +191 -0
  29. package/lib/stream/client.js.map +1 -0
  30. package/lib/stream/constants.d.ts +3 -0
  31. package/lib/stream/constants.js +8 -0
  32. package/lib/stream/constants.js.map +1 -0
  33. package/lib/stream/helpers.d.ts +1 -0
  34. package/lib/stream/helpers.js +8 -0
  35. package/lib/stream/helpers.js.map +1 -0
  36. package/lib/stream/index.d.ts +17 -0
  37. package/lib/stream/index.js +34 -0
  38. package/lib/stream/index.js.map +1 -0
  39. package/lib/stream/transport/phoenix.d.ts +121 -0
  40. package/lib/stream/transport/phoenix.js +642 -0
  41. package/lib/stream/transport/phoenix.js.map +1 -0
  42. package/lib/stream/transport/types.d.ts +85 -0
  43. package/lib/stream/transport/types.js +24 -0
  44. package/lib/stream/transport/types.js.map +1 -0
  45. package/lib/stream/types.d.ts +238 -0
  46. package/lib/stream/types.js +36 -0
  47. package/lib/stream/types.js.map +1 -0
  48. package/lib/types.d.ts +14 -0
  49. package/lib/utils/units.d.ts +5 -0
  50. package/lib/utils/units.js +48 -9
  51. package/lib/utils/units.js.map +1 -1
  52. package/package.json +10 -9
  53. package/src/api/api.ts +19 -2
  54. package/src/api/collections.ts +14 -1
  55. package/src/api/fetcher.ts +5 -2
  56. package/src/api/types.ts +27 -64
  57. package/src/api/walletAuth.ts +100 -13
  58. package/src/index.ts +1 -1
  59. package/src/orders/attribution.ts +29 -0
  60. package/src/orders/privateListings.ts +15 -0
  61. package/src/orders/types.ts +21 -44
  62. package/src/scopes.ts +1 -0
  63. package/src/sdk/fulfillment.ts +8 -3
  64. package/src/stream/client.ts +299 -0
  65. package/src/stream/constants.ts +5 -0
  66. package/src/stream/helpers.ts +3 -0
  67. package/src/stream/index.ts +18 -0
  68. package/src/stream/transport/phoenix.ts +815 -0
  69. package/src/stream/transport/types.ts +91 -0
  70. package/src/stream/types.ts +289 -0
  71. package/src/types.ts +14 -0
  72. package/src/utils/units.ts +54 -9
@@ -0,0 +1,299 @@
1
+ import { ENDPOINTS } from "./constants"
2
+ import { collectionTopic } from "./helpers"
3
+ import { PhoenixChannelsTransport } from "./transport/phoenix"
4
+ import type { StreamTransport } from "./transport/types"
5
+ import {
6
+ type BaseStreamMessage,
7
+ type Callback,
8
+ type ClientConfig,
9
+ type CollectionOfferEvent,
10
+ EventType,
11
+ type ItemCancelledEvent,
12
+ type ItemListedEvent,
13
+ type ItemMetadataUpdate,
14
+ type ItemReceivedBidEvent,
15
+ type ItemReceivedOfferEvent,
16
+ type ItemSoldEvent,
17
+ type ItemTransferredEvent,
18
+ LogLevel,
19
+ Network,
20
+ type OnClientEvent,
21
+ type OrderValidationEvent,
22
+ type TraitOfferEvent,
23
+ } from "./types"
24
+
25
+ /**
26
+ * Client for the OpenSea Stream API.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { OpenSeaStreamClient } from "@opensea/sdk/stream"
31
+ *
32
+ * const client = new OpenSeaStreamClient({ apiKey: "YOUR_API_KEY" })
33
+ *
34
+ * const unsubscribe = client.onItemListed("doodles-official", event => {
35
+ * console.log(event.payload.item.nft_id)
36
+ * })
37
+ * ```
38
+ *
39
+ * @category Main Classes
40
+ */
41
+ export class OpenSeaStreamClient {
42
+ private transport: StreamTransport
43
+ private logLevel: LogLevel
44
+ private onEvent: OnClientEvent
45
+
46
+ constructor({
47
+ network = Network.MAINNET,
48
+ apiKey,
49
+ token,
50
+ apiUrl,
51
+ connectOptions,
52
+ logLevel = LogLevel.INFO,
53
+ onError,
54
+ onEvent = () => true,
55
+ }: ClientConfig) {
56
+ const key = apiKey ?? token
57
+ if (!key) {
58
+ throw new Error(
59
+ "An OpenSea API key is required. Pass it as `apiKey` when constructing OpenSeaStreamClient.",
60
+ )
61
+ }
62
+
63
+ this.logLevel = logLevel
64
+ this.onEvent = onEvent
65
+
66
+ const endpoint = apiUrl || ENDPOINTS[network]
67
+ const { params, ...transportOptions } = connectOptions ?? {}
68
+
69
+ this.transport = new PhoenixChannelsTransport({
70
+ endpoint,
71
+ params: { token: key, ...params },
72
+ ...transportOptions,
73
+ logger: message => this.debug(message),
74
+ })
75
+
76
+ this.transport.onError(onError ?? (error => this.error(error)))
77
+ }
78
+
79
+ private log(level: LogLevel, message: unknown) {
80
+ if (this.logLevel > level) {
81
+ return
82
+ }
83
+
84
+ switch (level) {
85
+ case LogLevel.DEBUG:
86
+ console.debug("[DEBUG]:", message)
87
+ break
88
+ case LogLevel.INFO:
89
+ console.info("[INFO]:", message)
90
+ break
91
+ case LogLevel.WARN:
92
+ console.warn("[WARN]:", message)
93
+ break
94
+ case LogLevel.ERROR:
95
+ console.error("[ERROR]:", message)
96
+ break
97
+ }
98
+ }
99
+
100
+ private debug(message: unknown) {
101
+ this.log(LogLevel.DEBUG, message)
102
+ }
103
+
104
+ private info(message: unknown) {
105
+ this.log(LogLevel.INFO, message)
106
+ }
107
+
108
+ private warn(message: unknown) {
109
+ this.log(LogLevel.WARN, message)
110
+ }
111
+
112
+ private error(message: unknown) {
113
+ this.log(LogLevel.ERROR, message)
114
+ }
115
+
116
+ public connect = () => {
117
+ this.debug("Connecting to socket")
118
+ this.transport.connect()
119
+ }
120
+
121
+ public disconnect = (
122
+ callback = () => this.info(`Successfully disconnected from socket`),
123
+ ) => {
124
+ return this.transport.disconnect(callback)
125
+ }
126
+
127
+ private on = <Payload, Event extends BaseStreamMessage<Payload>>(
128
+ eventType: EventType,
129
+ collectionSlug: string,
130
+ callback: Callback<Event>,
131
+ eventTypes?: EventType[],
132
+ ) => {
133
+ const topic = collectionTopic(collectionSlug)
134
+ this.debug(`Subscribing to ${eventType} events on ${topic}`)
135
+
136
+ const onClientEvent = this.onEvent
137
+ const handler = (message: unknown) => {
138
+ const event = message as Event
139
+ if (onClientEvent(collectionSlug, eventType, event)) {
140
+ callback(event)
141
+ }
142
+ }
143
+
144
+ const subscription = this.transport.subscribe(
145
+ topic,
146
+ { eventTypes },
147
+ {
148
+ onSubscribed: () => this.info(`Successfully joined channel "${topic}"`),
149
+ onSubscribeError: reason => {
150
+ this.error(`Failed to join channel "${topic}"`)
151
+ this.debug(reason)
152
+ },
153
+ },
154
+ )
155
+ const off = subscription.on(eventType, handler)
156
+
157
+ return () => {
158
+ this.debug(`Unsubscribing from ${eventType} events on ${topic}`)
159
+ // Removes only this handler. The topic is torn down once nothing is
160
+ // listening to it. The original stream-js client left the whole channel
161
+ // here, which silently killed sibling subscriptions on the same
162
+ // collection.
163
+ off()
164
+ }
165
+ }
166
+
167
+ public onItemMetadataUpdated = (
168
+ collectionSlug: string,
169
+ callback: Callback<ItemMetadataUpdate>,
170
+ ) => {
171
+ return this.on(EventType.ITEM_METADATA_UPDATED, collectionSlug, callback)
172
+ }
173
+
174
+ public onItemCancelled = (
175
+ collectionSlug: string,
176
+ callback: Callback<ItemCancelledEvent>,
177
+ ) => {
178
+ return this.on(EventType.ITEM_CANCELLED, collectionSlug, callback)
179
+ }
180
+
181
+ public onItemListed = (
182
+ collectionSlug: string,
183
+ callback: Callback<ItemListedEvent>,
184
+ ) => {
185
+ return this.on(EventType.ITEM_LISTED, collectionSlug, callback)
186
+ }
187
+
188
+ public onItemSold = (
189
+ collectionSlug: string,
190
+ callback: Callback<ItemSoldEvent>,
191
+ ) => {
192
+ return this.on(EventType.ITEM_SOLD, collectionSlug, callback)
193
+ }
194
+
195
+ public onItemTransferred = (
196
+ collectionSlug: string,
197
+ callback: Callback<ItemTransferredEvent>,
198
+ ) => {
199
+ return this.on(EventType.ITEM_TRANSFERRED, collectionSlug, callback)
200
+ }
201
+
202
+ /**
203
+ * No-op. The Stream API does not emit `item_received_offer` and never has, so
204
+ * this only ever registered a handler that could not fire. Item-level offers
205
+ * arrive as `item_received_bid`, so use {@link onItemReceivedBid} instead.
206
+ *
207
+ * Kept callable, and kept from opening a connection for a topic that yields
208
+ * nothing, so existing call sites keep compiling and running unchanged.
209
+ *
210
+ * @deprecated Use {@link onItemReceivedBid}. This does nothing.
211
+ * @hidden
212
+ */
213
+ public onItemReceivedOffer = (
214
+ _collectionSlug: string,
215
+ _callback: Callback<ItemReceivedOfferEvent>,
216
+ ) => {
217
+ this.warn(
218
+ "onItemReceivedOffer does nothing: the Stream API does not emit item_received_offer. Item-level offers arrive as item_received_bid, via onItemReceivedBid.",
219
+ )
220
+ return () => {}
221
+ }
222
+
223
+ public onItemReceivedBid = (
224
+ collectionSlug: string,
225
+ callback: Callback<ItemReceivedBidEvent>,
226
+ ) => {
227
+ return this.on(EventType.ITEM_RECEIVED_BID, collectionSlug, callback)
228
+ }
229
+
230
+ public onCollectionOffer = (
231
+ collectionSlug: string,
232
+ callback: Callback<CollectionOfferEvent>,
233
+ ) => {
234
+ return this.on(EventType.COLLECTION_OFFER, collectionSlug, callback)
235
+ }
236
+
237
+ public onTraitOffer = (
238
+ collectionSlug: string,
239
+ callback: Callback<TraitOfferEvent>,
240
+ ) => {
241
+ return this.on(EventType.TRAIT_OFFER, collectionSlug, callback)
242
+ }
243
+
244
+ public onOrderInvalidate = (
245
+ collectionSlug: string,
246
+ callback: Callback<OrderValidationEvent>,
247
+ ) => {
248
+ return this.on(EventType.ORDER_INVALIDATE, collectionSlug, callback)
249
+ }
250
+
251
+ public onOrderRevalidate = (
252
+ collectionSlug: string,
253
+ callback: Callback<OrderValidationEvent>,
254
+ ) => {
255
+ return this.on(EventType.ORDER_REVALIDATE, collectionSlug, callback)
256
+ }
257
+
258
+ /**
259
+ * Subscribe to several event types on one collection with a single callback,
260
+ * filtered server-side to the requested types.
261
+ *
262
+ * The callback receives `BaseStreamMessage<unknown>`: one subscription can
263
+ * deliver several event types, so there is no single payload type to hand
264
+ * back. `event_type` identifies which arrived, and the payload needs
265
+ * narrowing before use.
266
+ *
267
+ * ```ts
268
+ * client.onEvents(slug, [EventType.ITEM_SOLD, EventType.ITEM_LISTED], event => {
269
+ * if (event.event_type === EventType.ITEM_SOLD) {
270
+ * const sale = event as ItemSoldEvent
271
+ * console.log(sale.payload.sale_price)
272
+ * }
273
+ * })
274
+ * ```
275
+ *
276
+ * Use the typed single-event methods, such as `onItemSold`, when you want the
277
+ * payload typed for you.
278
+ *
279
+ * @param collectionSlug collection slug, or `"*"` for all collections
280
+ * @param eventTypes event types to subscribe to
281
+ * @param callback invoked for every subscribed event type
282
+ * @returns a function that removes all of this call's handlers
283
+ */
284
+ public onEvents = (
285
+ collectionSlug: string,
286
+ eventTypes: EventType[],
287
+ callback: Callback<BaseStreamMessage<unknown>>,
288
+ ) => {
289
+ const subscriptions = eventTypes.map(eventType =>
290
+ this.on(eventType, collectionSlug, callback, eventTypes),
291
+ )
292
+
293
+ return () => {
294
+ for (const unsubscribe of subscriptions) {
295
+ unsubscribe()
296
+ }
297
+ }
298
+ }
299
+ }
@@ -0,0 +1,5 @@
1
+ import { Network } from "./types"
2
+
3
+ export const ENDPOINTS = {
4
+ [Network.MAINNET]: "wss://stream-api.opensea.io/socket",
5
+ }
@@ -0,0 +1,3 @@
1
+ export const collectionTopic = (slug: string) => {
2
+ return `collection:${slug}`
3
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * OpenSea Stream API client.
3
+ *
4
+ * Subscribe to real-time marketplace events over WebSocket.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { OpenSeaStreamClient } from "@opensea/sdk/stream"
9
+ *
10
+ * const client = new OpenSeaStreamClient({ apiKey: "YOUR_API_KEY" })
11
+ * client.onItemSold("doodles-official", event => console.log(event))
12
+ * ```
13
+ *
14
+ * @module
15
+ */
16
+
17
+ export * from "./client"
18
+ export * from "./types"