Module 0x2::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 0x1::option;
use 0x1::type_name;
use 0x2::balance;
use 0x2::coin;
use 0x2::dynamic_field;
use 0x2::event;
use 0x2::object;
use 0x2::package;
use 0x2::sui;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::vec_set;
Struct TransferRequest
A "Hot Potato" forcing the buyer to get a transfer permission from the item type (T) owner on purchase attempt.
struct TransferRequest<T>
Fields
Resource 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.
struct TransferPolicy<T> has store, key
Fields
Resource TransferPolicyCap
A Capability granting the owner permission to add/remove rules as well as to withdraw and destroy_and_withdraw the TransferPolicy.
struct TransferPolicyCap<T> has store, key
Fields
Struct TransferPolicyCreated
Event that is emitted when a publisher creates a new TransferPolicyCap making the discoverability and tracking the supported types easier.
struct TransferPolicyCreated<T> has copy, drop
Fields
Struct TransferPolicyDestroyed
Event that is emitted when a publisher destroys a TransferPolicyCap. Allows for tracking supported policies.
struct TransferPolicyDestroyed<T> has copy, drop
Fields
Struct RuleKey
Key to store "Rule" configuration for a specific TransferPolicy.
struct RuleKey<T: drop> has copy, drop, store
Fields
Constants
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;
A completed rule is not set in the TransferPolicy.
const EIllegalRule: u64 = 1;
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_request<T>(item: object::ID, paid: u64, from: object::ID): 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 new<T>(pub: &package::Publisher, ctx: &mut tx_context::TxContext): (transfer_policy::TransferPolicy<T>, 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 default<T>(pub: &package::Publisher, ctx: &mut 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 withdraw<T>(self: &mut transfer_policy::TransferPolicy<T>, cap: &transfer_policy::TransferPolicyCap<T>, amount: option::Option<u64>, ctx: &mut tx_context::TxContext): coin::Coin<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_withdraw<T>(self: transfer_policy::TransferPolicy<T>, cap: transfer_policy::TransferPolicyCap<T>, ctx: &mut tx_context::TxContext): coin::Coin<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_request<T>(self: &transfer_policy::TransferPolicy<T>, request: transfer_policy::TransferRequest<T>): (object::ID, u64, 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_rule<T, Rule: drop, Config: drop, store>(_: Rule, policy: &mut transfer_policy::TransferPolicy<T>, cap: &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_rule<T, Rule: drop, Config: drop, store>(_: Rule, policy: &transfer_policy::TransferPolicy<T>): &Config
Implementation
Function add_to_balance
Add some SUI to the balance of a TransferPolicy.
public fun add_to_balance<T, Rule: drop>(_: Rule, policy: &mut transfer_policy::TransferPolicy<T>, coin: coin::Coin<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_receipt<T, Rule: drop>(_: Rule, request: &mut transfer_policy::TransferRequest<T>)
Implementation
Function has_rule
Check whether a custom rule has been added to the TransferPolicy.
public fun has_rule<T, Rule: drop>(policy: &transfer_policy::TransferPolicy<T>): bool
Implementation
Function remove_rule
Remove the Rule from the TransferPolicy.
public fun remove_rule<T, Rule: drop, Config: drop, store>(policy: &mut transfer_policy::TransferPolicy<T>, cap: &transfer_policy::TransferPolicyCap<T>)
Implementation
Function uid
Allows reading custom attachments to the TransferPolicy if there are any.
public fun uid<T>(self: &transfer_policy::TransferPolicy<T>): &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_owner<T>(self: &mut transfer_policy::TransferPolicy<T>, cap: &transfer_policy::TransferPolicyCap<T>): &mut object::UID
Implementation
Function rules
Read the rules field from the TransferPolicy.
public fun rules<T>(self: &transfer_policy::TransferPolicy<T>): &vec_set::VecSet<type_name::TypeName>
Implementation
Function item
Get the item field of the TransferRequest.
public fun item<T>(self: &transfer_policy::TransferRequest<T>): object::ID
Implementation
Function paid
Get the paid field of the TransferRequest.
public fun paid<T>(self: &transfer_policy::TransferRequest<T>): u64
Implementation
Function from
Get the from field of the TransferRequest.
public fun from<T>(self: &transfer_policy::TransferRequest<T>): object::ID