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.
struct module_args { dapp_account_signer: pubkey;}Next, create the account inside the dapp_meta object you declared earlier.
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":
chr keygen --file .chromia/dapp-accountOpen chromia.yaml, add a module argument for the economy account, and set its value to the pubkey.
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.9database: schema: schema_rell_marketplacetest: 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: falseYou 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:
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.