@opensea/sdk 11.8.0 → 11.9.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 +45 -13
- package/lib/api/api.d.ts +9 -0
- package/lib/api/api.js +15 -2
- package/lib/api/api.js.map +1 -1
- package/lib/api/collections.d.ts +7 -0
- package/lib/api/collections.js +10 -1
- package/lib/api/collections.js.map +1 -1
- package/lib/api/fetcher.d.ts +5 -2
- package/lib/index.d.ts +1 -1
- package/lib/orders/privateListings.js +8 -0
- package/lib/orders/privateListings.js.map +1 -1
- package/lib/stream/client.d.ts +67 -0
- package/lib/stream/client.js +174 -0
- package/lib/stream/client.js.map +1 -0
- package/lib/stream/constants.d.ts +3 -0
- package/lib/stream/constants.js +8 -0
- package/lib/stream/constants.js.map +1 -0
- package/lib/stream/helpers.d.ts +1 -0
- package/lib/stream/helpers.js +8 -0
- package/lib/stream/helpers.js.map +1 -0
- package/lib/stream/index.d.ts +17 -0
- package/lib/stream/index.js +34 -0
- package/lib/stream/index.js.map +1 -0
- package/lib/stream/transport/phoenix.d.ts +121 -0
- package/lib/stream/transport/phoenix.js +642 -0
- package/lib/stream/transport/phoenix.js.map +1 -0
- package/lib/stream/transport/types.d.ts +85 -0
- package/lib/stream/transport/types.js +24 -0
- package/lib/stream/transport/types.js.map +1 -0
- package/lib/stream/types.d.ts +269 -0
- package/lib/stream/types.js +29 -0
- package/lib/stream/types.js.map +1 -0
- package/lib/types.d.ts +14 -0
- package/lib/utils/units.d.ts +5 -0
- package/lib/utils/units.js +48 -9
- package/lib/utils/units.js.map +1 -1
- package/package.json +9 -8
- package/src/api/api.ts +19 -2
- package/src/api/collections.ts +14 -1
- package/src/api/fetcher.ts +5 -2
- package/src/index.ts +1 -1
- package/src/orders/privateListings.ts +15 -0
- package/src/stream/client.ts +277 -0
- package/src/stream/constants.ts +5 -0
- package/src/stream/helpers.ts +3 -0
- package/src/stream/index.ts +18 -0
- package/src/stream/transport/phoenix.ts +815 -0
- package/src/stream/transport/types.ts +91 -0
- package/src/stream/types.ts +314 -0
- package/src/types.ts +14 -0
- package/src/utils/units.ts +54 -9
package/src/api/fetcher.ts
CHANGED
|
@@ -33,14 +33,17 @@ export type HttpMethod = "POST" | "PUT" | "PATCH" | "DELETE"
|
|
|
33
33
|
*
|
|
34
34
|
* Responses are camelized at this boundary — pass the API's snake_case shape
|
|
35
35
|
* (e.g. an api-types response type) as `T` and the return is the camelCase
|
|
36
|
-
* view. See `utils/case.ts`.
|
|
36
|
+
* view. See `utils/case.ts`. Endpoints whose response keys are data rather
|
|
37
|
+
* than field names opt out with {@link RequestOptions.camelizeResponse}.
|
|
37
38
|
*/
|
|
38
39
|
export interface Fetcher {
|
|
39
40
|
/**
|
|
40
41
|
* Generic fetch method for GET requests with automatic rate limit retry
|
|
41
42
|
* @param apiPath Path to URL endpoint under API
|
|
42
43
|
* @param query URL query params. Will be used to create a URLSearchParams object.
|
|
43
|
-
* @param options Request options like timeout and abort signal.
|
|
44
|
+
* @param options Request options like timeout and abort signal. Includes the
|
|
45
|
+
* {@link RequestOptions.camelizeResponse} opt-out for endpoints
|
|
46
|
+
* whose response keys are data (e.g. trait names).
|
|
44
47
|
* @returns The camelCase view of the API response.
|
|
45
48
|
*/
|
|
46
49
|
get<T>(
|
package/src/index.ts
CHANGED
|
@@ -25,7 +25,7 @@ export * from "./utils"
|
|
|
25
25
|
* // Example Setup
|
|
26
26
|
* ```ts
|
|
27
27
|
* import { ethers } from 'ethers'
|
|
28
|
-
* import { OpenSeaSDK, Chain } from 'opensea
|
|
28
|
+
* import { OpenSeaSDK, Chain } from '@opensea/sdk'
|
|
29
29
|
* const provider = new ethers.JsonRpcProvider('https://mainnet.infura.io')
|
|
30
30
|
* const client = new OpenSeaSDK(provider, {
|
|
31
31
|
* chain: Chain.Mainnet
|
|
@@ -62,6 +62,21 @@ export const constructPrivateListingCounterOrder = (
|
|
|
62
62
|
) {
|
|
63
63
|
throw new Error("Not all currency items were the same for private order")
|
|
64
64
|
}
|
|
65
|
+
// The counter order below sums every payment item into a single offer item
|
|
66
|
+
// denominated in the first item's token, so two different ERC-20s would be
|
|
67
|
+
// aggregated as if they were one. Seaport then rejects the match because
|
|
68
|
+
// the offer and consideration components disagree on token, surfacing as an
|
|
69
|
+
// onchain revert rather than as this error.
|
|
70
|
+
if (
|
|
71
|
+
!paymentItems.every(
|
|
72
|
+
item =>
|
|
73
|
+
item.token.toLowerCase() === paymentItems[0].token.toLowerCase(),
|
|
74
|
+
)
|
|
75
|
+
) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Not all currency items were the same for private order: payment items must all use the same token",
|
|
78
|
+
)
|
|
79
|
+
}
|
|
65
80
|
}
|
|
66
81
|
|
|
67
82
|
const { aggregatedStartAmount, aggregatedEndAmount } = paymentItems.reduce(
|
|
@@ -0,0 +1,277 @@
|
|
|
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 debug(message: unknown) {
|
|
80
|
+
if (this.logLevel <= LogLevel.DEBUG) {
|
|
81
|
+
console.debug("[DEBUG]:", message)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private info(message: unknown) {
|
|
86
|
+
if (this.logLevel <= LogLevel.INFO) {
|
|
87
|
+
console.info("[INFO]:", message)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private error(message: unknown) {
|
|
92
|
+
if (this.logLevel <= LogLevel.ERROR) {
|
|
93
|
+
console.error("[ERROR]:", message)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public connect = () => {
|
|
98
|
+
this.debug("Connecting to socket")
|
|
99
|
+
this.transport.connect()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public disconnect = (
|
|
103
|
+
callback = () => this.info(`Successfully disconnected from socket`),
|
|
104
|
+
) => {
|
|
105
|
+
return this.transport.disconnect(callback)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private on = <Payload, Event extends BaseStreamMessage<Payload>>(
|
|
109
|
+
eventType: EventType,
|
|
110
|
+
collectionSlug: string,
|
|
111
|
+
callback: Callback<Event>,
|
|
112
|
+
eventTypes?: EventType[],
|
|
113
|
+
) => {
|
|
114
|
+
const topic = collectionTopic(collectionSlug)
|
|
115
|
+
this.debug(`Subscribing to ${eventType} events on ${topic}`)
|
|
116
|
+
|
|
117
|
+
const onClientEvent = this.onEvent
|
|
118
|
+
const handler = (message: unknown) => {
|
|
119
|
+
const event = message as Event
|
|
120
|
+
if (onClientEvent(collectionSlug, eventType, event)) {
|
|
121
|
+
callback(event)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const subscription = this.transport.subscribe(
|
|
126
|
+
topic,
|
|
127
|
+
{ eventTypes },
|
|
128
|
+
{
|
|
129
|
+
onSubscribed: () => this.info(`Successfully joined channel "${topic}"`),
|
|
130
|
+
onSubscribeError: reason => {
|
|
131
|
+
this.error(`Failed to join channel "${topic}"`)
|
|
132
|
+
this.debug(reason)
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
)
|
|
136
|
+
const off = subscription.on(eventType, handler)
|
|
137
|
+
|
|
138
|
+
return () => {
|
|
139
|
+
this.debug(`Unsubscribing from ${eventType} events on ${topic}`)
|
|
140
|
+
// Removes only this handler. The topic is torn down once nothing is
|
|
141
|
+
// listening to it. The original stream-js client left the whole channel
|
|
142
|
+
// here, which silently killed sibling subscriptions on the same
|
|
143
|
+
// collection.
|
|
144
|
+
off()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public onItemMetadataUpdated = (
|
|
149
|
+
collectionSlug: string,
|
|
150
|
+
callback: Callback<ItemMetadataUpdate>,
|
|
151
|
+
) => {
|
|
152
|
+
this.debug(`Listening for item metadata updates on "${collectionSlug}"`)
|
|
153
|
+
return this.on(EventType.ITEM_METADATA_UPDATED, collectionSlug, callback)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public onItemCancelled = (
|
|
157
|
+
collectionSlug: string,
|
|
158
|
+
callback: Callback<ItemCancelledEvent>,
|
|
159
|
+
) => {
|
|
160
|
+
this.debug(`Listening for item cancellations on "${collectionSlug}"`)
|
|
161
|
+
return this.on(EventType.ITEM_CANCELLED, collectionSlug, callback)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public onItemListed = (
|
|
165
|
+
collectionSlug: string,
|
|
166
|
+
callback: Callback<ItemListedEvent>,
|
|
167
|
+
) => {
|
|
168
|
+
this.debug(`Listening for item listings on "${collectionSlug}"`)
|
|
169
|
+
return this.on(EventType.ITEM_LISTED, collectionSlug, callback)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public onItemSold = (
|
|
173
|
+
collectionSlug: string,
|
|
174
|
+
callback: Callback<ItemSoldEvent>,
|
|
175
|
+
) => {
|
|
176
|
+
this.debug(`Listening for item sales on "${collectionSlug}"`)
|
|
177
|
+
return this.on(EventType.ITEM_SOLD, collectionSlug, callback)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public onItemTransferred = (
|
|
181
|
+
collectionSlug: string,
|
|
182
|
+
callback: Callback<ItemTransferredEvent>,
|
|
183
|
+
) => {
|
|
184
|
+
this.debug(`Listening for item transfers on "${collectionSlug}"`)
|
|
185
|
+
return this.on(EventType.ITEM_TRANSFERRED, collectionSlug, callback)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public onItemReceivedOffer = (
|
|
189
|
+
collectionSlug: string,
|
|
190
|
+
callback: Callback<ItemReceivedOfferEvent>,
|
|
191
|
+
) => {
|
|
192
|
+
this.debug(`Listening for item offers on "${collectionSlug}"`)
|
|
193
|
+
return this.on(EventType.ITEM_RECEIVED_OFFER, collectionSlug, callback)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public onItemReceivedBid = (
|
|
197
|
+
collectionSlug: string,
|
|
198
|
+
callback: Callback<ItemReceivedBidEvent>,
|
|
199
|
+
) => {
|
|
200
|
+
this.debug(`Listening for item bids on "${collectionSlug}"`)
|
|
201
|
+
return this.on(EventType.ITEM_RECEIVED_BID, collectionSlug, callback)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public onCollectionOffer = (
|
|
205
|
+
collectionSlug: string,
|
|
206
|
+
callback: Callback<CollectionOfferEvent>,
|
|
207
|
+
) => {
|
|
208
|
+
this.debug(`Listening for collection offers on "${collectionSlug}"`)
|
|
209
|
+
return this.on(EventType.COLLECTION_OFFER, collectionSlug, callback)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public onTraitOffer = (
|
|
213
|
+
collectionSlug: string,
|
|
214
|
+
callback: Callback<TraitOfferEvent>,
|
|
215
|
+
) => {
|
|
216
|
+
this.debug(`Listening for trait offers on "${collectionSlug}"`)
|
|
217
|
+
return this.on(EventType.TRAIT_OFFER, collectionSlug, callback)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public onOrderInvalidate = (
|
|
221
|
+
collectionSlug: string,
|
|
222
|
+
callback: Callback<OrderValidationEvent>,
|
|
223
|
+
) => {
|
|
224
|
+
this.debug(`Listening for order invalidation events on "${collectionSlug}"`)
|
|
225
|
+
return this.on(EventType.ORDER_INVALIDATE, collectionSlug, callback)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public onOrderRevalidate = (
|
|
229
|
+
collectionSlug: string,
|
|
230
|
+
callback: Callback<OrderValidationEvent>,
|
|
231
|
+
) => {
|
|
232
|
+
this.debug(`Listening for order revalidation events on "${collectionSlug}"`)
|
|
233
|
+
return this.on(EventType.ORDER_REVALIDATE, collectionSlug, callback)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Subscribe to several event types on one collection with a single callback,
|
|
238
|
+
* filtered server-side to the requested types.
|
|
239
|
+
*
|
|
240
|
+
* The callback receives `BaseStreamMessage<unknown>`: one subscription can
|
|
241
|
+
* deliver several event types, so there is no single payload type to hand
|
|
242
|
+
* back. `event_type` identifies which arrived, and the payload needs
|
|
243
|
+
* narrowing before use.
|
|
244
|
+
*
|
|
245
|
+
* ```ts
|
|
246
|
+
* client.onEvents(slug, [EventType.ITEM_SOLD, EventType.ITEM_LISTED], event => {
|
|
247
|
+
* if (event.event_type === EventType.ITEM_SOLD) {
|
|
248
|
+
* const sale = event as ItemSoldEvent
|
|
249
|
+
* console.log(sale.payload.sale_price)
|
|
250
|
+
* }
|
|
251
|
+
* })
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* Use the typed single-event methods, such as `onItemSold`, when you want the
|
|
255
|
+
* payload typed for you.
|
|
256
|
+
*
|
|
257
|
+
* @param collectionSlug collection slug, or `"*"` for all collections
|
|
258
|
+
* @param eventTypes event types to subscribe to
|
|
259
|
+
* @param callback invoked for every subscribed event type
|
|
260
|
+
* @returns a function that removes all of this call's handlers
|
|
261
|
+
*/
|
|
262
|
+
public onEvents = (
|
|
263
|
+
collectionSlug: string,
|
|
264
|
+
eventTypes: EventType[],
|
|
265
|
+
callback: Callback<BaseStreamMessage<unknown>>,
|
|
266
|
+
) => {
|
|
267
|
+
const subscriptions = eventTypes.map(eventType =>
|
|
268
|
+
this.on(eventType, collectionSlug, callback, eventTypes),
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
return () => {
|
|
272
|
+
for (const unsubscribe of subscriptions) {
|
|
273
|
+
unsubscribe()
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -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"
|