Skip to content

Commit ff01ef2

Browse files
committed
fix: snippets
1 parent e2ae5b7 commit ff01ef2

File tree

5 files changed

+226
-63
lines changed

5 files changed

+226
-63
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const program = new Command();
2+
console.log(chalk.white.dim(figlet.textSync("Web3 Mail Watcher")));
3+
program
4+
.version('0.0.1')
5+
.description('Web3 Mail Watcher - A simple CLI tool to watch for remarks on the Polkadot network')
6+
.option('-a, --account <account>', 'Account to watch')
7+
.parse(process.argv);
8+
9+
// CLI arguments from commander
10+
const options = program.opts();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
async function withLightClient(): Promise<PolkadotClient> {
2+
// Start the light client
3+
const smoldot = start();
4+
// The Westend Relay Chain
5+
const relayChain = await smoldot.addChain({ chainSpec: westEndChainSpec });
6+
return createClient(
7+
getSmProvider(relayChain)
8+
);
9+
}
10+
11+
async function main() {
12+
// CLI code goes here...
13+
}
14+
15+
main();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
if (options.account) {
2+
console.log(chalk.black.bgRed("Watching account:"), chalk.bold.whiteBright(options.account));
3+
// Create a light client to connect to the Polkadot (Westend) network
4+
const lightClient = await withLightClient();
5+
// Get the typed API to interact with the network
6+
const dotApi = lightClient.getTypedApi(wnd);
7+
// Subscribe to the System.Remarked event and watch for remarks from the account
8+
dotApi.event.System.Remarked.watch().subscribe((event) => {
9+
const { sender, hash } = event.payload;
10+
const calculatedHash = bytesToHex(blake2b(`${options.account}+email`, { dkLen: 32 }));
11+
if (`0x${calculatedHash}` === hash.asHex()) {
12+
sound.play("youve-got-mail-sound.mp3");
13+
console.log(chalk.black.bgRed("You got mail!"));
14+
console.log(chalk.black.bgCyan("From:"), chalk.bold.whiteBright(sender.toString()));
15+
console.log(chalk.black.bgBlue("Hash:"), chalk.bold.whiteBright(hash.asHex()));
16+
}
17+
});
18+
} else {
19+
console.error('Account is required');
20+
return;
21+
}

llms.txt

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ Doc-Page: https://docs.polkadot.com/polkadot-protocol/
126126
Doc-Page: https://docs.polkadot.com/polkadot-protocol/onchain-governance/
127127
Doc-Page: https://docs.polkadot.com/polkadot-protocol/onchain-governance/origins-tracks/
128128
Doc-Page: https://docs.polkadot.com/polkadot-protocol/onchain-governance/overview/
129+
Doc-Page: https://docs.polkadot.com/tutorials/dapps/
130+
Doc-Page: https://docs.polkadot.com/tutorials/dapps/papi/
131+
Doc-Page: https://docs.polkadot.com/tutorials/dapps/papi/remark-tutorial/
129132
Doc-Page: https://docs.polkadot.com/tutorials/
130133
Doc-Page: https://docs.polkadot.com/tutorials/interoperability/
131134
Doc-Page: https://docs.polkadot.com/tutorials/interoperability/xcm-channels/
@@ -19644,6 +19647,170 @@ See [Cancelling, Killing, and Blacklisting](https://wiki.polkadot.network/docs/l
1964419647
- [**Polkadot.js Apps Governance**](https://polkadot.js.org/apps/#/referenda){target=\_blank} - overview of active referendums
1964519648
--- END CONTENT ---
1964619649

19650+
Doc-Content: https://docs.polkadot.com/tutorials/dapps/
19651+
--- BEGIN CONTENT ---
19652+
---
19653+
title: Decentralized Application Tutorials
19654+
description: Explore step-by-step tutorials for exploring the world of building decentralized applications using the toolkits that Polkadot provides.
19655+
template: index-page.html
19656+
---
19657+
--- END CONTENT ---
19658+
19659+
Doc-Content: https://docs.polkadot.com/tutorials/dapps/papi/
19660+
--- BEGIN CONTENT ---
19661+
---
19662+
title: Polkadot API (PAPI) Tutorials
19663+
description: Explore step-by-step tutorials for building applications using the Polkadot API.
19664+
template: index-page.html
19665+
---
19666+
--- END CONTENT ---
19667+
19668+
Doc-Content: https://docs.polkadot.com/tutorials/dapps/papi/remark-tutorial/
19669+
--- BEGIN CONTENT ---
19670+
---
19671+
title: Polkadot API Account Watcher Tutorial
19672+
description: Learn how to build a decentralized command-line application using the Polkadot API.
19673+
---
19674+
19675+
## Introduction
19676+
19677+
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`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.remark_with_event){target=\_blank} extrinsic.
19678+
19679+
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).
19680+
19681+
When the application detects a remark addressed to the specified account, it plays the "You've Got Mail!" sound byte.
19682+
19683+
## Prerequisites
19684+
19685+
Before starting, ensure the following tools and dependencies are installed:
19686+
19687+
- Node.js (version 18 or higher)
19688+
- A package manager (npm or yarn)
19689+
- [Polkadot.js Browser Extension (wallet)](https://polkadot.js.org/extension/){target=\_blank}
19690+
19691+
Additionally, you need an account with Westend tokens. Refer to the following resources for assistance:
19692+
19693+
- [Westend Faucet](https://faucet.polkadot.io/westend){target=\_blank}
19694+
19695+
## Clone the Repository
19696+
19697+
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:
19698+
19699+
```bash
19700+
git clone https://github.com/CrackTheCode016/polkadot-api-example-cli --branch empty-cli
19701+
```
19702+
19703+
After cloning, install the required dependencies by running:
19704+
19705+
```bash
19706+
cd polkadot-api-example-cli
19707+
npm install
19708+
```
19709+
19710+
## Explore the Template (Light Clients)
19711+
19712+
After opening the repository, you will find the following code (excluding imports):
19713+
19714+
```typescript title="index.ts"
19715+
code/develop/dapps/papi/remark-tutorial/index.ts
19716+
```
19717+
19718+
The `withLightClient` function is particularly important. It uses the built-in light client functionality, powered by [`smoldot`](https://github.com/smol-dot/smoldot){target=\_blank}, to create a light client that synchronizes and interacts with Polkadot directly within the application.
19719+
19720+
## Create the CLI
19721+
19722+
The CLI functionality is implemented within the `main` function. The CLI includes an option (`-a` / `--account`) to specify the account to monitor for remarks:
19723+
19724+
```typescript title="index.ts"
19725+
code/develop/dapps/papi/remark-tutorial/cli.ts
19726+
```
19727+
19728+
## Watch for Remarks
19729+
19730+
The application monitors the Westend network for remarks sent to the specified account. The following code, placed within the `main` function, implements this functionality:
19731+
19732+
```typescript title="index.ts"
19733+
if (options.account) {
19734+
console.log(chalk.black.bgRed("Watching account:"), chalk.bold.whiteBright(options.account));
19735+
// Create a light client to connect to the Polkadot (Westend) network
19736+
const lightClient = await withLightClient();
19737+
// Get the typed API to interact with the network
19738+
const dotApi = lightClient.getTypedApi(wnd);
19739+
// Subscribe to the System.Remarked event and watch for remarks from the account
19740+
dotApi.event.System.Remarked.watch().subscribe((event) => {
19741+
const { sender, hash } = event.payload;
19742+
const calculatedHash = bytesToHex(blake2b(`${options.account}+email`, { dkLen: 32 }));
19743+
if (`0x${calculatedHash}` === hash.asHex()) {
19744+
sound.play("youve-got-mail-sound.mp3");
19745+
console.log(chalk.black.bgRed("You got mail!"));
19746+
console.log(chalk.black.bgCyan("From:"), chalk.bold.whiteBright(sender.toString()));
19747+
console.log(chalk.black.bgBlue("Hash:"), chalk.bold.whiteBright(hash.asHex()));
19748+
}
19749+
});
19750+
} else {
19751+
console.error('Account is required');
19752+
return;
19753+
}
19754+
```
19755+
19756+
## Compile and Run
19757+
19758+
Compile and execute the application using the following command:
19759+
19760+
```bash
19761+
npm start -- --account <account-address>
19762+
```
19763+
19764+
For example:
19765+
19766+
```bash
19767+
npm start -- --account 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
19768+
```
19769+
19770+
The output should look like this:
19771+
19772+
```
19773+
__ __ _ _____ __ __ _ _ __ __ _ _
19774+
\ \ / /__| |__|___ / | \/ | __ _(_) | \ \ / /_ _| |_ ___| |__ ___ _ __
19775+
\ \ /\ / / _ \ '_ \ |_ \ | |\/| |/ _` | | | \ \ /\ / / _` | __/ __| '_ \ / _ \ '__|
19776+
\ V V / __/ |_) |__) | | | | | (_| | | | \ V V / (_| | || (__| | | | __/ |
19777+
\_/\_/ \___|_.__/____/ |_| |_|\__,_|_|_| \_/\_/ \__,_|\__\___|_| |_|\___|_|
19778+
19779+
Watching account: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
19780+
[smoldot] Smoldot v2.0.34
19781+
[smoldot] Chain initialization complete for westend2. Name: "Westend". Genesis hash: 0xe143…423e. Chain specification starting at: 0x10cf…b908 (#23920337)
19782+
```
19783+
19784+
## Test the CLI
19785+
19786+
To test the application, navigate to the [PAPI Dev Console > Extrinsics](https://dev.papi.how/extrinsics#networkId=westend&endpoint=light-client){target=\_blank}. Select the `System` pallet and the `remark_with_event` call.
19787+
19788+
Ensure the input field follows the convention `address+email`. For example, if monitoring `5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY`, the input should be:
19789+
19790+
![](images/develop/dapps/papi/papi-console.webp)
19791+
19792+
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:
19793+
19794+
```
19795+
__ __ _ _____ __ __ _ _ __ __ _ _
19796+
\ \ / /__| |__|___ / | \/ | __ _(_) | \ \ / /_ _| |_ ___| |__ ___ _ __
19797+
\ \ /\ / / _ \ '_ \ |_ \ | |\/| |/ _` | | | \ \ /\ / / _` | __/ __| '_ \ / _ \ '__|
19798+
\ V V / __/ |_) |__) | | | | | (_| | | | \ V V / (_| | || (__| | | | __/ |
19799+
\_/\_/ \___|_.__/____/ |_| |_|\__,_|_|_| \_/\_/ \__,_|\__\___|_| |_|\___|_|
19800+
19801+
Watching account: 5Cm8yiG45rqrpyV2zPLrbtr8efksrRuCXcqcB4xj8AejfcTB
19802+
You've got mail!
19803+
From: 5Cm8yiG45rqrpyV2zPLrbtr8efksrRuCXcqcB4xj8AejfcTB
19804+
Hash: 0xb6999c9082f5b1dede08b387404c9eb4eb2deee4781415dfa7edf08b87472050
19805+
```
19806+
19807+
## Next Steps
19808+
19809+
This application demonstrates how the Polkadot API can be used to build decentralized applications. While this is not a production-grade application, it introduces several key features for developing with the Polkadot API.
19810+
19811+
To explore more, refer to the [official PAPI documentation](https://papi.how){target=\_blank}.
19812+
--- END CONTENT ---
19813+
1964719814
Doc-Content: https://docs.polkadot.com/tutorials/
1964819815
--- BEGIN CONTENT ---
1964919816
---

tutorials/dapps/papi/remark-tutorial.md

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ title: Polkadot API Account Watcher Tutorial
33
description: Learn how to build a decentralized command-line application using the Polkadot API.
44
---
55

6-
!!!info "This tutorial uses the Westend Test Network"
7-
Ensure you have an account with WND tokens before proceeding with this tutorial.
8-
96
## Introduction
107

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.
8+
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`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.remark_with_event){target=\_blank} extrinsic.
129

1310
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).
1411

@@ -18,9 +15,8 @@ When the application detects a remark addressed to the specified account, it pla
1815

1916
Before starting, ensure the following tools and dependencies are installed:
2017

21-
- `npm` (or an alternative package manager)
22-
- `node`
23-
- `git`
18+
- Node.js (version 18 or higher)
19+
- A package manager (npm or yarn)
2420
- [Polkadot.js Browser Extension (wallet)](https://polkadot.js.org/extension/){target=\_blank}
2521

2622
Additionally, you need an account with Westend tokens. Refer to the following resources for assistance:
@@ -31,13 +27,13 @@ Additionally, you need an account with Westend tokens. Refer to the following re
3127

3228
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:
3329

34-
```shell
30+
```bash
3531
git clone https://github.com/CrackTheCode016/polkadot-api-example-cli --branch empty-cli
3632
```
3733

3834
After cloning, install the required dependencies by running:
3935

40-
```shell
36+
```bash
4137
cd polkadot-api-example-cli
4238
npm install
4339
```
@@ -46,23 +42,8 @@ npm install
4642

4743
After opening the repository, you will find the following code (excluding imports):
4844

49-
```typescript
50-
// filepath: /path/to/repository/src/index.ts
51-
async function withLightClient(): Promise<PolkadotClient> {
52-
// Start the light client
53-
const smoldot = start();
54-
// The Westend Relay Chain
55-
const relayChain = await smoldot.addChain({ chainSpec: westEndChainSpec });
56-
return createClient(
57-
getSmProvider(relayChain)
58-
);
59-
}
60-
61-
async function main() {
62-
// CLI code goes here...
63-
}
64-
65-
main();
45+
```typescript title="index.ts"
46+
--8<-- 'code/develop/dapps/papi/remark-tutorial/index.ts'
6647
```
6748

6849
The `withLightClient` function is particularly important. It uses the built-in light client functionality, powered by [`smoldot`](https://github.com/smol-dot/smoldot){target=\_blank}, to create a light client that synchronizes and interacts with Polkadot directly within the application.
@@ -71,60 +52,29 @@ The `withLightClient` function is particularly important. It uses the built-in l
7152

7253
The CLI functionality is implemented within the `main` function. The CLI includes an option (`-a` / `--account`) to specify the account to monitor for remarks:
7354

74-
```typescript
75-
// filepath: /path/to/repository/src/index.ts
76-
const program = new Command();
77-
console.log(chalk.white.dim(figlet.textSync("Web3 Mail Watcher")));
78-
program
79-
.version('0.0.1')
80-
.description('Web3 Mail Watcher - A simple CLI tool to watch for remarks on the Polkadot network')
81-
.option('-a, --account <account>', 'Account to watch')
82-
.parse(process.argv);
83-
84-
// CLI arguments from commander
85-
const options = program.opts();
55+
```typescript title="index.ts"
56+
--8<-- 'code/develop/dapps/papi/remark-tutorial/cli.ts'
8657
```
8758

8859
## Watch for Remarks
8960

9061
The application monitors the Westend network for remarks sent to the specified account. The following code, placed within the `main` function, implements this functionality:
9162

92-
```typescript
93-
// filepath: /path/to/repository/src/index.ts
94-
if (options.account) {
95-
console.log(chalk.black.bgRed("Watching account:"), chalk.bold.whiteBright(options.account));
96-
// Create a light client to connect to the Polkadot (Westend) network
97-
const lightClient = await withLightClient();
98-
// Get the typed API to interact with the network
99-
const dotApi = lightClient.getTypedApi(wnd);
100-
// Subscribe to the System.Remarked event and watch for remarks from the account
101-
dotApi.event.System.Remarked.watch().subscribe((event) => {
102-
const { sender, hash } = event.payload;
103-
const calculatedHash = bytesToHex(blake2b(`${options.account}+email`, { dkLen: 32 }));
104-
if (`0x${calculatedHash}` === hash.asHex()) {
105-
sound.play("youve-got-mail-sound.mp3");
106-
console.log(chalk.black.bgRed("You got mail!"));
107-
console.log(chalk.black.bgCyan("From:"), chalk.bold.whiteBright(sender.toString()));
108-
console.log(chalk.black.bgBlue("Hash:"), chalk.bold.whiteBright(hash.asHex()));
109-
}
110-
});
111-
} else {
112-
console.error('Account is required');
113-
return;
114-
}
63+
```typescript title="index.ts"
64+
--8<-- 'code/tutorials/dapps/papi/remark-tutorial/remarks.ts'
11565
```
11666

11767
## Compile and Run
11868

11969
Compile and execute the application using the following command:
12070

121-
```shell
71+
```bash
12272
npm start -- --account <account-address>
12373
```
12474

12575
For example:
12676

127-
```shell
77+
```bash
12878
npm start -- --account 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
12979
```
13080

0 commit comments

Comments
 (0)