You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/dapps/papi/remark-tutorial.md
+28-51Lines changed: 28 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,50 +1,50 @@
1
1
---
2
2
title: Polkadot API Account Watcher Tutorial
3
-
description: This tutorial will focus on learning how to build a decentralized commandline application using the Polkadot API.
3
+
description: Learn how to build a decentralized command-line application using the Polkadot API.
4
4
---
5
5
6
-
### Project Introduction
6
+
!!!info "This tutorial uses the Westend Test Network"
7
+
Ensure you have an account with WND tokens before proceeding with this tutorial.
7
8
8
-
Our project will be quite simple - it will be a CLI application that runs in the terminal, which watches the relay chain for a certain `extrinsic` (a transaction). This extrinsic will be the `system.remarkWithEvent` extrinsic, meaning it is coming from the `system` pallet (module) on the Westend test network.
9
+
### Introduction
9
10
10
-
The `system.remarkWithEvent` extrinsic allows us to send any arbitrary data on-chain, with the end result being a hash that is the address and the word "email" combined (`address+email`). We'll hash this combination and watch for remarks on a chain that are addressed to us. The `system.remarkWithEvent` extrinsic emits an event that we can use PAPI to watch the chain for.
11
+
This tutorial demonstrates how to build a simple command-line interface (CLI) application that monitors a user's account on the relay chain for the `system.remarkWithEvent` extrinsic.
11
12
12
-
Once we receive a remark addressed to us, we will play the infamous "You've Got Mail!" sound byte.
13
+
The `system.remarkWithEvent` extrinsic enables the submission of arbitrary data on-chain. In this tutorial, the data consists of a hash derived from the combination of an account address and the word "email" (`address+email`). This hash is monitored on-chain, and the application listens for remarks addressed to the specified account. The `system.remarkWithEvent` extrinsic emits an event that can be observed using the Polkadot API (PAPI).
14
+
15
+
Upon detecting a remark addressed to the specified account, the application plays the iconic "You've Got Mail!" sound byte.
13
16
14
17
### Prerequisites
15
18
16
-
You should have the following installed as a prerequisite:
19
+
Before starting, ensure the following tools and dependencies are installed:
For this tutorial, you can choose to run the example directly by cloning the [main branch of the repository](https://github.com/CrackTheCode016/polkadot-api-example-cli/tree/main), or to use a boilerplate/template and follow the tutorial.
30
+
### Cloning the Repository
31
31
32
-
We need to clone the template, which has everything we need to get started with the Polkadot API and Typescript. Be sure you clone the correct branch (`empty-cli`) which already provides all dependencies:
32
+
To follow this tutorial, you can either run the example directly or use a boilerplate/template. This tutorial uses a template that includes all necessary dependencies for working with the Polkadot API and TypeScript. Clone the appropriate branch (`empty-cli`) of the repository as follows:
The notable function to pay attention to is the `withLightClient` function. This function uses the builtin light client functionality (powered by [`smoldot`](https://github.com/smol-dot/smoldot)) to actually create a light client that syncs and interacts with Polkadot right there in our application.
67
+
The `withLightClient` function is of particular importance. It leverages the built-in light client functionality, powered by [`smoldot`](https://github.com/smol-dot/smoldot), to create a light client that synchronizes and interacts with Polkadot directly within the application.
68
68
69
69
### Creating the CLI
70
70
71
-
Next, let's create our CLI, which is to be done within the confines of the `main` function. We will include an option (`-a` / `--account`), which will be the account we will watch for our "mail":
71
+
The CLI functionality is implemented within the `main` function. The CLI includes an option (`-a` / `--account`) to specify the account to monitor for remarks:
72
72
73
73
```ts
74
74
const program =newCommand();
@@ -83,7 +83,7 @@ const options = program.opts();
83
83
84
84
### Watching for Remarks
85
85
86
-
Next, we need to start watching the Westend network for remarks sent to our account. As was done before, all code should be within the `main` function:
86
+
The application monitors the Westend network for remarks sent to the specified account. The following code, placed within the `main` function, implements this functionality:
87
87
88
88
```typescript
89
89
// We check for the --account flag, if its not provided we exit
@@ -114,24 +114,15 @@ Next, we need to start watching the Westend network for remarks sent to our acco
114
114
}
115
115
```
116
116
117
-
This code is doing quite a bit, so let's break it down:
118
-
119
-
- First, we check for the existance of the `--account` argument, and log that we are watching that account, else we exit. We are using the `chalk` package to add color to our `console.log` statements.
120
-
- Next, we create our light client.
121
-
- We use a light client and the Westend chain specification (`wnd`) to access a typed API.
122
-
- Once we have our API, we then begin to reactively watch the account for the event that corresponds to the remark. We analyze the payload, looking for a hash which is calculated as follows:
123
-
- hash of: `account_address+email`
124
-
- When an event containing this hash is identified, it then plays the "You've Got Mail!" soundbite.
117
+
### Compiling and Running
125
118
126
-
### Compiling and running
127
-
128
-
Once we have all of our code in place, we should compile and run the repository:
119
+
Compile and execute the application using the following command:
129
120
130
121
```shell
131
122
npm start -- --account <account-address>
132
123
```
133
124
134
-
Upon running, we should have the following output:
Now that our application is actively watching for remark events on-chain, we can move to testing to see if it works!
156
-
157
-
> As mentioned previously, you will need a Westend account with some tokens to pay for fees.
144
+
### Testing the CLI
158
145
159
-
Navigate to the [PAPI Dev Console > Extrinsics](https://dev.papi.how/extrinsics#networkId=westend&endpoint=light-client). You then want to select the `System` pallet, and the `remark_with_event` call:
146
+
To test the application, navigate to the [PAPI Dev Console > Extrinsics](https://dev.papi.how/extrinsics#networkId=westend&endpoint=light-client). Select the `System` pallet and the `remark_with_event` call.
160
147
161
-

162
-
163
-
Next, we want to be sure we get the correct input for the text field. We want to be sure we are following the convention we set forth in our application:
164
-
165
-
- `address+email`
166
-
167
-
If for example, we are watching `5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY`, then the field should look like the following:
148
+
Ensure the input field follows the convention `address+email`. For example, if monitoring `5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY`, the input should be:
168
149
169
150

170
151
171
-
Once this input is in place, you may click the `Submit extrinsic` button, where you can sign using the Polkadot.js browser wallet:
172
-
173
-

174
-
175
-
Heading back to our CLI, we should soon see the following, along with the fact that "YOU'VE GOT MAIL!" (as in the sound should play):
152
+
Submit the extrinsic and sign it using the Polkadot.js browser wallet. The CLI will display the following output and play the "You've Got Mail!" sound:
This application can be expanded in a number of ways, whether that is by adding a chatroom through remarks, or by using some of the rollups on Polkadot to expand the functionality.
169
+
This application can be extended in various ways, such as implementing a chatroom using remarks or leveraging rollups on Polkadot to enhance functionality.
0 commit comments