Modeling dApp Structures

Modules are very simple to write. At their core they are an object with a register property, that is a function with the signature async function.

Success

Everything we use inside ARK core is built with modules. To learn more about modules and their structure follow the source code of existing modules within core.

Information

GitHub learning repository has a template project available here . You can create a new module by creating a new GitHub repository and selecting the correct template: learn-ark/dapp-core-module-template.

Package properties

The module properties are set in the package.json file in the root of you core module.

  • name the module name
  • version the module version
  • arkecosystem:
    • core:
      • alias: the module alias
      • dependencies: the module dependencies
      • required: is module required

Service Provider

1import { Container, Contracts, Providers } from "@arkecosystem/core-kernel";
2import { DappManager } from "./manager";
3 
4const DappManagerIdentifier = Symbol.for("DappManager");
5 
6export class ServiceProvider extends Providers.ServiceProvider {
7 public async register(): Promise<void> {
8 this.app.get<Contracts.Kernel.Logger>(Container.Identifiers.LogService).info(`Loading dapp`);
9 
10 this.app.bind<DappManager>(DappManagerIdentifier).to(DappManager).inSingletonScope();
11 }
12 
13 public async boot(): Promise<void> {
14 this.app.get<Contracts.Kernel.Logger>(Container.Identifiers.LogService).info(`Booting dapp`);
15 
16 this.app.get<DappManager>(DappManagerIdentifier).start({});
17 }
18 
19 public async dispose(): Promise<void> {
20 this.app.get<Contracts.Kernel.Logger>(Container.Identifiers.LogService).info(`Disposing dapp`);
21 
22 this.app.get<DappManager>(DappManagerIdentifier).stop();
23 }
24}

Registration

Module needs to be registered in order to be picked up by the core engine. Looking at the source code above each core module must have the register method implemented. This method is called from the application container.

1public async register(): Promise<void> {
2 this.app.get<Contracts.Kernel.Logger>(Container.Identifiers.LogService).info(`Loading dapp`);
3 
4 this.app.bind<DappManager>(DappManagerIdentifier).to(DappManager).inSingletonScope();
5}

Take a look on Service Providers to learn more about it.

Let’s head over to the next section, where we will learn how to write our own dApp in a few minutes, just by following our dApp core module template repository.

Last updated 3 years ago
Edit Page
Share: