Dapplet config

When you create a dapplet module, one of the important entities is the dapplet config. It provides settings that can be selected by the user in the Dapplets extension. It also allows you to set different parameter values depending on the environment.

For example: you could define the network in which the dapplet will work, configure the monetization of the dapplet, change the url addresses of the server and the overlay part of the dapplet.

The configuration is implemented through the schema.json.

The schema follows the rules defined in http://json-schema.org/. The Dapplet settings UI is generated by react-jsonschema-form.

In the production environment we set up the dapplet config and specify the parameters that will be available in the browser through the extension.

Configuration#

The dapplet config is located in the ./config directory.

In the schema.json file we specify the dapplet settings that will be available to the user.

The scheme consists of parameters:

1{
2 // Schema type: always object
3 "type": "object",
4 // List the names of the required parameters. Other parameters will be optional
5 "required": [
6 "network"
7 ],
8 "properties": {
9 // Property name (ID)
10 "network": {
11 // string or number
12 "type": "string",
13 // Property title for the extension
14 "title": "Target network",
15 /**
16 An optional field visibility parameter. Boolean value.
17 Make the setting hidden for users.
18 It's displayed in the developer and testing mode, not in the public.
19 */
20 "hidden": false,
21 /**
22 An optional field in select format. It sets the dropdown form for the setting.
23 You should list the available values here.
24 */
25 "enum": [
26 "mainnet",
27 "testnet"
28 ]
29 },
30 }
31}

img

img

Inputs for numbers can have a limit on the maximum and minimum value and a step.

1{
2 "type": "object",
3 "required": [
4 "step",
5 "delay"
6 ],
7 "properties": {
8 "step": {
9 "type": "number",
10 "title": "Donation increase step",
11 "maximum": 1,
12 "minimum": 0.05,
13 // The property value must be a multiple of this parameter
14 "multipleOf": 0.05,
15 ...
16 },
17 "delay": {
18 "type": "number",
19 "title": "Time before sending tip (in seconds)",
20 "maximum": 0.1,
21 "minimum": 5,
22 "multipleOf": 0.1,
23 ...
24 },
25 }
26}

img

The default setting values are defined in the default.json.

There are three environments:

  • dev - used when a module is loaded from the development server;
  • test - used when a module is loaded from the Test Dapplet Registry;
  • prod or main - used when a module is loaded from the Production Dapplet Registry.

Describe the fields as an object:

1{
2 // Environment type
3 "main": {
4 // <property name>: <default value>
5 "network": "mainnet",
6 "step": 0.05,
7 "delay": 3
8 },
9 "test": {
10 "network": "mainnet",
11 "step": 0.05,
12 "delay": 2
13 },
14 "dev": {
15 "network": "testnet",
16 "step": 0.1,
17 "delay": 1
18 }
19}

Dapplet#

There is Core.storage API that allows you to work in the dapplet with its config. The most commonly used method is get(key: string). It allows to get current values of the settings.

1...
2async activate(): Promise<void> {
3 const step = await Core.storage.get('step');
4 const delay = await Core.storage.get('delay');
5 this._network = await Core.storage.get('network');
6 ...
7}

There are also set(key: string, value: any), remove(key: string) and clear() methods. They can be used to change a setting, restore a specific setting or all settings to their default values.

Extension#

User interaction with the dapplet config takes place in the User Settings page.

The user can interact with the field by saving the data or reverting it to the default dapplet config data.

video