-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into devx/edit-iota-101-remaining-articles
- Loading branch information
Showing
5 changed files
with
166 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 38 additions & 14 deletions
52
docs/content/developer/iota-101/create-coin/in-game-token.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,77 @@ | ||
--- | ||
title: Loyalty Tokens | ||
description: Learn how to create loyalty tokens on IOTA for use in digital services. | ||
--- | ||
import Quiz from '@site/src/components/Quiz'; | ||
import questions from '/json/developer/iota-101/create-coin/loyalty.json'; | ||
|
||
|
||
Using the IOTA [Closed-Loop Token](../../standards/closed-loop-token.mdx) 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. | ||
# Creating Loyalty Token | ||
|
||
The following example demonstrates the creation of a loyalty token that bearers can use to make purchases in a digital gift shop. | ||
You can use the IOTA [Closed-Loop Token](../../standards/closed-loop-token.mdx) standard | ||
to create [tokens](../../../references/framework/iota-framework/token.mdx) that are valid only within a specific service. | ||
For example, an airline might grant tokens to frequent flyers that they can use to purchase tickets or upgrades. | ||
|
||
## Example | ||
In this guide, you'll learn how to create a loyalty token that users can use to make purchases in a digital gift shop. | ||
|
||
The Loyalty Token example illustrates a loyalty token that is created with the Closed Loop Token standard. If you were to implement this example, the Admin would send `LOYALTY` tokens to the users of your service as a reward for their loyalty. The example creates a `GiftShop` where holders can spend `LOYALTY` tokens to buy `Gift`s. | ||
## Overview | ||
|
||
### examples::loyalty | ||
The following example demonstrates how to create a loyalty token using the Closed-Loop Token standard. | ||
As the administrator, you would send `LOYALTY` tokens to your service's users as a reward for their loyalty. | ||
The example includes a `GiftShop` where holders can spend `LOYALTY` tokens to buy `Gift` items. | ||
|
||
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. These are the characteristics of a OTW, which ensures the `LOYALTY` type has a single instance. | ||
## Module: `examples::loyalty` | ||
|
||
The `examples::loyalty` module, found in the `loyalty.move` source file, contains the code to create the loyalty token. | ||
The module defines a [one-time witness (OTW)](../move-overview/one-time-witness.mdx) | ||
that creates the coin named `LOYALTY`. | ||
This coin possesses only the `drop` ability and has no fields. | ||
These characteristics ensure the `LOYALTY` type has a single instance. | ||
|
||
```move file=<rootDir>/examples/move/token/sources/loyalty.move#L22-L23 | ||
``` | ||
|
||
The `init` function of the module uses the `LOYALTY` OTW to create the token. All `init` functions run one time only at the package publish event. The initializer function 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 trasury capability to the address associated with the publish event. The holder of these transferrable capabilities can mint new `LOYALTY` tokens and modify their policies. | ||
### Initialization Function | ||
|
||
The module's [`init` function](../move-overview/init.mdx) uses the `LOYALTY` OTW to create the token. | ||
Remember that all `init` functions run only once at the package publish event. | ||
The initializer function calls [`create_currency`](../../../references/framework/iota-framework/coin.mdx#function-create_currency) | ||
using the `LOYALTY` type defined earlier. | ||
It also sets up a policy by sending both the [policy capability](../../../references/framework/iota-framework/token.mdx#resource-tokenpolicycap) | ||
and the [treasury capability](../../../references/framework/iota-framework/coin.mdx#resource-treasurycap) to the address associated with the publish event. | ||
The holder of these transferable capabilities can mint new `LOYALTY` tokens and modify their policies. | ||
|
||
```move file=<rootDir>/examples/move/token/sources/loyalty.move#L37-L63 | ||
``` | ||
|
||
The `LOYALTY` minting function is called `reward_user`. As mentioned previously, the holder of the `TreasuryCap` can call this function to mint new loyalty tokens and send them to the desired address. The function uses the `token::mint` function to create the token and `token::transfer` to send it to the intended recipient. | ||
### Minting Function: `reward_user` | ||
|
||
The `reward_user` function allows the holder of the `TreasuryCap` | ||
to mint new loyalty tokens and send them to specified addresses. | ||
It uses the [`token::mint`](../../../references/framework/iota-framework/token.mdx#function-mint) function | ||
to create the tokens and [`token::transfer`](../../../references/framework/iota-framework/token.mdx#function-transfer) to deliver them to the intended recipients. | ||
|
||
```move file=<rootDir>/examples/move/token/sources/loyalty.move#L71-L81 | ||
``` | ||
|
||
Finally, the example includes a `buy_a_gift` function to handle 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. | ||
#### Redeeming Tokens: `buy_a_gift` | ||
|
||
Finally, the module includes a `buy_a_gift` function to handle the redemption of `LOYALTY` tokens for `Gift` items. | ||
This function ensures that the gift's price matches the number of loyalty tokens spent. | ||
It uses the [`token::spend`](../../../references/framework/iota-framework/token.mdx#function-spend) function to manage the treasury bookkeeping. | ||
|
||
```move file=<rootDir>/examples/move/token/sources/loyalty.move#L85-L100 | ||
``` | ||
|
||
## Full Source Code | ||
|
||
For a complete view of the module, you can review the full source code below. | ||
|
||
<details> | ||
<summary> | ||
Toggle complete source code | ||
</summary> | ||
<summary>Click to view the complete source code</summary> | ||
|
||
```move file=<rootDir>/examples/move/token/sources/loyalty.move | ||
``` | ||
|
||
</details> | ||
|
||
<Quiz questions={questions} /> | ||
<Quiz questions={questions} /> |
Oops, something went wrong.