# Basic wallet operations

Source: https://docs.formance.com/modules/wallets/basic-operations

Stack: v3.2

In this section, you'll use Wallets to model basic RPG game mechanics.

You'll manage the wallet of Eryldor, an elf mage living through countless adventures!

## An introduction to Wallets

At its core, a **wallet** is a container of assets related to one entity. It can be used to store and manage any kind of assets, from money to items, and even more complex objects.

In the context of Cones of Dunshire, a wallet is used to store the money, currencies, and points of a character.

A wallet is composed of **balances**. A balance is a mechanism to logically separate assets according to nature, usage, lifecycle or any other criteria. A balance can be seen as a sub-wallet, and can be used to store and manage a specific set of assets. A balance can expire. When a balance expires, its assets cannot be used anymore.

In Cones of Dunshire, Eryldor has a wallet with two balances: one for coins and experience points, and another for badges, which are earned in tournaments and expire when the tournament season ends.

Here is the structure of Eryldor's wallet.

![Eryldor's wallet structure](https://docs.formance.com/assets/images/wallets/eryldor-wallet.png)

## Creating Eryldor's wallet

Before starting, make sure you have followed the [prerequisites](https://docs.formance.com/getting-started/quickstart) and that you're logged in with the `fctl login` command.

First you'll create Eryldor's wallet. You'll use the `fctl wallet create` command to create a wallet named `eryldor`.

curl

```bash
curl -X POST $FORMANCE_API_URL/api/wallets/wallets \
  -H "Content-Type: application/json" \
  -d '{
    "name": "eryldor"
  }'
```

fctl

```bash
fctl wallets create eryldor
```

You'll get a response like this:

```
You are about to create a wallet.
Do you want to continue [Y/n]: y
 SUCCESS  Wallet created successfully with ID: 9d21fd84-xxxx-yyyy-zzzz-250aaa73b374
```

Wallets come with a default balance named `main`. You can use it to store and manage assets without creating additional balances. Here, you'll use the `main` balance to store and manage the coins and experience points of Eryldor.

### Create the `season-2024` balance

Now you'll create a balance named `season-2024` to store the badges of Eryldor. This balance will expire at the end of the tournament season 2024. Here it will expire on January 1st, 2025.

Feel free to adapt the expiration date to your needs.

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/wallets/wallets/<WALLET_ID>/balances" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "season-2024",
    "expiresAt": "2025-01-01T00:00:00Z"
  }'
```

fctl

```bash
fctl wallets balances create season-2024 --name eryldor --expires-at "2025-01-01T00:00:00Z"
```

You'll get a response like this:

```
 SUCCESS  Balance created successfully with name: season-2024
```

### Inspecting Eryldor's wallet

You can inspect the wallet in the Formance Console.

First open the console.

```bash
fctl ui
```

Then navigate to the Wallets section and select the `eryldor` wallet. You'll see the `main` balance and the `season-2024` balance as shown in the screenshot below.

![Eryldor's wallet in the Formance Console](https://docs.formance.com/assets/images/wallets/wallet-console.png)

Modeling with balances

A common use case for balances is to model vouchers, coupons, or any kind of assets that have a limited lifecycle. For example, you can create a balance for each voucher type, and set the expiration date to the end of the voucher validity period.

## Crediting Eryldor with coins and experience points

After tremendous adventures, Eryldor managed to complete their first quest! As a well-deserved reward, they earned 10 COIN and 150 experience points.

You'll use the `fctl wallets credit` command to credit Eryldor's wallet with these assets.

When you credit a wallet, you must specify where the funds come from. Here, you'll specify that the funds come from the account `world`. `world` is a special account that represents the external world, and is used to introduce or remove assets from the system.

Note that you didn't specify a balance. When you don't specify a balance, the `main` balance is used by default as it is the primary balance of the wallet.

**Add the coins**

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/wallets/wallets/<WALLET_ID>/credit" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": {
      "asset": "COIN",
      "amount": 10
    },
    "sources": [
      {
        "type": "account",
        "identifier": "world"
      }
    ]
  }'
```

fctl

```bash
fctl wallets credit 10 COIN --name eryldor --source account=world
```

**Add the experience points**

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/wallets/wallets/<WALLET_ID>/credit" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": {
      "asset": "XP",
      "amount": 150
    },
    "sources": [
      {
        "type": "account",
        "identifier": "world"
      }
    ]
  }'
```

fctl

```bash
fctl wallets credit 150 XP --name eryldor --source account=world
```

The \`source\` parameter

If you specify a source different from `world`, you'll get an `INSUFFICIENT_FUND` error. This is because you can only credit a wallet with assets coming from either the external world or another wallet.

### Inspecting Eryldor's wallet

Let's inspect Eryldor's wallet in the Formance Console.

You'll see that the `main` balance contains 10 COIN and 150 XP, as shown in the screenshot below.

![Eryldor's wallet after crediting](https://docs.formance.com/assets/images/wallets/wallet-console-credited.png)

## Debiting Eryldor's wallet

Eryldor is now ready to buy a new spellbook! They found a rare spellbook for 5 COIN! It is a very good deal, so they decide to buy it.

You'll use the `fctl wallets debit` command to debit Eryldor's wallet with 5 COIN.

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/wallets/wallets/<WALLET_ID>/debit" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": {
      "asset": "COIN",
      "amount": 5
    },
    "destination": {
      "type": "account",
      "identifier": "world"
    }
  }'
```

fctl

```bash
fctl wallets debit 5 COIN --name eryldor --destination account=world
```

You'll get a response like this:

```
 SUCCESS  Wallet debited successfully!
```

As we did for the credit, you must specify the destination of the funds. Here, you specify that the funds go to the account `world`. `world` being a special account that represents the external world, it can also be used to remove assets from the system.

### Inspecting Eryldor's wallet

Let's inspect Eryldor's wallet in the Formance Console.

You'll see that the `main` balance contains 5 COIN and 150 XP, as shown in the screenshot below.

![Eryldor's wallet after debiting](https://docs.formance.com/assets/images/wallets/wallet-console-debited.png)

## Earning badges

Eryldor is a very skilled mage and they are participating in the tournament season 2024. Thanks to their new spellbook, they managed to win the first tournament and earned a badge! Badges are earned in tournaments and expire at the end of the tournament season.

You'll use the `fctl wallets credit` command to credit Eryldor's wallet with a badge. It's a special asset that you'll represent with the `BADGE` currency. The process is the same as for the coins and experience points, with the difference that you'll specify the `season-2024` balance.

curl

```bash
curl -X POST "$FORMANCE_API_URL/api/wallets/wallets/<WALLET_ID>/credit" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": {
      "asset": "BADGE",
      "amount": 1
    },
    "balance": "season-2024",
    "sources": [
      {
        "type": "account",
        "identifier": "world"
      }
    ]
  }'
```

fctl

```bash
fctl wallets credit 1 BADGE --name eryldor --balance season-2024 --source account=world
```

You'll get a response like this:

```
 SUCCESS  Wallet credited successfully!
```

### Inspecting Eryldor's wallet

Let's inspect Eryldor's wallet in the Formance Console.

You'll see that the `season-2024` balance contains 1 BADGE, as shown in the screenshot below.

![Eryldor's wallet with badges](https://docs.formance.com/assets/images/wallets/wallet-console-badges.png)

## Next steps

In this section, you've learned how to create a wallet, create balances, credit and debit a wallet, and credit a balance. You've also learned how to inspect a wallet in the Formance Console.

In the next session, you'll learn how to use the Wallets to model a hold, a mechanism to freeze assets for a specific purpose, such as a deposit or a reservation.
