Skip to content

Commit bd19cef

Browse files
committed
finish enum types
1 parent e16bc98 commit bd19cef

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

docs/pages/types.mdx

+36-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,45 @@ Enums in the chain are represented as `{ type: string, value: T }`. As many of t
3333

3434
First of all, the Enums that are widely used across multiple chains are in a directory of well-known types, and they are represented with a descriptive name. A few examples: `MultiAddress`, `BalanceStatus`, `IdentityJudgement`, and many of the XCM pallet types: `XcmV3Junction`, `XcmV3MultiassetFungibility`, etc.
3535

36-
For these types, you can import them directly from the generated code and use them by calling their type:
36+
For these types, you can import them directly from the generated code and use them by calling their type. The call signature shown by an IDE will tell you exactly which enum types you should use to write your value. The following video shows how it might look like:
3737

3838
<video src="enums.mp4" controls />
3939

40+
The enums that are not well-known types, they are anonymous. In that case, you will find something like the following in the call signature:
41+
42+
```ts
43+
(value: IEnum<{
44+
transfer_allow_death: {
45+
dest: MultiAddress;
46+
value: bigint;
47+
};
48+
force_transfer: {
49+
dest: MultiAddress;
50+
value: bigint;
51+
source: MultiAddress;
52+
};
53+
... 4 more ...;
54+
force_set_balance: {
55+
...;
56+
};
57+
}>) => PolkadotRuntimeRuntimeCall
58+
```
59+
60+
This indicates that the parameter value is an enum, whose key will be either one of the keys of the object type (i.e. `transfer_allow_death`, `force_transfer`, ..., `force_set_balance`), and the type will be the value for that particular key.
61+
62+
For these cases, you should use the function `Enum(type, value)`, imported from `@polkadot-api/client`. This has full type inference support, and creates an Enum object that can be used as a parameter of a call:
63+
64+
```ts
65+
dotApi.apis.TransactionPaymentCallApi.query_call_info(
66+
PolkadotRuntimeRuntimeCall.Balances(
67+
Enum("transfer_allow_death", { dest: MultiAddress.Id(address), value: 3n })
68+
),
69+
10
70+
);
71+
```
72+
73+
When reading from Enums, these are objects with `{ type: string, value: unknown }` with discriminated types based on the `type` (so if you do `switch (enum.type) {` you will have the correct value for the `type`).
74+
4075
## Binary
4176

4277
Any array of u8's is represented as Binary. This is a utility type that has a few functions to easily create binary data:

0 commit comments

Comments
 (0)