opensea-js
Version:
TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data
130 lines • 6.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPrivateListingFulfillments = exports.constructPrivateListingCounterOrder = exports.getPrivateListingConsiderations = exports.computePrivateListingValue = void 0;
const constants_1 = require("@opensea/seaport-js/lib/constants");
const item_1 = require("@opensea/seaport-js/lib/utils/item");
const order_1 = require("@opensea/seaport-js/lib/utils/order");
const ethers_1 = require("ethers");
/**
* Compute the native currency (ETH) value required to fulfill a private listing.
* Sums all native currency consideration items not going to the taker.
* @param order The private listing order
* @param takerAddress The address of the private listing recipient
* @returns The total native currency value as a bigint
*/
const computePrivateListingValue = (order, takerAddress) => {
return order.parameters.consideration
.filter((item) => item.recipient.toLowerCase() !== takerAddress.toLowerCase() &&
item.token.toLowerCase() === ethers_1.ZeroAddress.toLowerCase() &&
item.itemType === constants_1.ItemType.NATIVE)
.reduce((sum, item) => sum + BigInt(item.startAmount), 0n);
};
exports.computePrivateListingValue = computePrivateListingValue;
const getPrivateListingConsiderations = (offer, privateSaleRecipient) => {
return offer.map((item) => {
return { ...item, recipient: privateSaleRecipient };
});
};
exports.getPrivateListingConsiderations = getPrivateListingConsiderations;
const constructPrivateListingCounterOrder = (order, privateSaleRecipient) => {
// Counter order offers up all the items in the private listing consideration
// besides the items that are going to the private listing recipient
const paymentItems = order.parameters.consideration.filter((item) => item.recipient.toLowerCase() !== privateSaleRecipient.toLowerCase());
// Only validate payment items if there are any (zero-payment private listings are valid)
if (paymentItems.length > 0) {
if (!paymentItems.every((item) => (0, item_1.isCurrencyItem)(item))) {
throw new Error("The consideration for the private listing did not contain only currency items");
}
if (!paymentItems.every((item) => item.itemType === paymentItems[0].itemType)) {
throw new Error("Not all currency items were the same for private order");
}
}
const { aggregatedStartAmount, aggregatedEndAmount } = paymentItems.reduce(({ aggregatedStartAmount, aggregatedEndAmount }, item) => ({
aggregatedStartAmount: aggregatedStartAmount + BigInt(item.startAmount),
aggregatedEndAmount: aggregatedEndAmount + BigInt(item.endAmount),
}), {
aggregatedStartAmount: 0n,
aggregatedEndAmount: 0n,
});
const counterOrder = {
parameters: {
...order.parameters,
offerer: privateSaleRecipient,
// Empty offer for zero-payment private listings, single aggregated item otherwise
offer: paymentItems.length > 0
? [
{
itemType: paymentItems[0].itemType,
token: paymentItems[0].token,
identifierOrCriteria: paymentItems[0].identifierOrCriteria,
startAmount: aggregatedStartAmount.toString(),
endAmount: aggregatedEndAmount.toString(),
},
]
: [],
// The consideration here is empty as the original private listing order supplies
// the taker address to receive the desired items.
consideration: [],
salt: (0, order_1.generateRandomSalt)(),
totalOriginalConsiderationItems: 0,
},
signature: "0x",
};
return counterOrder;
};
exports.constructPrivateListingCounterOrder = constructPrivateListingCounterOrder;
const getPrivateListingFulfillments = (privateListingOrder) => {
const nftRelatedFulfillments = [];
// For the original order, we need to match everything offered with every consideration item
// on the original order that's set to go to the private listing recipient
privateListingOrder.parameters.offer.forEach((offerItem, offerIndex) => {
const considerationIndex = privateListingOrder.parameters.consideration.findIndex((considerationItem) => considerationItem.itemType === offerItem.itemType &&
considerationItem.token === offerItem.token &&
considerationItem.identifierOrCriteria ===
offerItem.identifierOrCriteria);
if (considerationIndex === -1) {
throw new Error("Could not find matching offer item in the consideration for private listing");
}
nftRelatedFulfillments.push({
offerComponents: [
{
orderIndex: 0,
itemIndex: offerIndex,
},
],
considerationComponents: [
{
orderIndex: 0,
itemIndex: considerationIndex,
},
],
});
});
const currencyRelatedFulfillments = [];
// For the original order, we need to match everything offered with every consideration item
// on the original order that's set to go to the private listing recipient
privateListingOrder.parameters.consideration.forEach((considerationItem, considerationIndex) => {
if (!(0, item_1.isCurrencyItem)(considerationItem)) {
return;
}
// We always match the offer item (index 0) of the counter order (index 1)
// with all of the payment items on the private listing
currencyRelatedFulfillments.push({
offerComponents: [
{
orderIndex: 1,
itemIndex: 0,
},
],
considerationComponents: [
{
orderIndex: 0,
itemIndex: considerationIndex,
},
],
});
});
return [...nftRelatedFulfillments, ...currencyRelatedFulfillments];
};
exports.getPrivateListingFulfillments = getPrivateListingFulfillments;
//# sourceMappingURL=privateListings.js.map