Skip to content

Commit 529cdba

Browse files
authored
Merge pull request #14 from windingtree/docs/sync-refactor
Docs/sync refactor
2 parents 9ee128c + ef8cc7b commit 529cdba

File tree

6 files changed

+158
-165
lines changed

6 files changed

+158
-165
lines changed

docs/docs/clients.md

+16-38
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
More about the client configuration options is [here](./index.md#client-node).
66

77
```typescript
8-
import { ClientOptions, createClient } from '@windingtree/sdk';
9-
import { RequestQuery, OfferOptions } from './config.js';
8+
import { GenericQuery, GenericOfferOptions, ClientOptions, createClient } from '@windingtree/sdk';
109

11-
const options: ClientOptions<RequestQuery, OfferOptions> = {
10+
export interface RequestQuery extends GenericQuery {
11+
/** your custom request interface */
12+
}
13+
14+
export interface OfferOptions extends GenericOfferOptions {
15+
/** suppliers' offer options interface */
16+
}
17+
18+
const options: ClientOptions = {
1219
/*...*/
1320
};
1421

@@ -54,6 +61,8 @@ client.addEventListener('stop', () => {
5461
Every request structure must follow the generic message data structure proposed by the protocol.
5562

5663
```typescript
64+
import { GenericQuery, GenericOfferOptions } from '@windingtree/sdk';
65+
5766
type GenericQuery = Record<string, unknown>;
5867

5968
/**
@@ -77,40 +86,6 @@ interface RequestData<RequestQuery extends GenericQuery> extends GenericMessage
7786
}
7887
```
7988

80-
The protocol SDK uses the `zod` library for data structures validation. Ready-made data structures validation schemes and static typescript types can be imported from the SDK module.
81-
82-
```typescript
83-
import { z } from 'zod';
84-
import { GenericQuerySchema, createRequestDataSchema, RequestData } from '@windingtree/sdk';
85-
86-
/**
87-
* Custom query schema
88-
*/
89-
const MyCustomRequestQuerySchema = GenericQuerySchema.extend({
90-
howMuch: z.number(),
91-
});
92-
93-
type MyCustomRequestQuery = z.infer<typeof MyCustomRequestQuerySchema>;
94-
95-
const request: RequestData<MyCustomRequestQuery> = {
96-
/** ... */
97-
query: {
98-
howMuch: 10,
99-
},
100-
};
101-
102-
const requestSchema = createRequestDataSchema<typeof MyCustomRequestQuerySchema>(
103-
MyCustomRequestQuerySchema,
104-
);
105-
106-
// the `parse` method of schema is validating an object according to the schema rules
107-
const { query } = requestSchema.parse(request);
108-
console.log(query);
109-
// {
110-
// howMuch: 10,
111-
// }
112-
```
113-
11489
To build a request you can use the `requests.create` method of the client instance.
11590

11691
```typescript
@@ -181,7 +156,10 @@ type RequestRecord<RequestQuery, OfferOptions> = {
181156
const requestRecord = await client.request.get(request.id);
182157
```
183158

184-
- `getAll`: Returns an array of all registered request records
159+
- `create`: Creates the request
160+
- `publish`: Publishes the request
161+
- `get`: Returns a request from the client registry
162+
- `getAll`: Returns an array of all registered request records from the client registry
185163
- `cancel`: Cancels the request subscription. Offers for this request will not be accepted.
186164
- `delete`: Removes the request from the client registry
187165
- `clear`: Removes all requests from the registry

docs/docs/coordinator.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@
55
More about the server configuration options is [here](./index.md#coordination-server).
66

77
```typescript
8-
import { ServerOptions, createServer } from '@windingtree/sdk';
8+
import { ServerOptions, createServer, storage } from '@windingtree/sdk';
99

1010
const options: ServerOptions = {
11-
/*...*/
11+
messagesStorageInit: storage['YOUR_STORAGE_OPTION']
12+
.createInitializer
13+
/** Your storage configuration */
14+
(),
15+
/** Other server configuration options */
1216
};
1317

1418
const server = createServer(options);
19+
20+
server.addEventListener('start', () => {
21+
logger.trace('🚀 Server started at', new Date().toISOString());
22+
});
23+
24+
server.addEventListener('stop', () => {
25+
logger.trace('👋 Server stopped at:', new Date().toISOString());
26+
});
27+
1528
await server.start(); // Start the server
1629
await server.stop(); // Stop the server
1730
```

docs/docs/index.md

+27-52
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ yarn add @windingtree/sdk ethers
3333
The peer key is part of p2p security schema. It uses for peer identification and establishing secured connections between peers.
3434

3535
```typescript
36-
import { generatePeerKey } from '@windingtree.sdk';
36+
import { generatePeerKey } from '@windingtree/sdk';
3737

3838
await generatePeerKey();
3939
/*
@@ -74,7 +74,7 @@ const options: ServerOptions = {
7474
address: '0.0.0.0',
7575
port: 33333,
7676
peerKey,
77-
messagesStorageInit: storage.memoryStorage.init(),
77+
messagesStorageInit: storage.memoryStorage.createInitializer(),
7878
};
7979
```
8080

@@ -83,19 +83,23 @@ const options: ServerOptions = {
8383
A base options type definitions:
8484

8585
```typescript
86-
type NodeOptions<
87-
CustomRequestQuery extends GenericQuery,
88-
CustomOfferOptions extends GenericOfferOptions,
89-
> = {
86+
/** smart contract configuration */
87+
interface ContractConfig {
88+
/** Smart contract name */
89+
name: string;
90+
/** Internal smart contract version */
91+
version: string;
92+
/** Chain Id */
93+
chainId: BigNumberish;
94+
/** Smart contract address */
95+
address: string;
96+
}
97+
98+
type NodeOptions = {
9099
/** Period while the node waits and accepting requests with the same Id */
91100
noncePeriod: number;
92101
/** The protocol smart contract configuration */
93-
contractConfig: {
94-
address: string;
95-
name: string;
96-
version: string;
97-
chainId: string | number | bigint;
98-
};
102+
contractConfig: ContractConfig;
99103
/** Multiaddr of the coordination server */
100104
serverAddress: string;
101105
/** Seed phrase of the node signer wallet */
@@ -104,14 +108,10 @@ type NodeOptions<
104108
topics: string[];
105109
/** Unique supplier Id */
106110
supplierId: string;
107-
/** Query validation schema */
108-
querySchema: ZodType<CustomRequestQuery>;
109-
/** Offer options validation schema instance */
110-
offerOptionsSchema: ZodType<CustomOfferOptions>;
111111
/** Ethers.js provider instance */
112-
provider?: AbstractProvider | undefined;
112+
provider?: AbstractProvider;
113113
/** Additional Libp2p initialization options */
114-
libp2p?: Libp2pOptions | undefined;
114+
libp2p?: Libp2pInit;
115115
};
116116
```
117117

@@ -120,19 +120,9 @@ Here is an example of a supplier node configuration:
120120
```typescript
121121
import { NodeOptions } from '@windingtree/sdk';
122122
import { latLngToCell } from '@windingtree/sdk/utils';
123-
import {
124-
RequestQuerySchema,
125-
OfferOptionsSchema,
126-
RequestQuery,
127-
OfferOptions,
128-
supplierId,
129-
signerSeedPhrase,
130-
coordinates,
131-
} from './config.js';
123+
import { supplierId, signerSeedPhrase, coordinates } from './config.js';
132124

133-
const options: NodeOptions<RequestQuery, OfferOptions> = {
134-
querySchema: RequestQuerySchema, // zod schema
135-
offerOptionsSchema: OfferOptionsSchema, // zod schema
125+
const options: NodeOptions = {
136126
topics: ['hello', latLngToCell(coordinates)],
137127
contractConfig: {
138128
name: 'WtMarket',
@@ -150,31 +140,19 @@ const options: NodeOptions<RequestQuery, OfferOptions> = {
150140
### Client node
151141

152142
```typescript
153-
type ClientOptionsClientOptions<
154-
CustomRequestQuery extends GenericQuery,
155-
CustomOfferOptions extends GenericOfferOptions,
156-
> = {
143+
type ClientOptions = {
157144
/** The protocol smart contract configuration */
158-
contractConfig: {
159-
address: string;
160-
name: string;
161-
version: string;
162-
chainId: string | number | bigint;
163-
};
145+
contractConfig: ContractConfig; // See details in the supplier node section
164146
/** Multiaddr of the coordination server */
165147
serverAddress: string;
166-
/** Query validation schema */
167-
querySchema: ZodType<CustomRequestQuery>;
168-
/** Offer options validation schema instance */
169-
offerOptionsSchema: ZodType<CustomOfferOptions>;
170148
/** Client key-value storage initializer */
171149
storageInitializer: (...args: unknown[]) => Promise<Storage>;
172150
/** Request registry keys prefix */
173151
requestRegistryPrefix: string;
174152
/** Ethers.js provider instance */
175-
provider?: AbstractProvider | undefined;
153+
provider?: AbstractProvider;
176154
/** Additional Libp2p initialization options */
177-
libp2p?: Libp2pOptions | undefined;
155+
libp2p?: Libp2pInit;
178156
};
179157
```
180158

@@ -183,19 +161,16 @@ Here is an example of a client configuration:
183161
```typescript
184162
import { Wallet } from 'ethers';
185163
import { ClientOptions, storage } from '@windingtree/sdk';
186-
import { RequestQuerySchema, OfferOptionsSchema, RequestQuery, OfferOptions } from './config.js';
187164

188-
const options: ClientOptions<RequestQuery, OfferOptions> = {
189-
querySchema: RequestQuerySchema,
190-
offerOptionsSchema: OfferOptionsSchema,
165+
const options: ClientOptions = {
191166
contractConfig: {
192167
name: 'WtMarket',
193168
version: '1',
194169
chainId: '1',
195170
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
196171
},
197172
serverAddress: '/ip4/127.0.0.1/tcp/33333/ws/p2p/QmcXbDr...jgEuWPCf',
198-
storageInitializer: storage.localStorage.init({
173+
storageInitializer: storage.localStorage.createInitializer({
199174
session: true,
200175
}),
201176
requestRegistryPrefix: 'requestsRegistry',
@@ -204,6 +179,6 @@ const options: ClientOptions<RequestQuery, OfferOptions> = {
204179

205180
## SDK development
206181

207-
- WindingTree Discord server, [developers channel](https://discord.com/channels/898350336069218334/956614058323370014)
182+
- [WindingTree protocol discussions](https://github.com/windingtree/sdk/discussions)
208183
- Bug tracker: [https://github.com/windingtree/sdk/issues](https://github.com/windingtree/sdk/issues)
209184
- [Contribution](/docs/contribution.md)

0 commit comments

Comments
 (0)