Skip to content

Commit e378cd2

Browse files
authored
Merge pull request #1 from polkadot-api/provider
Add providers section
2 parents 5fbc918 + 3c5f166 commit e378cd2

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

docs/pages/providers.mdx

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
11
# Providers
22

3-
providers
3+
The entry point of Polkadot-API, `createClient(provider)` requires one `JsonRpcProvider`, which lets Polkadot-API communicate with a node. It's a function with the following shape:
4+
5+
```ts
6+
interface JsonRpcProvider {
7+
(onMessage: (message: string) => void) => JsonRpcConnection;
8+
}
9+
10+
interface JsonRpcConnection {
11+
send: (message: string) => void;
12+
disconnect: () => void;
13+
}
14+
```
15+
16+
Calling it will initiate a connection. Messages coming from the service will come through the `onMessage` call, and the returned connection handle can be used to send messages or terminate the connection.
17+
18+
Polkadot-API offers a couple of providers for some of the most used ways of connecting to a chain:
19+
20+
- `WebSocketProvider(uri: string)` from `polkadot-api/ws-provider/web` or `polkadot-api/ws-provider/node` to connect through WebSocket.
21+
- `getSmProvider(chain: smoldot.Chain)` from `polkadot-api/sm-provider` to connect through Smoldot.
22+
23+
The `JsonRpcProvider` interface is designed so that it can be easily enhanced: You can wrap any JsonRpcProvider with another one that adds in more features, such as logging, statistics, or error recovery.
24+
25+
## Logs provider
26+
27+
Polkadot-API has a subpackage `polkadot-api/logs-provider` that can be used to create a provider that will replay node messages from a log file (`logsProvider`), along with a provider enhancer that can be used to generate the logs consumed by `logsProvider`: `withLogsRecorder`.
28+
29+
```ts
30+
// 1. recording logs
31+
import { createClient } from 'polkadot-api';
32+
import { withLogsRecorder } from 'polkadot-api/logs-provider';
33+
importWebSocketProvider } from 'polkadot-api/ws-provider/node';
34+
35+
const wsProvider = WebSocketProvider("wss://example.url");
36+
// Using console.log to output each line, but you could e.g. write it directly to a
37+
// file or push into an array
38+
const provider = withLogsRecorder(line => console.log(line), wsProvider);
39+
const client = createClient(provider);
40+
```
41+
42+
```ts
43+
// 2. replaying logs
44+
import { createClient } from 'polkadot-api';
45+
import { logsProvider } from 'polkadot-api/logs-provider';
46+
import logs from './readLogs';
47+
48+
const provider = logsProvider(logs);
49+
const client = createClient(provider);
50+
```
51+
52+
This can be useful to debug specific scenarios without needing to depend on an external source.

0 commit comments

Comments
 (0)