Chromia
Start Building

15 / 15Module 3: Create the marketplaceAdvanced

Test using Chromia CLI

Every piece of the marketplace is now in place, and you can try it out with Chromia CLI. This command-line tool gives you a set of commands for interacting with the Chromia blockchain. The Chromia CLI documentation covers it in more detail.

Queries for verification

Add two helper queries to the app first. You will use them to retrieve accounts and their balances so you can check the data at each step.

src/rell_marketplace/module.rell
import lib.ft4.assets.{ asset, Unsafe, balance };
src/rell_marketplace/queries.rell
query get_all_accounts(){    return account @* {} ($.to_struct());}query get_all_balances(){    return balance @* {} ($.account.to_struct(), $.to_struct());}

Begin testing

Start the blockchain node with the following command:

Create accounts

shell
chr node start

In a second terminal, run your CLI commands. Begin by querying the accounts to confirm that the dapp account exists.

shell
chr query get_all_accounts

Next, register two users: Alice and Bob.

shell
chr keygen --file .chromia/alice.keypairchr keygen --file .chromia/bob.keypairchr tx --await create_user "<alice pubkey>" --secret .chromia/alice.keypairchr tx --await create_user "<bob pubkey>" --secret .chromia/bob.keypair

The chr tx command submits a transaction that invokes an operation on the blockchain.

To check that minting behaves as expected, query the balances of every account. Alice and Bob should each hold 1000 CRDs.

shell
chr query get_all_balances

The output resembles the following. It lists both new accounts, each with the 1000 CRD balance minted by create_user.

shell
[    [[x"3A0153952DCB30A904B1E3461321892C916B10E51897F32B3352DB65DDDA4562"], [7, 1, 1000L]],     [[x"8EF62426BA321FB23D6163DCFBFC53049E9A078500264F012BC4E4AB5048A859"], [13, 1, 1000L]]]

Buy a mystery card

With the balances confirmed, have Alice purchase a mystery card.

shell
chr tx --await buy_mystery_card --ft-auth --ft-account-id=<first id from get_all_balances query> --secret .chromia/alice.keypair

To confirm the transaction went through, inspect the current nft_cards and look at the balances again.

shell
chr query get_all_nfts

You should see output like this, describing a card NFT and its properties.

shell
[["health": 10, "nft_id": 22, "owner_id": x"<owner id>", "strength": 8]]

Run this query to check the balances

shell
chr query get_all_balances

and you get the following figures for Alice, Bob, and the Economy Account.

ParticipantActionAmountBalance
AlicePurchased a mystery card-100 CRD900 CRD
BobNothing1000 CRD
Economy AccountPayment from Alice+100 CRD100 CRD
shell
[    [[x"3A0153952DCB30A904B1E3461321892C916B10E51897F32B3352DB65DDDA4562"], [7, 1, 900L]],  // Alice    [[x"8EF62426BA321FB23D6163DCFBFC53049E9A078500264F012BC4E4AB5048A859"], [13, 1, 1000L]],  // Bob    [[x"522546AF72636ABA2AFE34E1BF2D7BF2B9C0A483C20F87E145BE32992339962C"], [2, 1, 100L]] // Economy account]

List cards on the marketplace

Alice now puts her card up for sale at 25 CRD. Take the nft_id from the earlier get_all_nfts output, and Alice's FT4 account id from the get_all_balances output.

shell
chr tx --await list_nft "<nft_id from query>" "25L" --secret .chromia/alice.keypair --ft-auth --ft-account-id=<FT4 account from query>

Then call get_cards, supplying arguments that filter and sort the results.

shell
chr query get_cards "amount=10" "card_sorting=PRICE_HIGH" 

The query should return exactly one entry: the card Alice listed.

Buy a card from the marketplace

Bob purchases Alice's card through the buy_card operation.

shell
chr tx --await buy_nft "<nft id>" --secret .chromia/bob.keypair --ft-auth --ft-account-id=<Bobs FT4 account id from get_all_balances>

Afterwards, run the query.

shell
chr query get_all_balances

The resulting balances should match this table

ParticipantActionBalance
AliceBought a mystery card for 100 CRD
Got 25 CRD from Bob
925 CRD
BobPaid 25 CRD for Alice's card975 CRD
Economy AccountCollected a 100 CRD fee from Alice100 CRD

and the query output confirms them.

shell
[    [[x"3A0153952DCB30A904B1E3461321892C916B10E51897F32B3352DB65DDDA4562"], [7, 1, 925L]],  // Alice    [[x"8EF62426BA321FB23D6163DCFBFC53049E9A078500264F012BC4E4AB5048A859"], [13, 1, 975L]],  // Bob    [[x"522546AF72636ABA2AFE34E1BF2D7BF2B9C0A483C20F87E145BE32992339962C"], [2, 1, 100L]]     // Economy account]

That completes the Chromia CLI tests and the course as a whole. Happy coding!

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