Chromia
Start Building

18 / 27Module 1: A Rell backend with FT accountsBeginner

Add modules to your dapp

Now apply this by splitting the code into smaller modules. Start by creating a news_feed folder and adding a module.rell file to it:

src/news_feed/module.rell
module;

In main.rell, add this line to import everything from the new module:

src/main.rell
import news_feed.*;

Create a model.rell file in src/news_feed and move the entity definitions out of the main file into it:

src/news_feed/model.rell
entity user {  mutable name;  key id: byte_array;  key account;}entity follower {  index user;  index follower: user;  key user, follower;}entity post {  timestamp = op_context.last_block_time;  index user;  content: text;}

Move the import of the account entity from ft-library into module.rell. Placing it there makes it available to every file in the module:

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

Relocate the remaining code as follows:

  • auth_handler goes to src/news_feed/auth.rell.
  • post_dto goes to src/news_feed/dto.rell.
  • The queries go to src/news_feed/queries.rell.
  • The operations go to src/news_feed/operations.rell.

You can also put the require_user and format_user utility functions directly in module.rell if you prefer.

Once you have made these changes, module.rell in the news_feed module looks like this:

src/news_feed/module.rell
module;import lib.ft4.accounts.{ account };import lib.ft4.auth;function require_user(id: byte_array) = require(user @? { id }, "User with id %s does not exist".format(id));function format_user(user) = "%s#%s".format(user.name, user.id.to_hex().sub(0, 5));

With the imports set up correctly, main.rell only needs to contain the create_user operation, which helps when testing.

Next, add a test_operations module to the test folder: create a file there and declare it as a module:

src/test/test_operations.rell
module;import lib.ft4.core.accounts. { create_account_with_auth };import lib.ft4.accounts.{ single_sig_auth_descriptor };import ^^.news_feed.{ user };operation create_user(name, pubkey) {    val account = create_account_with_auth(single_sig_auth_descriptor(pubkey, set(["A", "T", "S"])));    create user ( name, pubkey, account );}

Import the module explicitly in the test file. You can use a relative import here:

src/test/news_feed_test.rell
import ^.test_operations.{ create_user };

Your project is now organized into Rell modules. The file structure should look similar to this:

text
rell/src├─ main.rell├─ news_feed│  ├─ auth.rell│  ├─ dto.rell│  ├─ model.rell│  ├─ module.rell│  ├─ operations.rell│  └─ queries.rell├─ registration│  └─ module.rell└─ test   ├─ news_feed_test.rell   ├─ registration_test.rell   └─ test_operations.rell
Written for this site from Chromia’s public course. Code samples are the course’s own.