-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Kiosk SDK] Support kiosk_lock_rule
and environments
#12287
Changes from 7 commits
f6da53c
d184f09
bc28337
975d1d3
2811467
1d1cb51
5620950
6cc4830
f0d7f0a
a19a823
245de44
059f53d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mysten/kiosk": minor | ||
--- | ||
|
||
Support kiosk_lock_rule and environment support for rules package. Breaks `purchaseAndResolvePolicies` as it changes signature and return format. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { JsonRpcProvider } from '@mysten/sui.js'; | ||
import { TransferPolicy, bcs } from '../bcs'; | ||
|
||
/** Name of the event emitted when a TransferPolicy for T is created. */ | ||
export const TRANSFER_POLICY_CREATED_EVENT = `0x2::transfer_policy::TransferPolicyCreated`; | ||
import { bcs } from '../bcs'; | ||
import { | ||
TRANSFER_POLICY_CREATED_EVENT, | ||
TRANSFER_POLICY_TYPE, | ||
TransferPolicy, | ||
} from '../types'; | ||
|
||
/** | ||
* Searches the `TransferPolicy`-s for the given type. The seach is performed via | ||
|
@@ -44,15 +46,11 @@ export async function queryTransferPolicy( | |
); | ||
} | ||
|
||
let parsed = bcs.de( | ||
'0x2::transfer_policy::TransferPolicy', | ||
policy.bcs.bcsBytes, | ||
'base64', | ||
); | ||
let parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64'); | ||
|
||
return { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we could help provide initialSharedVersion for shared transfer policy. That could help build arguments for the purchase flow. |
||
id: policy?.objectId, | ||
type: `0x2::transfer_policy::TransferPolicy<${type}>`, | ||
type: `${TRANSFER_POLICY_TYPE}<${type}>`, | ||
owner: policy?.owner!, | ||
rules: parsed.rules, | ||
balance: parsed.balance, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,19 +7,22 @@ import { | |
TransactionBlock, | ||
} from '@mysten/sui.js'; | ||
|
||
import { ObjectArgument, getTypeWithoutPackageAddress, objArg } from '../utils'; | ||
import { getTypeWithoutPackageAddress, objArg } from '../utils'; | ||
import { KioskListing } from '../query/kiosk'; | ||
import { TransferPolicy } from '../bcs'; | ||
import { confirmRequest, resolveRoyaltyRule } from './transfer-policy'; | ||
|
||
/** The Kiosk module. */ | ||
export const KIOSK_MODULE = '0x2::kiosk'; | ||
|
||
/** The Kiosk type. */ | ||
export const KIOSK_TYPE = `${KIOSK_MODULE}::Kiosk`; | ||
|
||
/** The Kiosk Owner Cap Type */ | ||
export const KIOSK_OWNER_CAP = `${KIOSK_MODULE}::KioskOwnerCap`; | ||
import { | ||
confirmRequest, | ||
resolveKioskLockRule, | ||
resolveRoyaltyRule, | ||
} from './transfer-policy'; | ||
import { | ||
KIOSK_MODULE, | ||
KIOSK_TYPE, | ||
ObjectArgument, | ||
PurchaseAndResolvePoliciesResponse, | ||
PurchaseOptionalParams, | ||
RulesEnvironmentParam, | ||
TransferPolicy, | ||
} from '../types'; | ||
|
||
/** | ||
* Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple. | ||
|
@@ -200,20 +203,16 @@ export function purchase( | |
tx: TransactionBlock, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we call txb to align with other places? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this sdk, we have it defined as |
||
itemType: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some argument grouping / ordering would help. Eg kioskId, itemId, itemType? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to follow the same ordering we had on the other ones. (itemType was always first) |
||
kiosk: ObjectArgument, | ||
itemId: SuiAddress, | ||
item: ObjectArgument, | ||
manolisliolios marked this conversation as resolved.
Show resolved
Hide resolved
|
||
payment: ObjectArgument, | ||
): [TransactionArgument, TransactionArgument] { | ||
let [item, transferRequest] = tx.moveCall({ | ||
let [purchasedItem, transferRequest] = tx.moveCall({ | ||
target: `${KIOSK_MODULE}::purchase`, | ||
typeArguments: [itemType], | ||
arguments: [ | ||
objArg(tx, kiosk), | ||
tx.pure(itemId, 'address'), | ||
objArg(tx, payment), | ||
], | ||
arguments: [objArg(tx, kiosk), objArg(tx, item), objArg(tx, payment)], | ||
manolisliolios marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
return [item, transferRequest]; | ||
return [purchasedItem, transferRequest]; | ||
} | ||
|
||
/** | ||
|
@@ -335,35 +334,43 @@ export function returnValue( | |
* Completes the full purchase flow that includes: | ||
* 1. Purchasing the item. | ||
* 2. Resolving all the transfer policies (if any). | ||
* 3. Returns the PurchasedItem OR places the item in the user's kiosk (if there's a kiosk lock policy). | ||
* 3. Returns the item and whether the user can transfer it or not. | ||
* | ||
* If the item can be transferred, there's an extra transaction required by the user | ||
* to transfer it to an address or place it in their kiosk. | ||
*/ | ||
export function purchaseAndResolvePolicies( | ||
tx: TransactionBlock, | ||
itemType: string, | ||
listing: KioskListing, | ||
kioskId: string, | ||
itemId: string, | ||
kioskId: ObjectArgument, | ||
item: ObjectArgument, | ||
policy: TransferPolicy, | ||
): TransactionArgument | null { | ||
environment: RulesEnvironmentParam, | ||
extraParams?: PurchaseOptionalParams, | ||
): PurchaseAndResolvePoliciesResponse { | ||
// if we don't pass the listing or the listing doens't have a price, return. | ||
if (!listing || listing?.price === undefined) return null; | ||
if (!listing || listing?.price === undefined) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just require There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point for the price! |
||
throw new Error(`Listing not supplied.`); | ||
|
||
// Split the coin for the amount of the listing. | ||
const coin = tx.splitCoins(tx.gas, [tx.pure(listing.price)]); | ||
const coin = tx.splitCoins(tx.gas, [tx.pure(listing.price, 'u64')]); | ||
|
||
// initialize the purchase `kiosk::purchase` | ||
const [purchasedItem, transferRequest] = purchase( | ||
tx, | ||
itemType, | ||
kioskId, | ||
itemId, | ||
item, | ||
coin, | ||
); | ||
|
||
// Start resolving rules. | ||
// For now, we only support royalty rule. | ||
// Will need some tweaking to make it function properly with the other | ||
// ruleset. | ||
let hasKioskLockRule = false; | ||
|
||
for (let rule of policy.rules) { | ||
const ruleWithoutAddr = getTypeWithoutPackageAddress(rule); | ||
|
||
|
@@ -375,6 +382,24 @@ export function purchaseAndResolvePolicies( | |
listing.price, | ||
policy.id, | ||
transferRequest, | ||
environment, | ||
); | ||
break; | ||
case 'kiosk_lock_rule::Rule': | ||
manolisliolios marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!extraParams?.ownedKiosk || !extraParams?.ownedKioskCap) | ||
throw new Error( | ||
`This item type ${itemType} has a 'kiosk_lock_rule', but function call is missing user's kiosk and kioskCap params`, | ||
); | ||
hasKioskLockRule = true; | ||
resolveKioskLockRule( | ||
tx, | ||
itemType, | ||
purchasedItem, | ||
extraParams.ownedKiosk, | ||
extraParams.ownedKioskCap, | ||
policy.id, | ||
transferRequest, | ||
environment, | ||
); | ||
break; | ||
default: | ||
|
@@ -385,5 +410,8 @@ export function purchaseAndResolvePolicies( | |
// confirm the Transfer Policy request. | ||
confirmRequest(tx, itemType, policy.id, transferRequest); | ||
|
||
return purchasedItem; | ||
return { | ||
item: purchasedItem, | ||
canTransfer: !hasKioskLockRule, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we had a function to create generic events of kind: transferPolicyCreated(type):
0x2:....::Created<${type}>
. Wonder what you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@damirka Could you elaborate a bit on that?