@opensea/sdk 11.7.2 → 11.8.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 +1 -1
- package/lib/abi/abis.d.ts +16 -0
- package/lib/abi/abis.js +13 -1
- package/lib/abi/abis.js.map +1 -1
- package/lib/api/api.d.ts +2 -3
- package/lib/api/api.js +2 -3
- package/lib/api/api.js.map +1 -1
- package/lib/api/walletAuth.d.ts +7 -0
- package/lib/api/walletAuth.js +8 -1
- package/lib/api/walletAuth.js.map +1 -1
- package/lib/constants.d.ts +6 -0
- package/lib/constants.js +7 -1
- package/lib/constants.js.map +1 -1
- package/lib/orders/erc20Fulfillment.d.ts +41 -0
- package/lib/orders/erc20Fulfillment.js +190 -0
- package/lib/orders/erc20Fulfillment.js.map +1 -0
- package/lib/orders/orderUseCase.d.ts +18 -0
- package/lib/orders/orderUseCase.js +29 -0
- package/lib/orders/orderUseCase.js.map +1 -0
- package/lib/sdk/base.d.ts +19 -7
- package/lib/sdk/base.js +19 -7
- package/lib/sdk/base.js.map +1 -1
- package/lib/sdk/cancellation.d.ts +2 -1
- package/lib/sdk/cancellation.js +2 -0
- package/lib/sdk/cancellation.js.map +1 -1
- package/lib/sdk/fulfillment.d.ts +57 -3
- package/lib/sdk/fulfillment.js +147 -4
- package/lib/sdk/fulfillment.js.map +1 -1
- package/lib/sdk/orders.d.ts +16 -3
- package/lib/sdk/orders.js +27 -13
- package/lib/sdk/orders.js.map +1 -1
- package/package.json +3 -3
- package/src/abi/abis.ts +13 -0
- package/src/api/api.ts +2 -3
- package/src/api/walletAuth.ts +9 -0
- package/src/constants.ts +8 -0
- package/src/orders/erc20Fulfillment.ts +218 -0
- package/src/orders/orderUseCase.ts +34 -0
- package/src/sdk/base.ts +20 -6
- package/src/sdk/cancellation.ts +4 -1
- package/src/sdk/fulfillment.ts +188 -6
- package/src/sdk/orders.ts +27 -15
package/src/sdk/fulfillment.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { SeaportABI } from "@opensea/seaport-js/lib/abi/Seaport"
|
|
2
2
|
import type { OrderComponents } from "@opensea/seaport-js/lib/types"
|
|
3
|
+
import { CONDUIT_CONTROLLER_ABI, ERC20_ABI } from "../abi/abis"
|
|
3
4
|
import type { Listing, Offer, Order } from "../api/types"
|
|
5
|
+
import { CONDUIT_CONTROLLER_ADDRESS } from "../constants"
|
|
6
|
+
import {
|
|
7
|
+
getErc20Payment,
|
|
8
|
+
getFulfillerConduitKey,
|
|
9
|
+
isZeroConduitKey,
|
|
10
|
+
toBigInt,
|
|
11
|
+
} from "../orders/erc20Fulfillment"
|
|
4
12
|
import {
|
|
5
13
|
computePrivateListingValue,
|
|
6
14
|
constructPrivateListingCounterOrder,
|
|
@@ -15,6 +23,7 @@ import {
|
|
|
15
23
|
OrderSide,
|
|
16
24
|
} from "../types"
|
|
17
25
|
import {
|
|
26
|
+
getDefaultConduit,
|
|
18
27
|
getSeaportInstance,
|
|
19
28
|
hasErrorCode,
|
|
20
29
|
requireValidProtocol,
|
|
@@ -221,6 +230,13 @@ export class FulfillmentManager {
|
|
|
221
230
|
params = Object.values(inputData)
|
|
222
231
|
}
|
|
223
232
|
|
|
233
|
+
// ERC20-priced listings send no native value; Seaport pulls the payment from
|
|
234
|
+
// the buyer instead, which reverts with a bare "execution reverted" when the
|
|
235
|
+
// spender has not been approved. Catch that before spending gas.
|
|
236
|
+
await this.requireErc20PaymentIsSpendable(inputData, transaction.to, {
|
|
237
|
+
accountAddress,
|
|
238
|
+
})
|
|
239
|
+
|
|
224
240
|
const encodedData = this.context.contractCaller.encodeFunctionData({
|
|
225
241
|
abi: SeaportABI,
|
|
226
242
|
functionName,
|
|
@@ -247,6 +263,137 @@ export class FulfillmentManager {
|
|
|
247
263
|
return tx.hash
|
|
248
264
|
}
|
|
249
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Throw an actionable error when the buyer cannot pay for an ERC20-priced
|
|
268
|
+
* listing, because they hold too little of the payment token or have not
|
|
269
|
+
* approved the address Seaport pulls it from.
|
|
270
|
+
*
|
|
271
|
+
* The check is skipped for native-priced listings, for offer fulfillments, and
|
|
272
|
+
* for any response shape or RPC read it cannot interpret, so it never blocks a
|
|
273
|
+
* purchase that would otherwise have succeeded.
|
|
274
|
+
*/
|
|
275
|
+
private async requireErc20PaymentIsSpendable(
|
|
276
|
+
inputData: unknown,
|
|
277
|
+
seaportAddress: string,
|
|
278
|
+
{ accountAddress }: { accountAddress: string },
|
|
279
|
+
): Promise<void> {
|
|
280
|
+
const payment = getErc20Payment(inputData)
|
|
281
|
+
if (!payment) {
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const spender = await this.resolveFulfillerSpender(
|
|
286
|
+
getFulfillerConduitKey(inputData),
|
|
287
|
+
seaportAddress,
|
|
288
|
+
)
|
|
289
|
+
if (!spender) {
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const spendState = await this.readErc20SpendState(
|
|
294
|
+
payment.token,
|
|
295
|
+
accountAddress,
|
|
296
|
+
spender,
|
|
297
|
+
)
|
|
298
|
+
if (!spendState) {
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const amounts = `have ${spendState.balance}, need ${payment.amount} (base units of the payment token)`
|
|
303
|
+
if (spendState.balance < payment.amount) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`Insufficient ${payment.token} balance to fulfill this listing: ${amounts}. ` +
|
|
306
|
+
`Fund ${accountAddress} with the payment token before fulfilling.`,
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
if (spendState.allowance < payment.amount) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
`Payment token ${payment.token} is not approved for this listing: allowance for ` +
|
|
312
|
+
`${spender} is ${spendState.allowance}, need ${payment.amount} (base units of the ` +
|
|
313
|
+
`payment token). ERC20-priced listings send no native value, so Seaport pulls the ` +
|
|
314
|
+
`payment through this spender. Send approve(${spender}, ${payment.amount}) from ` +
|
|
315
|
+
`${accountAddress} first.`,
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Resolve the address that will pull the payment token: Seaport itself when the
|
|
322
|
+
* fulfiller conduit key is `bytes32(0)`, otherwise the conduit registered for
|
|
323
|
+
* that key. Returns null when the key cannot be resolved.
|
|
324
|
+
*/
|
|
325
|
+
private async resolveFulfillerSpender(
|
|
326
|
+
conduitKey: string | null,
|
|
327
|
+
seaportAddress: string,
|
|
328
|
+
): Promise<string | null> {
|
|
329
|
+
if (!conduitKey || isZeroConduitKey(conduitKey)) {
|
|
330
|
+
return seaportAddress
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const defaultConduit = getDefaultConduit(this.context.chain)
|
|
334
|
+
if (conduitKey.toLowerCase() === defaultConduit.key.toLowerCase()) {
|
|
335
|
+
return defaultConduit.address
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
const result = await this.context.contractCaller.readContract({
|
|
340
|
+
address: CONDUIT_CONTROLLER_ADDRESS,
|
|
341
|
+
abi: CONDUIT_CONTROLLER_ABI,
|
|
342
|
+
functionName: "getConduit",
|
|
343
|
+
args: [conduitKey],
|
|
344
|
+
})
|
|
345
|
+
if (
|
|
346
|
+
!Array.isArray(result) ||
|
|
347
|
+
typeof result[0] !== "string" ||
|
|
348
|
+
result[1] !== true
|
|
349
|
+
) {
|
|
350
|
+
return null
|
|
351
|
+
}
|
|
352
|
+
return result[0]
|
|
353
|
+
} catch (error) {
|
|
354
|
+
this.context.logger(
|
|
355
|
+
`Could not resolve conduit for key ${conduitKey}, skipping the ERC20 approval check: ${error}`,
|
|
356
|
+
)
|
|
357
|
+
return null
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Read the buyer's payment-token balance and allowance for a spender. */
|
|
362
|
+
private async readErc20SpendState(
|
|
363
|
+
token: string,
|
|
364
|
+
owner: string,
|
|
365
|
+
spender: string,
|
|
366
|
+
): Promise<{ balance: bigint; allowance: bigint } | null> {
|
|
367
|
+
const contractCaller = this.context.contractCaller
|
|
368
|
+
try {
|
|
369
|
+
const [rawBalance, rawAllowance] = await Promise.all([
|
|
370
|
+
contractCaller.readContract({
|
|
371
|
+
address: token,
|
|
372
|
+
abi: ERC20_ABI,
|
|
373
|
+
functionName: "balanceOf",
|
|
374
|
+
args: [owner],
|
|
375
|
+
}),
|
|
376
|
+
contractCaller.readContract({
|
|
377
|
+
address: token,
|
|
378
|
+
abi: ERC20_ABI,
|
|
379
|
+
functionName: "allowance",
|
|
380
|
+
args: [owner, spender],
|
|
381
|
+
}),
|
|
382
|
+
])
|
|
383
|
+
const balance = toBigInt(rawBalance)
|
|
384
|
+
const allowance = toBigInt(rawAllowance)
|
|
385
|
+
if (balance === null || allowance === null) {
|
|
386
|
+
return null
|
|
387
|
+
}
|
|
388
|
+
return { balance, allowance }
|
|
389
|
+
} catch (error) {
|
|
390
|
+
this.context.logger(
|
|
391
|
+
`Could not read ${token} balance or allowance, skipping the ERC20 approval check: ${error}`,
|
|
392
|
+
)
|
|
393
|
+
return null
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
250
397
|
/**
|
|
251
398
|
* Returns whether an order is fulfillable.
|
|
252
399
|
* An order may not be fulfillable if a target item's transfer function
|
|
@@ -324,22 +471,35 @@ export class FulfillmentManager {
|
|
|
324
471
|
|
|
325
472
|
/**
|
|
326
473
|
* Validates an order onchain using Seaport's validate() method.
|
|
474
|
+
*
|
|
475
|
+
* The order is left with an empty signature: Seaport takes the transaction
|
|
476
|
+
* sender as proof the offerer approved it. This does not post the order to
|
|
477
|
+
* OpenSea. It becomes discoverable only once OpenSea ingests the resulting
|
|
478
|
+
* `OrderValidated` event.
|
|
479
|
+
*
|
|
480
|
+
* @param orderComponents The order to validate
|
|
481
|
+
* @param accountAddress Address sending the validate transaction
|
|
482
|
+
* @param protocolAddress Seaport protocol address the components were built
|
|
483
|
+
* for. Defaults to {@link DEFAULT_SEAPORT_CONTRACT_ADDRESS}.
|
|
484
|
+
* @returns Transaction hash of the validate transaction
|
|
485
|
+
*
|
|
486
|
+
* @throws Error if the accountAddress is not available through wallet or provider.
|
|
487
|
+
* @throws Error if the protocol address is not supported by OpenSea. See {@link isValidProtocol}.
|
|
327
488
|
*/
|
|
328
489
|
async validateOrderOnchain(
|
|
329
490
|
orderComponents: OrderComponents,
|
|
330
491
|
accountAddress: string,
|
|
492
|
+
protocolAddress: string = DEFAULT_SEAPORT_CONTRACT_ADDRESS,
|
|
331
493
|
) {
|
|
332
494
|
await this.context.requireAccountIsAvailable(accountAddress)
|
|
495
|
+
requireValidProtocol(protocolAddress)
|
|
333
496
|
|
|
334
497
|
this.context.dispatch(EventType.ApproveOrder, {
|
|
335
498
|
orderV2: { protocolData: orderComponents } as unknown as OrderV2,
|
|
336
499
|
accountAddress,
|
|
337
500
|
})
|
|
338
501
|
|
|
339
|
-
const seaport = getSeaportInstance(
|
|
340
|
-
DEFAULT_SEAPORT_CONTRACT_ADDRESS,
|
|
341
|
-
this.context.seaport,
|
|
342
|
-
)
|
|
502
|
+
const seaport = getSeaportInstance(protocolAddress, this.context.seaport)
|
|
343
503
|
const transaction = await seaport
|
|
344
504
|
.validate(
|
|
345
505
|
[{ parameters: orderComponents, signature: "0x" }],
|
|
@@ -357,7 +517,18 @@ export class FulfillmentManager {
|
|
|
357
517
|
}
|
|
358
518
|
|
|
359
519
|
/**
|
|
360
|
-
* Create and validate
|
|
520
|
+
* Create a listing and validate it onchain, instead of signing it offchain.
|
|
521
|
+
*
|
|
522
|
+
* Runs any token approvals the listing needs, then sends one `validate()`
|
|
523
|
+
* transaction. The wallet is never asked for an EIP-712 signature, so this
|
|
524
|
+
* works for contract accounts that cannot produce one.
|
|
525
|
+
*
|
|
526
|
+
* Unlike {@link OrdersManager.createListing}, this does not post the listing
|
|
527
|
+
* to OpenSea. It becomes discoverable only once OpenSea ingests the
|
|
528
|
+
* `OrderValidated` event, so the returned hash is a transaction hash, not an
|
|
529
|
+
* order hash.
|
|
530
|
+
*
|
|
531
|
+
* @returns Transaction hash of the validate transaction
|
|
361
532
|
*/
|
|
362
533
|
async createListingAndValidateOnchain({
|
|
363
534
|
asset,
|
|
@@ -403,7 +574,18 @@ export class FulfillmentManager {
|
|
|
403
574
|
}
|
|
404
575
|
|
|
405
576
|
/**
|
|
406
|
-
* Create and validate
|
|
577
|
+
* Create an offer and validate it onchain, instead of signing it offchain.
|
|
578
|
+
*
|
|
579
|
+
* Runs any token approvals the offer needs, then sends one `validate()`
|
|
580
|
+
* transaction. The wallet is never asked for an EIP-712 signature, so this
|
|
581
|
+
* works for contract accounts that cannot produce one.
|
|
582
|
+
*
|
|
583
|
+
* Unlike {@link OrdersManager.createOffer}, this does not post the offer to
|
|
584
|
+
* OpenSea. It becomes discoverable only once OpenSea ingests the
|
|
585
|
+
* `OrderValidated` event, so the returned hash is a transaction hash, not an
|
|
586
|
+
* order hash.
|
|
587
|
+
*
|
|
588
|
+
* @returns Transaction hash of the validate transaction
|
|
407
589
|
*/
|
|
408
590
|
async createOfferAndValidateOnchain({
|
|
409
591
|
asset,
|
package/src/sdk/orders.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
} from "@opensea/seaport-js/lib/types"
|
|
6
6
|
import type { CollectionOffer, Listing, NFT, Offer } from "../api/types"
|
|
7
7
|
import { INVERSE_BASIS_POINT, ZERO_ADDRESS } from "../constants"
|
|
8
|
+
import { executeApprovalsAndGetOrderComponents } from "../orders/orderUseCase"
|
|
8
9
|
import type { ProtocolData } from "../orders/types"
|
|
9
10
|
import {
|
|
10
11
|
type Amount,
|
|
@@ -143,7 +144,10 @@ export class OrdersManager {
|
|
|
143
144
|
/**
|
|
144
145
|
* Build listing order without submitting to API
|
|
145
146
|
* @param options Listing parameters
|
|
146
|
-
* @returns
|
|
147
|
+
* @returns The seaport-js use case. Call `executeAllActions()` to approve and
|
|
148
|
+
* sign it for API submission, or
|
|
149
|
+
* {@link executeApprovalsAndGetOrderComponents} to approve it without a
|
|
150
|
+
* signature for onchain validation.
|
|
147
151
|
*/
|
|
148
152
|
private async _buildListingOrder({
|
|
149
153
|
asset,
|
|
@@ -212,7 +216,7 @@ export class OrdersManager {
|
|
|
212
216
|
zone = collection.requiredZone
|
|
213
217
|
}
|
|
214
218
|
|
|
215
|
-
|
|
219
|
+
return this.context.seaport.createOrder(
|
|
216
220
|
{
|
|
217
221
|
offer: offerAssetItems,
|
|
218
222
|
consideration: considerationFeeItems,
|
|
@@ -227,12 +231,15 @@ export class OrdersManager {
|
|
|
227
231
|
},
|
|
228
232
|
accountAddress,
|
|
229
233
|
)
|
|
230
|
-
|
|
231
|
-
return executeAllActions()
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
/**
|
|
235
|
-
* Build listing order components without submitting to API
|
|
237
|
+
* Build listing order components without submitting to API.
|
|
238
|
+
*
|
|
239
|
+
* Runs any token approvals the order needs, but does not ask the wallet to
|
|
240
|
+
* sign: the components are meant for onchain validation, which needs no
|
|
241
|
+
* offchain signature.
|
|
242
|
+
*
|
|
236
243
|
* @param options Listing parameters
|
|
237
244
|
* @returns OrderComponents ready for onchain validation
|
|
238
245
|
*/
|
|
@@ -261,7 +268,7 @@ export class OrdersManager {
|
|
|
261
268
|
includeOptionalCreatorFees?: boolean
|
|
262
269
|
zone?: string
|
|
263
270
|
}): Promise<OrderComponents> {
|
|
264
|
-
const
|
|
271
|
+
const useCase = await this._buildListingOrder({
|
|
265
272
|
asset,
|
|
266
273
|
accountAddress,
|
|
267
274
|
amount,
|
|
@@ -274,7 +281,7 @@ export class OrdersManager {
|
|
|
274
281
|
includeOptionalCreatorFees,
|
|
275
282
|
zone,
|
|
276
283
|
})
|
|
277
|
-
return
|
|
284
|
+
return executeApprovalsAndGetOrderComponents(useCase)
|
|
278
285
|
}
|
|
279
286
|
|
|
280
287
|
/**
|
|
@@ -334,7 +341,7 @@ export class OrdersManager {
|
|
|
334
341
|
zone = collection.requiredZone
|
|
335
342
|
}
|
|
336
343
|
|
|
337
|
-
|
|
344
|
+
return this.context.seaport.createOrder(
|
|
338
345
|
{
|
|
339
346
|
offer: [
|
|
340
347
|
{
|
|
@@ -355,12 +362,15 @@ export class OrdersManager {
|
|
|
355
362
|
},
|
|
356
363
|
accountAddress,
|
|
357
364
|
)
|
|
358
|
-
|
|
359
|
-
return executeAllActions()
|
|
360
365
|
}
|
|
361
366
|
|
|
362
367
|
/**
|
|
363
|
-
* Build offer order components without submitting to API
|
|
368
|
+
* Build offer order components without submitting to API.
|
|
369
|
+
*
|
|
370
|
+
* Runs any token approvals the order needs, but does not ask the wallet to
|
|
371
|
+
* sign: the components are meant for onchain validation, which needs no
|
|
372
|
+
* offchain signature.
|
|
373
|
+
*
|
|
364
374
|
* @param options Offer parameters
|
|
365
375
|
* @returns OrderComponents ready for onchain validation
|
|
366
376
|
*/
|
|
@@ -383,7 +393,7 @@ export class OrdersManager {
|
|
|
383
393
|
expirationTime?: Amount
|
|
384
394
|
zone?: string
|
|
385
395
|
}): Promise<OrderComponents> {
|
|
386
|
-
const
|
|
396
|
+
const useCase = await this._buildOfferOrder({
|
|
387
397
|
asset,
|
|
388
398
|
accountAddress,
|
|
389
399
|
amount,
|
|
@@ -393,7 +403,7 @@ export class OrdersManager {
|
|
|
393
403
|
expirationTime,
|
|
394
404
|
zone,
|
|
395
405
|
})
|
|
396
|
-
return
|
|
406
|
+
return executeApprovalsAndGetOrderComponents(useCase)
|
|
397
407
|
}
|
|
398
408
|
|
|
399
409
|
/**
|
|
@@ -433,7 +443,7 @@ export class OrdersManager {
|
|
|
433
443
|
expirationTime?: Amount
|
|
434
444
|
zone?: string
|
|
435
445
|
}): Promise<Offer> {
|
|
436
|
-
const
|
|
446
|
+
const useCase = await this._buildOfferOrder({
|
|
437
447
|
asset,
|
|
438
448
|
accountAddress,
|
|
439
449
|
amount,
|
|
@@ -443,6 +453,7 @@ export class OrdersManager {
|
|
|
443
453
|
expirationTime,
|
|
444
454
|
zone,
|
|
445
455
|
})
|
|
456
|
+
const order = await useCase.executeAllActions()
|
|
446
457
|
|
|
447
458
|
return this.context.api.postOffer(
|
|
448
459
|
order,
|
|
@@ -495,7 +506,7 @@ export class OrdersManager {
|
|
|
495
506
|
includeOptionalCreatorFees?: boolean
|
|
496
507
|
zone?: string
|
|
497
508
|
}): Promise<Listing> {
|
|
498
|
-
const
|
|
509
|
+
const useCase = await this._buildListingOrder({
|
|
499
510
|
asset,
|
|
500
511
|
accountAddress,
|
|
501
512
|
amount,
|
|
@@ -508,6 +519,7 @@ export class OrdersManager {
|
|
|
508
519
|
includeOptionalCreatorFees,
|
|
509
520
|
zone,
|
|
510
521
|
})
|
|
522
|
+
const order = await useCase.executeAllActions()
|
|
511
523
|
|
|
512
524
|
return this.context.api.postListing(
|
|
513
525
|
order,
|