Skip to content

Commit 297d3b5

Browse files
committed
feat: add UnsafeApi
1 parent 6779170 commit 297d3b5

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

docs/pages/client.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ interface PolkadotClient {
104104
*/
105105
getTypedApi: <D extends ChainDefinition>(descriptors: D) => TypedApi<D>
106106

107+
/**
108+
* Returns an instance of a `UnsafeApi`.
109+
*
110+
* Note that this method is only meant for advanced users that really know
111+
* what are they doing. This API does not provide any runtime compatibility
112+
* checks protection and the consumer should implement them on their own.
113+
*/
114+
getUnsafeApi: <D>() => UnsafeApi<D>
115+
107116
/**
108117
* This will `unfollow` the provider, disconnect and error every subscription.
109118
* After calling it nothing can be done with the client.
@@ -131,4 +140,4 @@ interface PolkadotClient {
131140
}
132141
```
133142

134-
As one can note, `PolkadotClient` heavily relies on rxjs' `Observable`, used as well under the hood of Promise-based methods. Every method is fairly straight-forward and already documented exhaustively, except for `getTypedApi`. Let's dive into it.
143+
As one can note, `PolkadotClient` heavily relies on rxjs' `Observable`, used as well under the hood of Promise-based methods. Every method is fairly straight-forward and already documented exhaustively, except for `getTypedApi` and `getUnsafeApi`. We will see first of all the `TypedApi`, and afterwards the `UnsafeApi`.

docs/pages/typed.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ The `TypedApi` allows to interact with the runtime metadata easily and with a gr
66
type TypedApi = {
77
query: StorageApi
88
tx: TxApi
9+
txFromCallData: TxFromBinary
910
event: EvApi
1011
apis: RuntimeCallsApi
1112
constants: ConstApi
1213
compatibilityToken: Promise<CompatibilityToken>
1314
}
1415
```
1516
16-
Every field except for `compatibilityToken` is a `Record<string, Record<string, ???>>`. The first index defines the pallet, and the second one defines which query/tx/event/api/constant within that pallet. Each one of them will be described in the following pages, but let's focus on the compatibility check, which is common for all of them.
17+
Every field except for `compatibilityToken` and `txFromCallData` is a `Record<string, Record<string, ???>>`. The first index defines the pallet, and the second one defines which query/tx/event/api/constant within that pallet. Each one of them will be described in the following pages.
18+
19+
`txFromCallData` will be explained as well in the [`tx`](/typed/tx) section. Let's focus on the compatibility check, which is common for all of them.
1720
1821
## getCompatibilityLevel
1922

docs/pages/unsafe.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# UnsafeApi
2+
3+
The `UnsafeApi` enables interaction with the chain easily to the same extend [TypedApi](/typed) does, but it does not requires any descriptors. It is an advanced method and should only be used if you really know what you are doing. In order to create it, you can still pass a descriptors' type to get the same type inference as in the `typedApi`, but the shape of the entries at runtime level is not guaranteed.
4+
5+
:::warning
6+
The `UnsafeApi` does not provide any compatibility checks protection as `TypedApi` does.
7+
:::
8+
9+
The UnsafeApi has the following structure:
10+
11+
```ts
12+
type UnsafeApi = {
13+
query: StorageApi
14+
tx: TxApi
15+
txFromCallData: TxFromBinary
16+
event: EvApi
17+
apis: RuntimeCallsApi
18+
constants: ConstApi
19+
runtimeToken: Promise<RuntimeToken>
20+
}
21+
```
22+
23+
In order to create the unsafe api, it is actually very straightforward:
24+
25+
```ts
26+
const unsafeApi = client.getUnsafeApi() // without typings
27+
28+
// optionally generate descriptors to get type inference
29+
import { dot } from "@polkadot-api/descriptors"
30+
const unsafeApi = client.getUnsafeApi<typeof dot>() // with typings
31+
```
32+
33+
One can notice the API is actually very similar to the `TypedApi`, check [its docs](/typed) for the API reference since it behaves the exact same way. The only difference is that interactions do not include [compatibility methods](/typed#getcompatibilitylevel), and any reference to `compatibilityToken` in the typedApi is here a `runtimeToken`. For example:
34+
35+
```ts
36+
const typedApi = client.getTypedApi(descriptors)
37+
const unsafeApi = client.getUnsafeApi()
38+
39+
// in typed is `compatibilityToken`
40+
const typedToken = await typedApi.compatibilityToken
41+
// in unsafe is `runtimeToken`
42+
const unsafeToken = await typedApi.runtimeToken
43+
44+
// typed version
45+
typedApi.consts.System.SS58Prefix(typedToken)
46+
47+
// unsafe
48+
unsafeApi.consts.System.SS58Prefix(unsafeToken)
49+
```

vocs.config.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export default defineConfig({
7979
},
8080
],
8181
},
82+
{
83+
text: "Unsafe API",
84+
link: "/unsafe",
85+
},
8286
],
8387
},
8488
{
@@ -102,8 +106,8 @@ export default defineConfig({
102106
},
103107
{
104108
text: "Substrate Kitties",
105-
link: "https://github.com/shawntabrizi/substratekitties"
106-
}
109+
link: "https://github.com/shawntabrizi/substratekitties",
110+
},
107111
],
108112
},
109113
],

0 commit comments

Comments
 (0)