Skip to main content

Loyalty Tokens

Using the Sui Closed-Loop Token standard, you can create tokens that are valid only for a specific service, like an airline that wants to grant tokens to frequent flyers to purchase tickets or upgrades.

In this example, the Admin sends LOYALTY tokens to the users of your service as a reward for their contiunued buisness. The example creates a GiftShop where holders can spend LOYALTY tokens to buy Gifts.

The loyalty.move source file contains the examples::loyalty module code that creates the loyalty token. The module includes the One-Time Witness (OTW) that creates the coin (with the same name as the module, LOYALTY), possesses only the drop ability, and has no fields.

/// The OTW for the Token / Coin.
public struct LOYALTY has drop {}

The init function of the module uses the LOYALTY OTW to create the token. It makes use of the OTW LOYALTY type defined previously in its call to create_currency. The function also defines a policy, sending both the policy capability and treasury capability to the address associated with the publish event. The holder of these transferable capabilities can mint new LOYALTY tokens and modify their policies.

info

init functions run only during the package publish event.

#[allow(deprecated_usage)]
fun init(otw: LOYALTY, ctx: &mut TxContext) {
let (treasury_cap, coin_metadata) = coin::create_currency(
otw,
0, // no decimals
b"LOY", // symbol
b"Loyalty Token", // name
b"Token for Loyalty", // description
option::none(), // url
ctx,
);

let (mut policy, policy_cap) = token::new_policy(&treasury_cap, ctx);

token::add_rule_for_action<LOYALTY, GiftShop>(
&mut policy,
&policy_cap,
token::spend_action(),
ctx,
);

token::share_policy(policy);

transfer::public_freeze_object(coin_metadata);
transfer::public_transfer(policy_cap, tx_context::sender(ctx));
transfer::public_transfer(treasury_cap, tx_context::sender(ctx));
}

The LOYALTY minting function is called reward_user. The holder of the TreasuryCap can call this function to mint new loyalty tokens. The function uses the token::mint function to create the token and token::transfer to send it to the intended recipient.

public fun reward_user(
cap: &mut TreasuryCap<LOYALTY>,
amount: u64,
recipient: address,
ctx: &mut TxContext,
) {
let token = token::mint(cap, amount, ctx);
let req = token::transfer(token, recipient, ctx);

token::confirm_with_treasury_cap(cap, req, ctx);
}

Lastly, a buy_a_gift function handles the redemption of LOYALTY tokens for Gift types. The function ensures the gift price matches the number of loyalty tokens spent, then uses the token::spend function to handle the treasury bookkeeping.

public fun buy_a_gift(token: Token<LOYALTY>, ctx: &mut TxContext): (Gift, ActionRequest<LOYALTY>) {
assert!(token::value(&token) == GIFT_PRICE, EIncorrectAmount);

let gift = Gift { id: object::new(ctx) };
let mut req = token::spend(token, ctx);

token::add_approval(GiftShop {}, &mut req, ctx);

(gift, req)
}

View the full example on GitHub.