# Charge a fee for mystery card purchases · Decentralized marketplace with FT4 · Learn Chromia

> The minting lesson introduced operation buy_mystery_card, which mints a random card for the caller. To turn it into a real purchase, you need to define a…

Source: https://chromia.com/learn/marketplace-course/module-assets/buy-mystery-card/

Decentralized marketplace with FT4Lesson 11 of 15

1. [01Decentralized marketplace with FT4](https://chromia.com/learn/marketplace-course/introduction/)
2. [02Set up your project](https://chromia.com/learn/marketplace-course/setup/)

Module 1: Registering accounts and assets

1. [03Overview](https://chromia.com/learn/marketplace-course/module-ft4/)
2. [04Register payment token](https://chromia.com/learn/marketplace-course/module-ft4/register-token/)
3. [05Create user accounts](https://chromia.com/learn/marketplace-course/module-ft4/register-account/)

Module 2: Model NFTs in Rell

1. [06Overview](https://chromia.com/learn/marketplace-course/module-nft/)
2. [07Define the NFT model](https://chromia.com/learn/marketplace-course/module-nft/nft/)
3. [08Mint NFTs](https://chromia.com/learn/marketplace-course/module-nft/mint-nfts/)
4. [09Make card attributes random](https://chromia.com/learn/marketplace-course/module-nft/randomness/)

Module 3: Create the marketplace

1. [10Overview](https://chromia.com/learn/marketplace-course/module-assets/)
2. [11Charge a fee for mystery card purchases](https://chromia.com/learn/marketplace-course/module-assets/buy-mystery-card/)
3. [12Put a card up for sale on the marketplace](https://chromia.com/learn/marketplace-course/module-assets/list-card/)
4. [13Buy a card on the marketplace](https://chromia.com/learn/marketplace-course/module-assets/buy-listed-card/)
5. [14Test our marketplace](https://chromia.com/learn/marketplace-course/module-assets/test-marketplace/)
6. [15Test using Chromia CLI](https://chromia.com/learn/marketplace-course/module-assets/test-cli/)

[Learn](https://chromia.com/learn/)[Decentralized marketplace with FT4](https://chromia.com/learn/marketplace-course/)

11 / 15Module 3: Create the marketplaceAdvanced

# Charge a fee for mystery card purchases

The minting lesson introduced `operation buy_mystery_card`, which mints a random card for the caller. To turn it into a real purchase, you need to define a price and charge the user that amount each time the operation runs.

Each payment moves from the user to a dedicated dapp account that collects all card-sale revenue.

That economy account does not exist yet, so you will create it with FT4. Add a new entity, `dapp_account`, to hold it. This lets your Rell code reach the account directly.

First, define a structure in Rell that references the account.



src/rell\_marketplace/module.rell

```
struct module_args {
    dapp_account_signer: pubkey;
}
```

Next, create the account inside the `dapp_meta` object you declared earlier.

src/rell\_marketplace/module.rell

```
object dapp_meta {
    asset = Unsafe.register_asset("Collector Card", "CRD", 6, chain_context.blockchain_rid, "https://url-to-asset-icon");
    account = create_account_with_auth(single_sig_auth_descriptor(chain_context.args.dapp_account_signer, set(["A", "T"])));
}
```

This code calls `create_account_with_auth` to create the account and passes it a single-signature auth descriptor with the following values:

* The dapp account pubkey, read from the moduleArgs settings
* The flags "A", which allows managing accounts, and "T", which allows token transfers from this account

Now generate a new keypair to act as the dapp's "account owner":

shell

```nolines
chr keygen --file .chromia/dapp-account
```

Open `chromia.yaml`, add a module argument for the economy account, and set its value to the `pubkey`.

chromia.yml

```
blockchains:
  rell_marketplace:
    module: main
    moduleArgs:
      lib.ft4.core.accounts:
        rate_limit:
          max_points: 10
          recovery_time: 5000
          points_at_account_creation: 2
        lib.ft4.core.admin:
          admin_pubkey: x"<dapp_account_pubkey>" # Replace with previously generated public key here.
        rell_marketplace:
          dapp_account_signer: x"<dapp_account_pubkey>" # Replace with previously generated public key here.
compile:
  rellVersion: 0.14.9
database:
  schema: schema_rell_marketplace
test:
  modules:
    - rell_marketplace_test
  moduleArgs:
    rell_marketplace:
      dapp_account_signer: x"<dapp_account_pubkey>" # Replace with previously generated public key here.
    lib.ft4.core.admin:
        admin_pubkey: x"<dapp_account_pubkey>" # Replace with previously generated public key here.
libs:
  ft4:
    registry: https://gitlab.com/chromaway/ft4-lib.git
    path: rell/src/lib/ft4
    tagOrBranch: v1.2.0r
    rid: x"E96915FD7C9A5DD4E183D86F4A5B4BB5083A7CC1EDEB1465EDFFC926E67C93F6"
    insecure: false
  iccf:
    registry: https://gitlab.com/chromaway/core/directory-chain
    path: src/lib/iccf
    tagOrBranch: 1.87.0
    rid: x"9C359787B75927733034EA1CEE74EEC8829D2907E4FC94790B5E9ABE4396575D"
    insecure: false
```

You now have an account that can receive charges. When a user buys a card, for example, the amount moves from their account to this one.

To apply this, add a transfer to the mystery card purchase. The transfer takes these parameters:

* From account
* To account
* Asset
* Amount

With the transfer in place, `buy_mystery_card` looks like this:

src/rell\_marketplace/marketplace.rell

```
operation buy_mystery_card() {
    val account = auth.authenticate();
    Unsafe.transfer(account, dapp_meta.account, dapp_meta.asset, 100);
    mint_card(account);
}
```

The card price is fixed at 100 CRD. Each call moves 100 CRD from the authenticated account to your `dapp_account`.

Written for this site from Chromia’s public course. Code samples are the course’s own.

[← Previous**Module 3: Create the marketplace**](https://chromia.com/learn/marketplace-course/module-assets/)[Next →**Put a card up for sale on the marketplace**](https://chromia.com/learn/marketplace-course/module-assets/list-card/)
