Module sui::transfer_policy
Defines the TransferPolicy type and the logic to approve TransferRequests.
-
TransferPolicy - is a highly customizable primitive, which provides an interface for the type owner to set custom transfer rules for every deal performed in the Kiosk or a similar system that integrates with TP.
-
Once a TransferPolicy<T> is created for and shared (or frozen), the type T becomes tradable in Kiosks. On every purchase operation, a TransferRequest is created and needs to be confirmed by the TransferPolicy hot potato or transaction will fail.
-
Type owner (creator) can set any Rules as long as the ecosystem supports them. All of the Rules need to be resolved within a single transaction (eg pay royalty and pay fixed commission). Once required actions are performed, the TransferRequest can be "confirmed" via confirm_request call.
-
TransferPolicy aims to be the main interface for creators to control trades of their types and collect profits if a fee is required on sales. Custom policies can be removed at any moment, and the change will affect all instances of the type at once.
use std::address;
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::type_name;
use std::vector;
use sui::address;
use sui::bag;
use sui::balance;
use sui::coin;
use sui::config;
use sui::deny_list;
use sui::dynamic_field;
use sui::dynamic_object_field;
use sui::event;
use sui::hex;
use sui::object;
use sui::package;
use sui::sui;
use sui::table;
use sui::transfer;
use sui::tx_context;
use sui::types;
use sui::url;
use sui::vec_set;
Struct TransferRequest
A "Hot Potato" forcing the buyer to get a transfer permission from the item type (T) owner on purchase attempt.
public struct TransferRequestT
Fields
Struct TransferPolicy
A unique capability that allows the owner of the T to authorize transfers. Can only be created with the Publisher object. Although there's no limitation to how many policies can be created, for most of the cases there's no need to create more than one since any of the policies can be used to confirm the TransferRequest.
public struct TransferPolicyT has key, store
Fields
Struct TransferPolicyCap
A Capability granting the owner permission to add/remove rules as well as to withdraw and destroy_and_withdraw the TransferPolicy.
public struct TransferPolicyCapT has key, store
Fields
Struct TransferPolicyCreated
Event that is emitted when a publisher creates a new TransferPolicyCap making the discoverability and tracking the supported types easier.
public struct TransferPolicyCreatedT has copy, drop
Fields
Struct TransferPolicyDestroyed
Event that is emitted when a publisher destroys a TransferPolicyCap. Allows for tracking supported policies.
public struct TransferPolicyDestroyedT has copy, drop
Fields
Struct RuleKey
Key to store "Rule" configuration for a specific TransferPolicy.
public struct RuleKeyT has copy, drop, store
Fields
Constants
A completed rule is not set in the TransferPolicy.
const EIllegalRule: u64 = 1;
Trying to withdraw more than there is.
const ENotEnough: u64 = 5;
Trying to withdraw or close_and_withdraw with a wrong Cap.
const ENotOwner: u64 = 4;
The number of receipts does not match the TransferPolicy requirement.
const EPolicyNotSatisfied: u64 = 0;
Attempting to create a Rule that is already set.
const ERuleAlreadySet: u64 = 3;
A Rule is not set.
const EUnknownRequirement: u64 = 2;
Function new_request
Construct a new TransferRequest hot potato which requires an approving action from the creator to be destroyed / resolved. Once created, it must be confirmed in the confirm_request call otherwise the transaction will fail.
public fun new_requestT(item: sui::object::ID, paid: u64, from: sui::object::ID): sui::transfer_policy::TransferRequest<T>
Implementation
Function new
Register a type in the Kiosk system and receive a TransferPolicy and a TransferPolicyCap for the type. The TransferPolicy is required to confirm kiosk deals for the T. If there's no TransferPolicy available for use, the type can not be traded in kiosks.
public fun newT(pub: &sui::package::Publisher, ctx: &mut sui::tx_context::TxContext): (sui::transfer_policy::TransferPolicy<T>, sui::transfer_policy::TransferPolicyCap<T>)
Implementation
Function default
Initialize the Transfer Policy in the default scenario: Create and share the TransferPolicy, transfer TransferPolicyCap to the transaction sender.
entry fun defaultT(pub: &sui::package::Publisher, ctx: &mut sui::tx_context::TxContext)
Implementation
Function withdraw
Withdraw some amount of profits from the TransferPolicy. If amount is not specified, all profits are withdrawn.
public fun withdrawT(self: &mut sui::transfer_policy::TransferPolicy<T>, cap: &sui::transfer_policy::TransferPolicyCap<T>, amount: std::option::Option<u64>, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<sui::sui::SUI>
Implementation
Function destroy_and_withdraw
Destroy a TransferPolicyCap. Can be performed by any party as long as they own it.
public fun destroy_and_withdrawT(self: sui::transfer_policy::TransferPolicy<T>, cap: sui::transfer_policy::TransferPolicyCap<T>, ctx: &mut sui::tx_context::TxContext): sui::coin::Coin<sui::sui::SUI>
Implementation
Function confirm_request
Allow a TransferRequest for the type T. The call is protected by the type constraint, as only the publisher of the T can get TransferPolicy<T>.
Note: unless there's a policy for T to allow transfers, Kiosk trades will not be possible.
public fun confirm_requestT(self: &sui::transfer_policy::TransferPolicy<T>, request: sui::transfer_policy::TransferRequest<T>): (sui::object::ID, u64, sui::object::ID)
Implementation
Function add_rule
Add a custom Rule to the TransferPolicy. Once set, TransferRequest must receive a confirmation of the rule executed so the hot potato can be unpacked.
- T: the type to which TransferPolicy
is applied. - Rule: the witness type for the Custom rule
- Config: a custom configuration for the rule
Config requires drop to allow creators to remove any policy at any moment, even if graceful unpacking has not been implemented in a "rule module".
public fun add_ruleT, Rule, Config(_: Rule, policy: &mut sui::transfer_policy::TransferPolicy<T>, cap: &sui::transfer_policy::TransferPolicyCap<T>, cfg: Config)
Implementation
Function get_rule
Get the custom Config for the Rule (can be only one per "Rule" type).
public fun get_ruleT, Rule, Config(_: Rule, policy: &sui::transfer_policy::TransferPolicy<T>): &Config
Implementation
Function add_to_balance
Add some SUI to the balance of a TransferPolicy.
public fun add_to_balanceT, Rule(_: Rule, policy: &mut sui::transfer_policy::TransferPolicy<T>, coin: sui::coin::Coin<sui::sui::SUI>)
Implementation
Function add_receipt
Adds a Receipt to the TransferRequest, unblocking the request and confirming that the policy requirements are satisfied.
public fun add_receiptT, Rule(_: Rule, request: &mut sui::transfer_policy::TransferRequest<T>)
Implementation
Function has_rule
Check whether a custom rule has been added to the TransferPolicy.
public fun has_ruleT, Rule(policy: &sui::transfer_policy::TransferPolicy<T>): bool
Implementation
Function remove_rule
Remove the Rule from the TransferPolicy.
public fun remove_ruleT, Rule, Config(policy: &mut sui::transfer_policy::TransferPolicy<T>, cap: &sui::transfer_policy::TransferPolicyCap<T>)
Implementation
Function uid
Allows reading custom attachments to the TransferPolicy if there are any.
public fun uidT(self: &sui::transfer_policy::TransferPolicy<T>): &sui::object::UID
Implementation
Function uid_mut_as_owner
Get a mutable reference to the self.id to enable custom attachments to the TransferPolicy.
public fun uid_mut_as_ownerT(self: &mut sui::transfer_policy::TransferPolicy<T>, cap: &sui::transfer_policy::TransferPolicyCap<T>): &mut sui::object::UID
Implementation
Function rules
Read the rules field from the TransferPolicy.
public fun rulesT(self: &sui::transfer_policy::TransferPolicy<T>): &sui::vec_set::VecSet<std::type_name::TypeName>
Implementation
Function item
Get the item field of the TransferRequest.
public fun itemT(self: &sui::transfer_policy::TransferRequest<T>): sui::object::ID
Implementation
Function paid
Get the paid field of the TransferRequest.
public fun paidT(self: &sui::transfer_policy::TransferRequest<T>): u64
Implementation
Function from
Get the from field of the TransferRequest.
public fun fromT(self: &sui::transfer_policy::TransferRequest<T>): sui::object::ID