Skip to content

Commit

Permalink
Merge branch 'develop' into devx/edit-iota-101-remaining-articles
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-tortora authored Nov 4, 2024
2 parents 80a7323 + 3a1fdae commit 64a65a7
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 78 deletions.
74 changes: 47 additions & 27 deletions docs/content/developer/iota-101/create-coin/create-coin.mdx
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
---
title: Create Coins and Tokens
description: Learn how to create coins and tokens on the IOTA blockchain using Move.
---
import Quiz from '@site/src/components/Quiz';
import questions from '/json/developer/iota-101/create-coin/create-coin.json';

# Creating Coins and Tokens

Coins and tokens on IOTA are similar. In practice, the terms are used interchangeably, but there are some differences in their implementation. You can learn about these differences in the respective standard documentation, [Closed-Loop Token](../../standards/closed-loop-token.mdx) and [Coin](../../standards/coin.mdx).
Coins and tokens in IOTA are similar concepts, often used interchangeably,
but there are subtle differences in their implementation.
To understand these differences, refer to the standard documentation for [Closed-Loop Token](../../standards/closed-loop-token.mdx) and [Coin](../../standards/coin.mdx).

Publishing a coin on IOTA is nearly as straightforward as publishing a new type. The main difference is the requirement of a [one-time witness](../move-overview/one-time-witness.mdx) when creating a coin.
## Publishing a Coin

Publishing a coin on IOTA is almost as straightforward as publishing a new type.
The key difference is the requirement of a [one-time witness](../move-overview/one-time-witness.mdx) when creating a coin.

```move file=<rootDir>/examples/move/coin/sources/my_coin.move
```

The `Coin<T>` is a generic implementation of a coin on IOTA. Access to the `TreasuryCap` provides control over the minting and burning of coins. Further transactions can be sent directly to the `iota::coin::Coin` with `TreasuryCap` object as authorization.
The [`Coin<T>`](../../../references/framework/iota-framework/coin.mdx) is a generic coin implementation in IOTA.
By accessing the [`TreasuryCap`](../../../references/framework/iota-framework/coin.mdx#resource-treasurycap),
you gain control over minting and burning coins.
You can send further transactions directly to `iota::coin::Coin` using the `TreasuryCap` object for authorization.

Extending the example further, add a `mint` function to the module. Use the `mint` function of the `Coin` module to create (mint) a coin and then transfer it to an address.
## Extending the Coin Module

```move file=<rootDir>/examples/move/coin/sources/my_coin.move#L23-L32
```
To extend the coin module, add a `mint` function.
This function utilizes the [`mint`](../../../references/framework/iota-framework/coin.mdx#function-mint) method from the `Coin` module
to create a coin and transfer it to a specified address.

### IOTA CLI
## IOTA CLI

If you published the previous example to a IOTA network, you can use the `iota client call` command to mint coins and deliver them to the address you provide. See [IOTA CLI](../../../references/cli.mdx) for more information on the command line interface.
### Minting Coins

After publishing the coin module to the IOTA network,
you can mint coins and send them to an address using the `iota client call` command.
For more details on the command-line interface, see [IOTA CLI](../../../references/cli.mdx).

```shell
iota client call --function mint --module mycoin --package <PACKAGE-ID> --args <TREASURY-CAP-ID> <COIN-AMOUNT> <RECIPIENT-ADDRESS>
```

If the call is successful your console displays the result, which includes a **Balance Changes** section with the following information included:
Upon successful execution, the console displays output including a **Balance Changes** section:

```shell
...
Expand All @@ -39,34 +53,40 @@ Amount: <COIN-AMOUNT>
...
```

## DenyList

See [`DenyList`](./regulated.mdx#denylist).
## Implementing a Deny List

## Create regulated coin
If you need to restrict specific addresses from accessing your coin, consider implementing a [`DenyList`](./regulated.mdx#managing-the-deny-list).

If you need the ability to deny specific addresses from having access to your coin, you can use the `create_regulated_currency` function (instead of `create_currency`) to create it.
## Creating a Regulated Coin

Behind the scenes, `create_regulated_currency` uses the `create_currency` function to create the coin, but also produces a `DenyCap` object that allows its bearer to control access to the coin's deny list in a `DenyList` object. Consequently, the way to create a coin using `create_regulated_currency` is similar to the previous example, with the addition of a transfer of the `DenyCap` object to the module publisher.
To deny specific addresses from holding your coin,
use the [`create_regulated_currency`](../../../references/framework/iota-framework/coin.mdx#function-create_regulated_currency_v1) function instead of [`create_currency`](../../../references/framework/iota-framework/coin.mdx#function-create_currency).

## Create tokens
Internally, `create_regulated_currency` calls `create_currency` to create the coin
and also produces a [`DenyCap`](../../../references/framework/iota-framework/coin.mdx#resource-denycapv1) object.
This object allows you to manage the deny list in a `DenyList` object.
The process is similar to the previous example but includes transferring the `DenyCap` object to the module publisher.

Tokens reuse the `TreasuryCap` defined in the `iota::coin` module and therefore have the same initialization process. The `coin::create_currency` function guarantees the uniqueness of the `TreasuryCap` and forces the creation of a `CoinMetadata` object.
## Creating Tokens

Coin-like functions perform the minting and burning of tokens. Both require the `TreasuryCap`:
Tokens reuse the [`TreasuryCap`](../../../references/framework/iota-framework/coin.mdx#resource-treasurycap) defined in the `iota::coin` module
and follow the same initialization process.
The `coin::create_currency` function ensures the uniqueness of the `TreasuryCap` and enforces the creation of a `CoinMetadata` object.

- `token::mint` - mint a token
- `token::burn` - burn a token
You can mint and burn tokens using functions similar to those for coins, both requiring the `TreasuryCap`:

See [Closed-Loop Token](../../standards/closed-loop-token.mdx) standard for complete details of working with tokens.
- [`token::mint`](../../../references/framework/iota-framework/token.mdx#function-mint) — Mint a token.
- [`token::burn`](../../../references/framework/iota-framework/token.mdx#function-burn) — Burn a token.

## Examples
For complete details on working with tokens, refer to the [Closed-Loop Token](../../standards/closed-loop-token.mdx) standard.

See the following topics for examples of some common use cases for coin and token creation.
## Additional Examples

- [Regulated Coin and Deny List](regulated.mdx): Create a regulated coin and add or remove names from the deny list.
- [Loyalty Token](loyalty.mdx): Create a token to reward user loyalty.
- [In-Game Token](in-game-token.mdx): Create tokens that can be used only within a mobile game.
Explore these topics for practical examples of coin and token creation:

- [Migrate to CoinManager](migrate-to-coin-manager.mdx): Learn about IOTA's unique [`CoinManager`](../../../references/framework/iota-framework/coin_manager.mdx), and how it simplify managing your [Coins](../../../references/framework/iota-framework/coin.mdx).
- [Regulated Coin and Deny List](regulated.mdx): Learn how to create a regulated coin and manage the deny list.
- [Loyalty Token](loyalty.mdx): Discover how to create a token to reward user loyalty.
- [In-Game Token](in-game-token.mdx): Understand how to create tokens usable within a mobile game.

<Quiz questions={questions} />
52 changes: 38 additions & 14 deletions docs/content/developer/iota-101/create-coin/in-game-token.mdx
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
---
title: In-Game Currency
description: Learn how to create in-game currency on IOTA using the Closed-Loop Token standard.
---
import Quiz from '@site/src/components/Quiz';
import questions from '/json/developer/iota-101/create-coin/in-game-token.json';

# Creating In-Game Currency

Using the IOTA [Closed-Loop Token](../../standards/closed-loop-token.mdx) standard, you can create in-game currency (such as gems or diamonds in mobile games) that you can grant to players for their actions or make available to purchase. You mint the tokens on IOTA, but players can only use the tokens within the economy of the game itself. These types of tokens are usually not transferrable and you would typically mint them in predefined amounts to maintain scarcity and game balance.
You can use the IOTA [Closed-Loop Token](../../standards/closed-loop-token.mdx) standard
to develop in-game currencies like gems or diamonds commonly found in mobile games.
These tokens can be awarded to players for their actions or made available for purchase.
While minted on the IOTA network, players can only use these tokens within the game's ecosystem.
Typically, such tokens are non-transferable and are minted in predefined quantities to maintain scarcity and balance within the game.

The following example creates an in-game currency called a GEM, which represents a certain number of IOTA. In the example, the user can buy fungible GEMs using IOTA, which can then be used as currency within the game. Use the code comments to follow the logic of the example.
## Setting Up the GEM Currency

## Example
In the following example creates an in-game currency called `GEM`, representing a specific amount of IOTA.
Players can purchase fungible `GEMs` using IOTA, which they can then spend within the game.

The IOTA repo hosts a basic example of creating in-game currency. The Move modules that create the economy of the example are in the gems.move source file.
### Example Overview

### Module examples::sword
The IOTA repository includes a [basic example of creating an in-game currency]https://github.com/iotaledger/iota/tree/develop/examples/move/token.
The Move modules responsible for establishing the game's economy are located in the [`gems.move`](https://github.com/iotaledger/iota/blob/develop/examples/move/token/sources/gems.move) source file.

The `examples::sword` module creates one of the objects, a `sword`, that has in-game value. The module assigns a value in GEMs (the other valuable in-game item) to the sword. The module also provides the logic for trading GEMs to receive a sword.
### The `examples::sword` Module

The [`examples::sword` module](https://github.com/iotaledger/iota/blob/develop/examples/move/token/sources/gems.move#L8) defines an in-game object, a `sword`,
which holds value within the game.
This module assigns a `GEM` value to the sword and includes the logic for trading `GEMs` to acquire a sword.

```move file=<rootDir>/examples/move/token/sources/gems.move#L8-L32
```

### Module examples::gem
### The `examples::gem` Module

The [`examples::gem` module](https://github.com/iotaledger/iota/blob/develop/examples/move/token/sources/gems.move#L36) is responsible
for creating the `GEM` in-game currency.
Players spend IOTA to purchase `GEMs`, which they can trade for swords or other in-game items.
The module defines three tiers of `GEM` packages—small, medium, and large—each representing different in-game values.
Constants within the module specify both the value and the quantity of GEMs in each package.

```move file=<rootDir>/examples/move/token/sources/gems.move#L73-L69
```

The `examples::gem` module creates the in-game currency, GEMs. Users spend IOTA to purchase GEMs, which can then be traded for swords. The module defines three groups of GEMs (small, medium, and large), with each group representing a different in-game value. Constants hold both the value of each package and the actual number of GEMs the groups contain.
#### Initializing the GEM Currency

The module's `init` function uses `coin::create_currency` to create the GEM. The `init` function, which runs only the one time when the module publishes, also sets the policies for the in-game currency, freezes the metadata for the coin, and transfers the policy capability to the publisher of the package.
The `init` function in the module uses `coin::create_currency` to create the GEM currency. This function runs only once upon module publication. It sets the policies for the in-game currency, freezes the coin's metadata, and transfers the policy capability to the package publisher.

```move file=<rootDir>/examples/move/token/sources/gems.move#L76-L100
```

The module handles the purchase of GEMs with the `buy_gems` function.
#### Purchasing GEMs

The module handles the purchase of `GEMs` through the `buy_gems` function.

```move file=<rootDir>/examples/move/token/sources/gems.move#L104-L125
```

## Viewing the Complete Module Code

For a comprehensive understanding, you can view the complete code of the `gems.move` module below.

<details>
<summary>
Toggle complete module code
</summary>
<summary>Click to expand the full module code</summary>

```move file=<rootDir>/examples/move/token/sources/gems.move
```
Expand Down
56 changes: 42 additions & 14 deletions docs/content/developer/iota-101/create-coin/loyalty.mdx
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} />
Loading

0 comments on commit 64a65a7

Please sign in to comment.