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
To quickly test _all_ of æternity's blockchain features from your terminal, you can install and use the [CLI](https://github.com/aeternity/aepp-cli-js) by running:
Copy file name to clipboardExpand all lines: docs/guides/low-vs-high-usage.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,22 +8,22 @@ But there is also low-level interfaces. It's excellent for additional control, a
8
8
9
9
### Node API
10
10
11
-
The aeternity node exposes [a REST API]. This API is described in the [OpenAPI document]. SDK uses this document to generate a TypeScript client. The result client (implemented in [`Node` class]) a basically a mapping of all node endpoints as functions.
11
+
The aeternity node exposes [a REST API]. This API is described in the [OpenAPI document]. SDK uses this document to generate a TypeScript client. The result client (implemented in [`Node` class][node]) a basically a mapping of all node endpoints as functions.
So to get a transaction based on its hash you would invoke `node.getTransactionByHash('th_fWEsg152BNYcrqA9jDh9VVpacYojCUb1yu45zUnqhmQ3dAAC6')`. In this way the SDK is simply a mapping of the raw API calls into JavaScript.
18
18
19
19
### Transaction builder
20
20
21
-
Any blockchain state change requires signing a transaction. Transaction should be built according to the [protocol]. SDK implements it in [`buildTx`], [`buildTxAsync`], and [`unpackTx`]. [`buildTxAsync`] requires fewer arguments than [`buildTx`], but it expects the node instance provided in arguments.
21
+
Any blockchain state change requires signing a transaction. Transaction should be built according to the [protocol]. SDK implements it in [`buildTx`][buildTx], [`buildTxAsync`][buildTxAsync], and [`unpackTx`][unpackTx]. [`buildTxAsync`][buildTxAsync] requires fewer arguments than [`buildTx`][buildTx], but it expects the node instance provided in arguments.
Copy file name to clipboardExpand all lines: docs/guides/typescript.md
+30-30Lines changed: 30 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Firstly, ensure you've set up TypeScript according to the [installation guide].
8
8
9
9
## Extract types of methods exposed by SDK
10
10
11
-
SDK doesn't expose types separately to reduce the number of exports and simplify tracking of breaking changes. But you may need these types to prepare parameters or to hold the return value. In such cases, it is advised to use TypeScript-provided generics [`Parameters`] and [`ReturnType`]. For example,
11
+
SDK doesn't expose types separately to reduce the number of exports and simplify tracking of breaking changes. But you may need these types to prepare parameters or to hold the return value. In such cases, it is advised to use TypeScript-provided generics [`Parameters`][Parameters] and [`ReturnType`][ReturnType]. For example,
The problem in this case, is that TypeScript will generalize the type of `unpackedEntry.txHash` to `string` instead of `th_${string}` making it incompatible with arguments of [`packEntry`]. To fix this you may define `gaAuthData`'s type explicitly, like:
58
+
The problem in this case, is that TypeScript will generalize the type of `unpackedEntry.txHash` to `string` instead of `th_${string}` making it incompatible with arguments of [`packEntry`][packEntry]. To fix this you may define `gaAuthData`'s type explicitly, like:
In the last case, `txHash`'s type will be exactly `"th_2CKnN6EorvNiwwqRjSzXLrPLiHmcwo4Ny22dwCrSYRoD6MVGK1"`, making it compatible with [`packEntry`].
86
+
In the last case, `txHash`'s type will be exactly `"th_2CKnN6EorvNiwwqRjSzXLrPLiHmcwo4Ny22dwCrSYRoD6MVGK1"`, making it compatible with [`packEntry`][packEntry].
## Narrow the union type returned by [`unpackTx`], [`unpackDelegation`], and [`unpackEntry`]
90
+
## Narrow the union type returned by [`unpackTx`][unpackTx], [`unpackDelegation`][unpackDelegation], and [`unpackEntry`][unpackEntry]
91
91
92
-
Some sdk methods return a [union] of multiple types. For example, [`unpackTx`] returns a union of [all supported transaction] fields. To work correctly you need to narrow this type to a specific transaction before accessing its fields. For example,
92
+
Some sdk methods return a [union] of multiple types. For example, [`unpackTx`][unpackTx] returns a union of [all supported transaction] fields. To work correctly you need to narrow this type to a specific transaction before accessing its fields. For example,
93
93
94
94
```ts
95
95
import { unpackTx, Tag } from'@aeternity/aepp-sdk';
@@ -110,15 +110,15 @@ Without checking the `tx.tag` TypeScript will fail with
110
110
111
111
> Property 'amount' does not exist on type 'TxUnpackedSignedTx1 & { tag: Tag; }'.
112
112
113
-
The above check is also implemented in [`unpackTx`] itself, instead of checking the `tx.tag` you can provide Tag in the second argument:
113
+
The above check is also implemented in [`unpackTx`][unpackTx] itself, instead of checking the `tx.tag` you can provide Tag in the second argument:
114
114
115
115
```ts
116
116
const tx =unpackTx(encodedTx, Tag.SpendTx);
117
117
```
118
118
119
119
But if you need to get SpendTx properties inside a SignedTx you still need to use the above `tag` check.
120
120
121
-
You may find that [`unpackTx`] is a generic function so that it can be executed as
121
+
You may find that [`unpackTx`][unpackTx] is a generic function so that it can be executed as
## Functions to assert types of user-provided data
136
136
137
-
Let's assume we need to receive an address from the user to send some coins to it. The user enters an address in a text box, we can get it as a string. [`spend`] method accepts the address as [`Encoded.AccountAddress`], it won't accept a general string. We can overcome this restriction by adding a type assertion, like:
137
+
Let's assume we need to receive an address from the user to send some coins to it. The user enters an address in a text box, we can get it as a string. [`spend`][spend] method accepts the address as [`Encoded.AccountAddress`][Encoded.AccountAddress], it won't accept a general string. We can overcome this restriction by adding a type assertion, like:
The problem is that TypeScript won't check if `address` is an `ak_`-encoded string, and the [`spend`] method will fail in this case.
143
+
The problem is that TypeScript won't check if `address` is an `ak_`-encoded string, and the [`spend`][spend] method will fail in this case.
144
144
A more accurate solution would be to check the `address` in advance, providing user feedback if it is incorrect. For example:
145
145
146
146
```ts
@@ -154,9 +154,9 @@ if (!isEncoded(address, Encoding.AccountAddress)) {
154
154
awaitaeSdk.spend(100, address);
155
155
```
156
156
157
-
Please note that this method doesn't require explicit casting `string` to [`Encoded.AccountAddress`] because [`isEncoded`] implicitly marks `address` as `ak_${string}` in case it returns `true`.
157
+
Please note that this method doesn't require explicit casting `string` to [`Encoded.AccountAddress`][Encoded.AccountAddress] because [`isEncoded`][isEncoded] implicitly marks `address` as `ak_${string}` in case it returns `true`.
158
158
159
-
Additionally, you can use [`isEncoded`] to validate data against other address types:
159
+
Additionally, you can use [`isEncoded`][isEncoded] to validate data against other address types:
160
160
161
161
```ts
162
162
import { Encoding } from'@aeternity/aepp-sdk';
@@ -170,13 +170,13 @@ Or encoding types in general:
If you don't need to handle invalid names specially then you can use [`ensureName`]:
189
+
If you don't need to handle invalid names specially then you can use [`ensureName`][ensureName]:
190
190
191
191
```ts
192
192
import { ensureName, Name } from'@aeternity/aepp-sdk';
@@ -196,14 +196,14 @@ ensureName(nameAsString);
196
196
const name =newName(nameAsString, options);
197
197
```
198
198
199
-
Doing this way, [`ensureName`] will throw an exception if `nameAsString` is not a proper AENS name. TypeScript will handle `nameAsString` as `${string}.chain` in lines below [`ensureName`] invocation.
199
+
Doing this way, [`ensureName`][ensureName] will throw an exception if `nameAsString` is not a proper AENS name. TypeScript will handle `nameAsString` as `${string}.chain` in lines below [`ensureName`][ensureName] invocation.
By default, it is allowed to call any method of the [`Contract`] instance. You can enable type-checking by providing a contract interface in a generic parameter of [`Contract`]. For example:
206
+
By default, it is allowed to call any method of the [`Contract`][Contract] instance. You can enable type-checking by providing a contract interface in a generic parameter of [`Contract`][Contract]. For example:
207
207
208
208
```ts
209
209
import { Contract } from'@aeternity/aepp-sdk';
@@ -239,7 +239,7 @@ console.log((await contract.bar(new Map([['test', 10n]]))).decodedResult); // Ma
0 commit comments