@mysten/kiosk
Advanced tools
Comparing version 0.0.0-experimental-20230530115307 to 0.0.0-experimental-20230530145812
@@ -29,2 +29,4 @@ "use strict"; | ||
TRANSFER_POLICY_RULES_PACKAGE_ADDRESS: () => TRANSFER_POLICY_RULES_PACKAGE_ADDRESS, | ||
attachListingsAndPrices: () => attachListingsAndPrices, | ||
attachLockedItems: () => attachLockedItems, | ||
bcs: () => import_sui.bcs, | ||
@@ -42,3 +44,2 @@ borrow: () => borrow, | ||
getKioskObject: () => getKioskObject, | ||
getObjects: () => getObjects, | ||
getTypeWithoutPackageAddress: () => getTypeWithoutPackageAddress, | ||
@@ -62,2 +63,5 @@ list: () => list, | ||
// src/utils.ts | ||
var import_sui2 = require("@mysten/sui.js"); | ||
// src/bcs.ts | ||
@@ -113,3 +117,3 @@ var import_sui = require("@mysten/sui.js"); | ||
} | ||
function extractKioskData(data) { | ||
function extractKioskData(data, listings, lockedItemIds) { | ||
return data.reduce( | ||
@@ -122,4 +126,5 @@ (acc, val) => { | ||
acc.items.push({ | ||
itemId: val.objectId, | ||
itemType: val.objectType | ||
objectId: val.objectId, | ||
type: val.objectType, | ||
isLocked: false | ||
}); | ||
@@ -129,4 +134,4 @@ break; | ||
acc.listingIds.push(val.objectId); | ||
acc.listings.push({ | ||
itemId: val.name.value.id, | ||
listings.push({ | ||
objectId: val.name.value.id, | ||
listingId: val.objectId, | ||
@@ -136,17 +141,44 @@ isExclusive: val.name.value.is_exclusive | ||
break; | ||
case "kiosk::Lock": | ||
lockedItemIds?.push(val.name.value.id); | ||
break; | ||
} | ||
return acc; | ||
}, | ||
{ listings: [], items: [], itemIds: [], listingIds: [] } | ||
{ items: [], itemIds: [], listingIds: [], extensions: [] } | ||
); | ||
} | ||
function getObjects(provider, ids, options) { | ||
if (ids.length === 0) { | ||
return Promise.resolve([]); | ||
} | ||
return provider.multiGetObjects({ ids, options }); | ||
} | ||
var getTypeWithoutPackageAddress = (type) => { | ||
return type.split("::").slice(-2).join("::"); | ||
}; | ||
var attachListingsAndPrices = (kioskData, listings, listingObjects) => { | ||
const itemListings = listings.reduce( | ||
(acc, item, idx) => { | ||
acc[item.objectId] = { ...item }; | ||
if (listingObjects.length === 0) | ||
return acc; | ||
const data = (0, import_sui2.getObjectFields)(listingObjects[idx]); | ||
if (!data) | ||
return acc; | ||
acc[item.objectId].price = data.value; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
kioskData.items.map((item) => { | ||
item.listing = itemListings[item.objectId] || void 0; | ||
}); | ||
}; | ||
var attachLockedItems = (kioskData, lockedItemIds) => { | ||
const lockedStatuses = lockedItemIds.reduce( | ||
(acc, item) => { | ||
acc[item] = true; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
kioskData.items.map((item) => { | ||
item.isLocked = lockedStatuses[item.objectId] || false; | ||
}); | ||
}; | ||
@@ -389,9 +421,3 @@ // src/tx/transfer-policy.ts | ||
// src/query/kiosk.ts | ||
var import_sui2 = require("@mysten/sui.js"); | ||
async function fetchKiosk(provider, kioskId, pagination, { | ||
includeKioskFields = false, | ||
includeItems = false, | ||
withListingPrices = false, | ||
itemOptions = { showDisplay: true, showType: true } | ||
}) { | ||
async function fetchKiosk(provider, kioskId, pagination, options) { | ||
const { data, nextCursor, hasNextPage } = await provider.getDynamicFields({ | ||
@@ -401,21 +427,18 @@ parentId: kioskId, | ||
}); | ||
const kioskData = extractKioskData(data); | ||
const [kiosk, itemObjects, listingObjects] = await Promise.all([ | ||
includeKioskFields ? getKioskObject(provider, kioskId) : Promise.resolve(void 0), | ||
includeItems ? getObjects(provider, kioskData.itemIds, itemOptions) : Promise.resolve([]), | ||
withListingPrices ? getObjects(provider, kioskData.listingIds, { | ||
showBcs: true, | ||
showContent: true | ||
const listings = []; | ||
const lockedItemIds = []; | ||
const kioskData = extractKioskData(data, listings, lockedItemIds); | ||
const [kiosk, listingObjects] = await Promise.all([ | ||
options.withKioskFields ? getKioskObject(provider, kioskId) : Promise.resolve(void 0), | ||
options.withListingPrices ? provider.multiGetObjects({ | ||
ids: kioskData.listingIds, | ||
options: { | ||
showContent: true | ||
} | ||
}) : Promise.resolve([]) | ||
]); | ||
if (includeKioskFields) | ||
if (options.withKioskFields) | ||
kioskData.kiosk = kiosk; | ||
if (includeItems) | ||
kioskData.items = itemObjects; | ||
if (withListingPrices) | ||
kioskData.listings.map((l, i) => { | ||
const fields = (0, import_sui2.getObjectFields)(listingObjects[i]); | ||
l.price = fields?.value; | ||
return l; | ||
}); | ||
attachListingsAndPrices(kioskData, listings, listingObjects); | ||
attachLockedItems(kioskData, lockedItemIds); | ||
return { | ||
@@ -469,2 +492,4 @@ data: kioskData, | ||
TRANSFER_POLICY_RULES_PACKAGE_ADDRESS, | ||
attachListingsAndPrices, | ||
attachLockedItems, | ||
bcs, | ||
@@ -482,3 +507,2 @@ borrow, | ||
getKioskObject, | ||
getObjects, | ||
getTypeWithoutPackageAddress, | ||
@@ -485,0 +509,0 @@ list, |
# @mysten/kiosk | ||
## 0.0.0-experimental-20230530115307 | ||
## 0.0.0-experimental-20230530145812 | ||
### Minor Changes | ||
- c322a230da: Fix fetchKiosk consistency/naming, include locked state in items | ||
## 1.0.0 | ||
### Major Changes | ||
@@ -20,2 +26,2 @@ | ||
- Updated dependencies [bb50698551] | ||
- @mysten/sui.js@0.0.0-experimental-20230530115307 | ||
- @mysten/sui.js@0.35.0 |
@@ -29,2 +29,4 @@ "use strict"; | ||
TRANSFER_POLICY_RULES_PACKAGE_ADDRESS: () => TRANSFER_POLICY_RULES_PACKAGE_ADDRESS, | ||
attachListingsAndPrices: () => attachListingsAndPrices, | ||
attachLockedItems: () => attachLockedItems, | ||
bcs: () => import_sui.bcs, | ||
@@ -42,3 +44,2 @@ borrow: () => borrow, | ||
getKioskObject: () => getKioskObject, | ||
getObjects: () => getObjects, | ||
getTypeWithoutPackageAddress: () => getTypeWithoutPackageAddress, | ||
@@ -62,2 +63,5 @@ list: () => list, | ||
// src/utils.ts | ||
var import_sui2 = require("@mysten/sui.js"); | ||
// src/bcs.ts | ||
@@ -113,3 +117,3 @@ var import_sui = require("@mysten/sui.js"); | ||
} | ||
function extractKioskData(data) { | ||
function extractKioskData(data, listings, lockedItemIds) { | ||
return data.reduce( | ||
@@ -122,4 +126,5 @@ (acc, val) => { | ||
acc.items.push({ | ||
itemId: val.objectId, | ||
itemType: val.objectType | ||
objectId: val.objectId, | ||
type: val.objectType, | ||
isLocked: false | ||
}); | ||
@@ -129,4 +134,4 @@ break; | ||
acc.listingIds.push(val.objectId); | ||
acc.listings.push({ | ||
itemId: val.name.value.id, | ||
listings.push({ | ||
objectId: val.name.value.id, | ||
listingId: val.objectId, | ||
@@ -136,17 +141,44 @@ isExclusive: val.name.value.is_exclusive | ||
break; | ||
case "kiosk::Lock": | ||
lockedItemIds?.push(val.name.value.id); | ||
break; | ||
} | ||
return acc; | ||
}, | ||
{ listings: [], items: [], itemIds: [], listingIds: [] } | ||
{ items: [], itemIds: [], listingIds: [], extensions: [] } | ||
); | ||
} | ||
function getObjects(provider, ids, options) { | ||
if (ids.length === 0) { | ||
return Promise.resolve([]); | ||
} | ||
return provider.multiGetObjects({ ids, options }); | ||
} | ||
var getTypeWithoutPackageAddress = (type) => { | ||
return type.split("::").slice(-2).join("::"); | ||
}; | ||
var attachListingsAndPrices = (kioskData, listings, listingObjects) => { | ||
const itemListings = listings.reduce( | ||
(acc, item, idx) => { | ||
acc[item.objectId] = { ...item }; | ||
if (listingObjects.length === 0) | ||
return acc; | ||
const data = (0, import_sui2.getObjectFields)(listingObjects[idx]); | ||
if (!data) | ||
return acc; | ||
acc[item.objectId].price = data.value; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
kioskData.items.map((item) => { | ||
item.listing = itemListings[item.objectId] || void 0; | ||
}); | ||
}; | ||
var attachLockedItems = (kioskData, lockedItemIds) => { | ||
const lockedStatuses = lockedItemIds.reduce( | ||
(acc, item) => { | ||
acc[item] = true; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
kioskData.items.map((item) => { | ||
item.isLocked = lockedStatuses[item.objectId] || false; | ||
}); | ||
}; | ||
@@ -389,9 +421,3 @@ // src/tx/transfer-policy.ts | ||
// src/query/kiosk.ts | ||
var import_sui2 = require("@mysten/sui.js"); | ||
async function fetchKiosk(provider, kioskId, pagination, { | ||
includeKioskFields = false, | ||
includeItems = false, | ||
withListingPrices = false, | ||
itemOptions = { showDisplay: true, showType: true } | ||
}) { | ||
async function fetchKiosk(provider, kioskId, pagination, options) { | ||
const { data, nextCursor, hasNextPage } = await provider.getDynamicFields({ | ||
@@ -401,21 +427,18 @@ parentId: kioskId, | ||
}); | ||
const kioskData = extractKioskData(data); | ||
const [kiosk, itemObjects, listingObjects] = await Promise.all([ | ||
includeKioskFields ? getKioskObject(provider, kioskId) : Promise.resolve(void 0), | ||
includeItems ? getObjects(provider, kioskData.itemIds, itemOptions) : Promise.resolve([]), | ||
withListingPrices ? getObjects(provider, kioskData.listingIds, { | ||
showBcs: true, | ||
showContent: true | ||
const listings = []; | ||
const lockedItemIds = []; | ||
const kioskData = extractKioskData(data, listings, lockedItemIds); | ||
const [kiosk, listingObjects] = await Promise.all([ | ||
options.withKioskFields ? getKioskObject(provider, kioskId) : Promise.resolve(void 0), | ||
options.withListingPrices ? provider.multiGetObjects({ | ||
ids: kioskData.listingIds, | ||
options: { | ||
showContent: true | ||
} | ||
}) : Promise.resolve([]) | ||
]); | ||
if (includeKioskFields) | ||
if (options.withKioskFields) | ||
kioskData.kiosk = kiosk; | ||
if (includeItems) | ||
kioskData.items = itemObjects; | ||
if (withListingPrices) | ||
kioskData.listings.map((l, i) => { | ||
const fields = (0, import_sui2.getObjectFields)(listingObjects[i]); | ||
l.price = fields?.value; | ||
return l; | ||
}); | ||
attachListingsAndPrices(kioskData, listings, listingObjects); | ||
attachLockedItems(kioskData, lockedItemIds); | ||
return { | ||
@@ -469,2 +492,4 @@ data: kioskData, | ||
TRANSFER_POLICY_RULES_PACKAGE_ADDRESS, | ||
attachListingsAndPrices, | ||
attachLockedItems, | ||
bcs, | ||
@@ -482,3 +507,2 @@ borrow, | ||
getKioskObject, | ||
getObjects, | ||
getTypeWithoutPackageAddress, | ||
@@ -485,0 +509,0 @@ list, |
@@ -1,2 +0,2 @@ | ||
import { JsonRpcProvider, PaginationArguments, SuiAddress, SuiObjectDataOptions, SuiObjectResponse } from '@mysten/sui.js'; | ||
import { JsonRpcProvider, ObjectId, ObjectType, PaginationArguments, SuiAddress } from '@mysten/sui.js'; | ||
import { Kiosk } from '../bcs'; | ||
@@ -9,3 +9,3 @@ /** | ||
/** The ID of the Item */ | ||
itemId: string; | ||
objectId: ObjectId; | ||
/** | ||
@@ -19,3 +19,3 @@ * Whether or not there's a `PurchaseCap` issued. `true` means that | ||
/** The ID of the listing */ | ||
listingId: string; | ||
listingId: ObjectId; | ||
price?: string; | ||
@@ -29,5 +29,9 @@ }; | ||
/** The ID of the Item */ | ||
itemId: string; | ||
objectId: ObjectId; | ||
/** The type of the Item */ | ||
itemType: string; | ||
type: ObjectType; | ||
/** Whether the item is Locked (there must be a `Lock` Dynamic Field) */ | ||
isLocked: boolean; | ||
/** Optional listing */ | ||
listing?: KioskListing; | ||
}; | ||
@@ -38,7 +42,7 @@ /** | ||
export type KioskData = { | ||
items: KioskItem[] | SuiObjectResponse[]; | ||
listings: KioskListing[]; | ||
itemIds: string[]; | ||
listingIds: string[]; | ||
items: KioskItem[]; | ||
itemIds: ObjectId[]; | ||
listingIds: ObjectId[]; | ||
kiosk?: Kiosk; | ||
extensions: any[]; | ||
}; | ||
@@ -51,11 +55,5 @@ export type PagedKioskData = { | ||
export type FetchKioskOptions = { | ||
includeKioskFields?: boolean; | ||
includeItems?: boolean; | ||
itemOptions?: SuiObjectDataOptions; | ||
withKioskFields?: boolean; | ||
withListingPrices?: boolean; | ||
listingOptions?: SuiObjectDataOptions; | ||
}; | ||
/** | ||
* | ||
*/ | ||
export declare function fetchKiosk(provider: JsonRpcProvider, kioskId: SuiAddress, pagination: PaginationArguments<string>, { includeKioskFields, includeItems, withListingPrices, itemOptions, }: FetchKioskOptions): Promise<PagedKioskData>; | ||
export declare function fetchKiosk(provider: JsonRpcProvider, kioskId: SuiAddress, pagination: PaginationArguments<string>, options: FetchKioskOptions): Promise<PagedKioskData>; |
@@ -1,3 +0,3 @@ | ||
import { JsonRpcProvider, SharedObjectRef, SuiObjectDataOptions, SuiObjectRef, TransactionArgument, TransactionBlock } from '@mysten/sui.js'; | ||
import { KioskData } from './query/kiosk'; | ||
import { JsonRpcProvider, ObjectId, SharedObjectRef, SuiObjectRef, SuiObjectResponse, TransactionArgument, TransactionBlock } from '@mysten/sui.js'; | ||
import { KioskData, KioskListing } from './query/kiosk'; | ||
import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields'; | ||
@@ -18,59 +18,11 @@ import { Kiosk } from './bcs'; | ||
export declare function getKioskObject(provider: JsonRpcProvider, id: string): Promise<Kiosk>; | ||
export declare function extractKioskData(data: DynamicFieldInfo[]): KioskData; | ||
export declare function getObjects(provider: JsonRpcProvider, ids: string[], options: SuiObjectDataOptions): Promise<{ | ||
data?: { | ||
objectId: string; | ||
version: string; | ||
digest: string; | ||
type?: string | undefined; | ||
bcs?: { | ||
type: string; | ||
version: number; | ||
hasPublicTransfer: boolean; | ||
dataType: "moveObject"; | ||
bcsBytes: string; | ||
} | { | ||
id: string; | ||
dataType: "package"; | ||
moduleMap: Record<string, string>; | ||
} | undefined; | ||
owner?: "Immutable" | { | ||
AddressOwner: string; | ||
} | { | ||
ObjectOwner: string; | ||
} | { | ||
Shared: { | ||
initial_shared_version: number; | ||
}; | ||
} | undefined; | ||
storageRebate?: string | undefined; | ||
previousTransaction?: string | undefined; | ||
content?: { | ||
type: string; | ||
fields: Record<string, any>; | ||
hasPublicTransfer: boolean; | ||
dataType: "moveObject"; | ||
} | { | ||
disassembled: Record<string, string>; | ||
dataType: "package"; | ||
} | undefined; | ||
display?: Record<string, string> | { | ||
data: Record<string, string> | null; | ||
error: { | ||
code: string; | ||
version?: number | undefined; | ||
digest?: string | undefined; | ||
error?: string | undefined; | ||
object_id?: string | undefined; | ||
} | null; | ||
} | undefined; | ||
} | undefined; | ||
error?: { | ||
code: string; | ||
version?: number | undefined; | ||
digest?: string | undefined; | ||
error?: string | undefined; | ||
object_id?: string | undefined; | ||
} | undefined; | ||
}[]>; | ||
export declare function extractKioskData(data: DynamicFieldInfo[], listings: KioskListing[], lockedItemIds: string[]): KioskData; | ||
export declare const getTypeWithoutPackageAddress: (type: string) => string; | ||
/** | ||
* A helper that attaches the listing prices to kiosk listings. | ||
*/ | ||
export declare const attachListingsAndPrices: (kioskData: KioskData, listings: KioskListing[], listingObjects: SuiObjectResponse[]) => void; | ||
/** | ||
* A Helper to attach locked state to items in Kiosk Data. | ||
*/ | ||
export declare const attachLockedItems: (kioskData: KioskData, lockedItemIds: ObjectId[]) => void; |
@@ -5,3 +5,3 @@ { | ||
"description": "Sui Kiosk library", | ||
"version": "0.0.0-experimental-20230530115307", | ||
"version": "0.0.0-experimental-20230530145812", | ||
"license": "Apache-2.0", | ||
@@ -28,3 +28,3 @@ "main": "./dist/index.js", | ||
"dependencies": { | ||
"@mysten/sui.js": "0.0.0-experimental-20230530115307" | ||
"@mysten/sui.js": "0.35.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -6,3 +6,2 @@ # Kiosk SDK | ||
This Kiosk SDK library provides different utilities to interact/create/manage a [Kiosk](https://github.com/MystenLabs/sui/tree/main/kiosk). | ||
@@ -42,5 +41,10 @@ | ||
hasNextPage, | ||
} = await fetchKiosk(provider, kioskAddress, { limit: 100 }); // could also add `cursor` for pagination | ||
} = await fetchKiosk( | ||
provider, | ||
kioskAddress, | ||
{ limit: 100 }, | ||
{ withListingPrices: true, withKioskFields: true }, | ||
); // could also add `cursor` for pagination | ||
console.log(res); // { listings: [], items: [], itemIds: [], listingIds: [] } | ||
console.log(res); // { items: [], itemIds: [], listingIds: [], kiosk: {...} } | ||
console.log(nextCursor); // null | ||
@@ -54,2 +58,51 @@ console.log(hasNextPage); // false | ||
<details> | ||
<summary>Purchasing an item (currently supports royalty rule deployed on testnet or no rules)</summary> | ||
```typescript | ||
import { fetchKiosk } from '@mysten/kiosk'; | ||
import { Connection, JsonRpcProvider } from '@mysten/sui.js'; | ||
const provider = new JsonRpcProvider( | ||
new Connection({ fullnode: 'https://fullnode.testnet.sui.io:443' }), | ||
); | ||
// the kiosk we're purchasing from | ||
const kioskId = `0xSomeKioskAddress`; | ||
// a sample item retrieved from `fetchKiosk` function (or hard-coded) | ||
const item = { | ||
isLocked: false, | ||
objectId: "0xb892d61a9992a10c9453efcdbd14ca9720d7dc1000a2048224209c9e544ed223" | ||
type: "0x52852c4ba80040395b259c641e70b702426a58990ff73cecf5afd31954429090::test::TestItem", | ||
listing: { | ||
isExclusive: false, | ||
listingId: "0x368b512ff2514dbea814f26ec9a3d41198c00e8ed778099961e9ed22a9f0032b", | ||
price: "20000000000" // in MIST | ||
} | ||
} | ||
const purchaseItem = async (item, kioskId) => { | ||
// fetch the policy of the item (could be an array, if there's more than one transfer policy) | ||
const policies = await queryTransferPolicy(provider, item.type); | ||
// selecting the first one for simplicity. | ||
const policyId = policy[0]?.id; | ||
// initialize tx block. | ||
const tx = new TransactionBlock(); | ||
// Purchases the item. Right now it also resolves a royalty rule, if one exists. | ||
// There will be some additional work to support further rules & custom ones soon. | ||
const purchasedItem = purchaseAndResolvePolicies(tx, item.type, item.listing, kioskId, item.objectId, policy[0]); | ||
// now we need to decide what to do with the item | ||
// ... e.g. place() // places the item to the user's kiosk. | ||
// (NOT YET SUPPORTED BUT WORTH MENTIONING if the item has the `kiosk_lock` rule, the resolver will place it in the kiosk automatically. | ||
// For now, to support this rule, we need to manually resolve the `kiosk_lock` rule and place it in our owned kiosk.) | ||
// ...finally, sign PTB & execute it. | ||
}; | ||
``` | ||
</details> | ||
<details> | ||
<summary>Create a kiosk, share it and get transfer the `kioskOwnerCap` to the wallet's address</summary> | ||
@@ -56,0 +109,0 @@ |
@@ -6,9 +6,13 @@ // Copyright (c) Mysten Labs, Inc. | ||
JsonRpcProvider, | ||
ObjectId, | ||
ObjectType, | ||
PaginationArguments, | ||
SuiAddress, | ||
SuiObjectDataOptions, | ||
SuiObjectResponse, | ||
getObjectFields, | ||
} from '@mysten/sui.js'; | ||
import { extractKioskData, getKioskObject, getObjects } from '../utils'; | ||
import { | ||
attachListingsAndPrices, | ||
attachLockedItems, | ||
extractKioskData, | ||
getKioskObject, | ||
} from '../utils'; | ||
import { Kiosk } from '../bcs'; | ||
@@ -22,3 +26,3 @@ | ||
/** The ID of the Item */ | ||
itemId: string; | ||
objectId: ObjectId; | ||
/** | ||
@@ -32,3 +36,3 @@ * Whether or not there's a `PurchaseCap` issued. `true` means that | ||
/** The ID of the listing */ | ||
listingId: string; | ||
listingId: ObjectId; | ||
price?: string; | ||
@@ -43,7 +47,10 @@ }; | ||
/** The ID of the Item */ | ||
itemId: string; | ||
objectId: ObjectId; | ||
/** The type of the Item */ | ||
itemType: string; | ||
type: ObjectType; | ||
/** Whether the item is Locked (there must be a `Lock` Dynamic Field) */ | ||
isLocked: boolean; | ||
/** Optional listing */ | ||
listing?: KioskListing; | ||
}; | ||
/** | ||
@@ -53,7 +60,7 @@ * Aggregated data from the Kiosk. | ||
export type KioskData = { | ||
items: KioskItem[] | SuiObjectResponse[]; | ||
listings: KioskListing[]; | ||
itemIds: string[]; | ||
listingIds: string[]; | ||
items: KioskItem[]; | ||
itemIds: ObjectId[]; | ||
listingIds: ObjectId[]; | ||
kiosk?: Kiosk; | ||
extensions: any[]; // type will be defined on later versions of the SDK. | ||
}; | ||
@@ -68,12 +75,6 @@ | ||
export type FetchKioskOptions = { | ||
includeKioskFields?: boolean; | ||
includeItems?: boolean; | ||
itemOptions?: SuiObjectDataOptions; | ||
withKioskFields?: boolean; | ||
withListingPrices?: boolean; | ||
listingOptions?: SuiObjectDataOptions; | ||
}; | ||
/** | ||
* | ||
*/ | ||
export async function fetchKiosk( | ||
@@ -83,8 +84,3 @@ provider: JsonRpcProvider, | ||
pagination: PaginationArguments<string>, | ||
{ | ||
includeKioskFields = false, | ||
includeItems = false, | ||
withListingPrices = false, | ||
itemOptions = { showDisplay: true, showType: true }, | ||
}: FetchKioskOptions, | ||
options: FetchKioskOptions, | ||
): Promise<PagedKioskData> { | ||
@@ -96,4 +92,7 @@ const { data, nextCursor, hasNextPage } = await provider.getDynamicFields({ | ||
const listings: KioskListing[] = []; | ||
const lockedItemIds: ObjectId[] = []; | ||
// extracted kiosk data. | ||
const kioskData = extractKioskData(data); | ||
const kioskData = extractKioskData(data, listings, lockedItemIds); | ||
@@ -103,13 +102,12 @@ // split the fetching in two queries as we are most likely passing different options for each kind. | ||
// For listings we usually seek the DF value (price) / exclusivity. | ||
const [kiosk, itemObjects, listingObjects] = await Promise.all([ | ||
includeKioskFields | ||
const [kiosk, listingObjects] = await Promise.all([ | ||
options.withKioskFields | ||
? getKioskObject(provider, kioskId) | ||
: Promise.resolve(undefined), | ||
includeItems | ||
? getObjects(provider, kioskData.itemIds, itemOptions) | ||
: Promise.resolve([]), | ||
withListingPrices | ||
? getObjects(provider, kioskData.listingIds, { | ||
showBcs: true, | ||
showContent: true, | ||
options.withListingPrices | ||
? provider.multiGetObjects({ | ||
ids: kioskData.listingIds, | ||
options: { | ||
showContent: true, | ||
}, | ||
}) | ||
@@ -119,12 +117,7 @@ : Promise.resolve([]), | ||
if (includeKioskFields) kioskData.kiosk = kiosk; | ||
if (includeItems) kioskData.items = itemObjects; | ||
if (withListingPrices) | ||
kioskData.listings.map((l, i) => { | ||
const fields = getObjectFields(listingObjects[i]); | ||
// l.price = bcs.de('u64', listingObjects[i].data?.bcs.bcsBytes, 'base64'); | ||
// TODO: Figure out a way to do this with BCS to avoid querying content. | ||
l.price = fields?.value; | ||
return l; | ||
}); | ||
if (options.withKioskFields) kioskData.kiosk = kiosk; | ||
// attach items listings. IF we have `options.withListingPrices === true`, it will also attach the prices. | ||
attachListingsAndPrices(kioskData, listings, listingObjects); | ||
// add `locked` status to items that are locked. | ||
attachLockedItems(kioskData, lockedItemIds); | ||
@@ -131,0 +124,0 @@ return { |
@@ -6,9 +6,11 @@ // Copyright (c) Mysten Labs, Inc. | ||
JsonRpcProvider, | ||
ObjectId, | ||
SharedObjectRef, | ||
SuiObjectDataOptions, | ||
SuiObjectRef, | ||
SuiObjectResponse, | ||
TransactionArgument, | ||
TransactionBlock, | ||
getObjectFields, | ||
} from '@mysten/sui.js'; | ||
import { KioskData } from './query/kiosk'; | ||
import { KioskData, KioskListing } from './query/kiosk'; | ||
import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields'; | ||
@@ -74,3 +76,7 @@ import { bcs, Kiosk } from './bcs'; | ||
// helper to extract kiosk data from dynamic fields. | ||
export function extractKioskData(data: DynamicFieldInfo[]): KioskData { | ||
export function extractKioskData( | ||
data: DynamicFieldInfo[], | ||
listings: KioskListing[], | ||
lockedItemIds: string[], | ||
): KioskData { | ||
return data.reduce<KioskData>( | ||
@@ -84,4 +90,5 @@ (acc: KioskData, val: DynamicFieldInfo) => { | ||
acc.items.push({ | ||
itemId: val.objectId, | ||
itemType: val.objectType, | ||
objectId: val.objectId, | ||
type: val.objectType, | ||
isLocked: false, | ||
}); | ||
@@ -91,4 +98,4 @@ break; | ||
acc.listingIds.push(val.objectId); | ||
acc.listings.push({ | ||
itemId: val.name.value.id, | ||
listings.push({ | ||
objectId: val.name.value.id, | ||
listingId: val.objectId, | ||
@@ -98,22 +105,12 @@ isExclusive: val.name.value.is_exclusive, | ||
break; | ||
case 'kiosk::Lock': | ||
lockedItemIds?.push(val.name.value.id); | ||
break; | ||
} | ||
return acc; | ||
}, | ||
{ listings: [], items: [], itemIds: [], listingIds: [] }, | ||
{ items: [], itemIds: [], listingIds: [], extensions: [] }, | ||
); | ||
} | ||
// simple multiGetObjects wrapper to simplify cases on functions. | ||
export function getObjects( | ||
provider: JsonRpcProvider, | ||
ids: string[], | ||
options: SuiObjectDataOptions, | ||
) { | ||
if (ids.length === 0) { | ||
return Promise.resolve([]); | ||
} | ||
return provider.multiGetObjects({ ids, options }); | ||
} | ||
// e.g. 0x2::kiosk::Item -> kiosk::Item | ||
@@ -123,1 +120,55 @@ export const getTypeWithoutPackageAddress = (type: string) => { | ||
}; | ||
/** | ||
* A helper that attaches the listing prices to kiosk listings. | ||
*/ | ||
export const attachListingsAndPrices = ( | ||
kioskData: KioskData, | ||
listings: KioskListing[], | ||
listingObjects: SuiObjectResponse[], | ||
) => { | ||
// map item listings as {item_id: KioskListing} | ||
// for easier mapping on the nex | ||
const itemListings = listings.reduce<Record<ObjectId, KioskListing>>( | ||
(acc: Record<ObjectId, KioskListing>, item, idx) => { | ||
acc[item.objectId] = { ...item }; | ||
// return in case we don't have any listing objects. | ||
// that's the case when we don't have the `listingPrices` included. | ||
if (listingObjects.length === 0) return acc; | ||
const data = getObjectFields(listingObjects[idx]); | ||
if (!data) return acc; | ||
acc[item.objectId].price = data.value; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
kioskData.items.map((item) => { | ||
item.listing = itemListings[item.objectId] || undefined; | ||
}); | ||
}; | ||
/** | ||
* A Helper to attach locked state to items in Kiosk Data. | ||
*/ | ||
export const attachLockedItems = ( | ||
kioskData: KioskData, | ||
lockedItemIds: ObjectId[], | ||
) => { | ||
// map lock status in an array of type { item_id: true } | ||
const lockedStatuses = lockedItemIds.reduce<Record<ObjectId, boolean>>( | ||
(acc: Record<ObjectId, boolean>, item: string) => { | ||
acc[item] = true; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
// parse lockedItemIds and attach their locked status. | ||
kioskData.items.map((item) => { | ||
item.isLocked = lockedStatuses[item.objectId] || false; | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167668
2553
173
+ Added@mysten/sui.js@0.35.0(transitive)
- Removed@mysten/sui.js@0.0.0-experimental-20230530115307(transitive)
Updated@mysten/sui.js@0.35.0