Search
⌃K

User Registry

This example contains the necessary documentation to write, deploy and interact with a small user registry in EXM.

Goal

By the end of this article, you will be able to deploy a user registry inside EXM. If done right, this article will teach you the basic principles of dApps built on EXM (Also known as EXM Functions).

Requirements

  • Account
  • API Key
For more information about API keys, click here.

Writing Handler File

Knowledge about function standards might be useful for this article.
In order to write our dApp, an entry point method is needed. This method must be named handle and contains two parameters: state and action (For more information about actions, click here.)
This dApp will be located in a file we will call index.js for the purposes of the article, but the file name does not have any impact on the way the code is deployed or interacted with.
index.js:
export async function handle(state, action) {
const { username, name, lastName } = action.input;
state.users.push({ username, fullName: `${name} ${lastName}` });
return { state };
}
What's happening in the example above?
  • We are getting the data sent the write operation located in action.input , in this case, username, name and lastName .
  • We are pushing an object to an array of object containing our users. This is state.users .
  • We are returning the modified state (state) variable with the new user given the write operation that took place.
Note how state.users is assumed to not be undefined and an array, and state itself is assumed to be an object. This is because, we will make sure they comply with our code when we deploy with an initial state. The initial state makes sure our EXM dApp is backed by predefined content that we can use or rely on from the very first write operation.

Deploying

Deployment documentation might be useful for this section.
After we have written our index.js file following the example above, we are ready to deploy.
$ $ exm function:deploy --src index.js --init-state '{"users": []}' --token MY_EXM_TOKEN
Notice how we are using the flag --init-state which allows us to provide the initial state for the EXM function. In this case, we are providing the following initial state in JSON format:
{
"users": []
}
Which would be used from inside the handle(state, action) function as follows: state.users .
After running the deployment command shown above, you should see a similar output to the one in the following picture:
With your
Such output will contain the Id of your EXM Function

Interacting With Function

Write Operations might be useful for this section.
After having deployed your decentralized application (also known as EXM Function), you will be able to immediately interact with it, no waiting or confirmation times are needed to start using your dApp.
Interactions are called Write Operations, this is because they write to the existent state by adding, modifying or removing data from it.
In order to perform a write operation, let's follow the example:
Given that our action.input in the previously deployed function is expecting username, name, and lastName, this means, our input for the write operation should contain those 3. Our example input will look:
{
"username": "superman",
"name": "Clark",
"lastName": "Kent"
}
Now that we have our input, we just need to send it to our dApp by running the following command:
exm function:write $FUNCTION_ID --input '{"username": "superman", "name": "Clark", "lastName": "Kent"}' --show-output --token MY_EXM_TOKEN
Note how the flag --show-output is being used. This is to show the newest state in your console right after your write operation has been processed.
Don't forget to replace $FUNCTION_ID with the id generated during the deployment of your EXM Function
After running the write operation command, you should see something similar to:

Reading State

If you only wish to read the state of an already-deployed EXM Function, you can take a look at the following article:

Conclusions

In this article, we have accomplished:
  • Creating a dApp from scratch
  • Deploying it
  • Interacting with it
  • Reading its current state