Skip to main content

Incorporate modules in the dapp

Let's put this into practice by moving code into smaller modules. Start by creating a folder named Tic_tac_toe_Rell/src/tic_tac_toe and adding a module.rell file inside it:

src/news_feed/module.rell
module;

Next, create a file named src/tic_tac_toe/entities.rell and move the entity definitions into this file.

src/main/entities.rell
entity game {
created_by: ft4_account;
created_at: timestamp = get_last_block_time();
mutable player2_account_rowid: rowid = rowid(0);
mutable last_step_by: rowid;
mutable last_step_at: timestamp = get_last_block_time();
mutable winner: rowid = rowid(0);
mutable status: game_status = game_status.created;
mutable slot_1: slot_states = slot_states.free;
mutable slot_2: slot_states = slot_states.free;
mutable slot_3: slot_states = slot_states.free;
mutable slot_4: slot_states = slot_states.free;
mutable slot_5: slot_states = slot_states.free;
mutable slot_6: slot_states = slot_states.free;
mutable slot_7: slot_states = slot_states.free;
mutable slot_8: slot_states = slot_states.free;
mutable slot_9: slot_states = slot_states.free;
}

Also, move the import statement for the account entity from the ft4-library to the module.rell file. Adding it to the module.rell file makes sense as all files in the module can access it.

src/news_feed/module.rell
module;
import lib.ft4.accounts.{ ft4_account: account };
import ft4_auth: lib.ft4.auth;

Now, create a new module called module in the src/test/tic_tac_toe folder by creating a file there and declaring it as a module:

src/test/tic_tac_toe/module.rell
@test module;

import tic_tac_toe.{ game, game_status, slot_states };
import tic_tac_toe.external.{ create_game, get_games_by_status, join_game, make_move, get_game_by_id };
import lib.ft4.accounts.{ ft4_account: account, auth_descriptor };
import lib.ft4.core.accounts.strategies.open.{ ras_open };
import lib.ft4.test.utils. { create_auth_descriptor, ft_auth_operation_for };
import lib.ft4.external.accounts.strategies.{ register_account };

In your chromia.yml file, you can toggle between using the main module and the development module as the entry point for your dapp by altering the module: tag in the blockchains definition:

chromia.yml
blockchains:
tic_tac_toe:
module: main
compile:
rellVersion: 0.13.5
database:
schema: schema_tic_tac_toe
test:
modules:
- test.tic_tac_toe
libs:
ft4:
registry: https://gitlab.com/chromaway/ft4-lib.git
path: rell/src/lib/ft4
tagOrBranch: v1.0.0r
rid: x"FA487D75E63B6B58381F8D71E0700E69BEDEAD3A57D1E6C1A9ABB149FAC9E65F"
insecure: false

You have now learned to use Rell modules to structure your project effectively. Your file structure should resemble the following:

├── main.rell
├── tic_tac_toe
│ ├── auth.rell
│ ├── entities.rell
│ ├── enums.rell
│ ├── functions.rell
│ ├── mappers.rell
│ ├── module.rell
│ ├── utils.rell
│ ├── require.rell
│ └── external
│ ├── module.rell
│ ├── operations.rell
│ ├── queries.rell
└── test/tic_tac_toe
├── create_account_test.rell
├── create_full_game.rell
├── create_game_test.rell
├── helpers.rell
├── join_game_test.rell
├── make_move_test.rell
└── module.rell