Kiosk SDK
This package is still in active development. Use at your own risk. Currently, the only
supported environment is Sui Testnet.
This Kiosk SDK library provides different utilities to interact/create/manage a
Kiosk.
Installation
To install, add @mysten/kiosk
package to your project
npm i @mysten/kiosk
You can also use your preferred package manager, such as yarn or pnpm.
Examples
Here are some indicative examples on how to use the kiosk SDK.
Getting the listings & items by the kiosk's id
import { fetchKiosk } from '@mysten/kiosk';
import { SuiClient } from '@mysten/sui.js/client';
const client = new SuiClient(
url: 'https://fullnode.testnet.sui.io:443',
);
const getKiosk = async () => {
const kioskAddress = `0xSomeKioskAddress`;
const { data } = await fetchKiosk(
client,
kioskAddress,
{},
{ withListingPrices: true, withKioskFields: true },
);
console.log(data);
};
Purchasing an item (currently supports royalty rule, kiosk_lock_rule, no rules, (combination works too))
import { queryTransferPolicy, purchaseAndResolvePolicies, place, testnetEnvironment } from '@mysten/kiosk';
import { SuiClient } from '@mysten/sui.js/client';
const client = new SuiClient(
url: 'https://fullnode.testnet.sui.io:443',
);
const kioskId = `0xSomeKioskAddress`;
const item = {
isLocked: false,
objectId: "0xb892d61a9992a10c9453efcdbd14ca9720d7dc1000a2048224209c9e544ed223"
type: "0x52852c4ba80040395b259c641e70b702426a58990ff73cecf5afd31954429090::test::TestItem",
listing: {
isExclusive: false,
listingId: "0x368b512ff2514dbea814f26ec9a3d41198c00e8ed778099961e9ed22a9f0032b",
price: "20000000000"
}
}
const ownedKiosk = `0xMyKioskAddress`;
const ownedKioskCap = `0xMyKioskOwnerCap`;
const purchaseItem = async (item, kioskId) => {
const policies = await queryTransferPolicy(client, item.type);
const policyId = policy[0]?.id;
const tx = new TransactionBlock();
const extraParams = {
ownedKiosk,
ownedKioskCap
}
const environment = testnetEnvironment;
const result = purchaseAndResolvePolicies(tx, item.type, item.listing.price, kioskId, item.objectId, policy[0], environment, extraParams);
if(result.canTransfer) place(tx, item.type, ownedKiosk, ownedKioskCap , result.item);
};
Create a kiosk, share it and get transfer the `kioskOwnerCap` to the wallet's address
import { createKioskAndShare } from '@mysten/kiosk';
import { TransactionBlock } from '@mysten/sui.js/transactions';
const createKiosk = async () => {
const accountAddress = '0xSomeSuiAddress';
const tx = new TransactionBlock();
const kiosk_cap = createKioskAndShare(tx);
tx.transferObjects([kiosk_cap], tx.pure(accountAddress, 'address'));
};
Place an item and list it for sale in the kiosk
import { placeAndList } from '@mysten/kiosk';
import { TransactionBlock } from '@mysten/sui.js/transactions';
const placeAndListToKiosk = async () => {
const kiosk = 'SomeKioskId';
const kioskCap = 'KioskCapObjectId';
const itemType = '0xItemAddr::some:ItemType';
const item = 'SomeItemId';
const price = '100000';
const tx = new TransactionBlock();
placeAndList(tx, itemType, kiosk, kioskCap, item, price);
};
Withdraw profits from your kiosk
import { withdrawFromKiosk } from '@mysten/kiosk';
import { TransactionBlock } from '@mysten/sui.js/transactions';
const withdraw = async () => {
const kiosk = 'SomeKioskId';
const kioskCap = 'KioskCapObjectId';
const address = '0xSomeAddressThatReceivesTheFunds';
const amount = '100000';
const tx = new TransactionBlock();
withdrawFromKiosk(tx, kiosk, kioskCap, amount);
tx.transferObjects([coin], tx.pure(address, 'address'));
};