Skip to content

Commit 27d52df

Browse files
committed
docs: Updated the SDK docs
1 parent fb3fb64 commit 27d52df

File tree

5 files changed

+137
-75
lines changed

5 files changed

+137
-75
lines changed

docs/availability.md

-60
This file was deleted.

docs/clients.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ Next, we need to create industry-specific TypeScript types for the requests data
3232
```typescript
3333
import { GenericQuery, GenericOfferOptions } from '@windingtree/sdk-types';
3434

35+
// A request model
3536
export interface RequestQuery extends GenericQuery {
3637
greeting: string; // This is a very simple request, just for testing
3738
}
3839

40+
// An offer options model
3941
export interface OfferOptions extends GenericOfferOptions {
4042
date?: string;
4143
buongiorno: boolean; // Yes, we expect that a supplier may propose us a good day
@@ -104,7 +106,7 @@ Now we are able to instantiate managers.
104106

105107
```typescript
106108
import { ClientRequestsManager, ClientDealsManager } from '@windingtree/sdk-client';
107-
import { polygonZkEvmTestnet } from 'viem/chains';
109+
import { gnosisChiado } from 'viem/chains';
108110

109111
const requestsManager = new ClientRequestsManager<
110112
RequestQuery,
@@ -121,7 +123,7 @@ const dealsManager = new ClientDealsManager<
121123
storage: store,
122124
prefix: 'wt_deals_',
123125
checkInterval: '5s', // Interval for deal state change check
124-
chain: polygonZkEvmTestnet, // Target blockchain network we will use
126+
chain: gnosisChiado, // Target blockchain network we will use
125127
contracts: contractsConfig,
126128
publicClient, // See https://viem.sh/docs/clients/public.html
127129
});

docs/index.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
55
The WindingTree Market Protocol facilitates coordination and deal management for buyers and sellers. This SDK provides essential software tools to configure and construct the protocol actors, streamlining the implementation of its workflow. For a comprehensive understanding of the protocol's operation, explore the detailed documentation available at this [link](/docs/protocol.md).
66

7-
## Protocol Smart Contracts - Polygon zkEVM
7+
## Protocol Smart Contracts - Gnosis Chiado
88

99
### Smart Contract Instances
1010

11-
- Config: [0x098b1d12cAfE7315C77b6d308A62ce02806260Ee](https://explorer.public.zkevm-test.net/address/0x098b1d12cAfE7315C77b6d308A62ce02806260Ee/read-proxy#address-tabs) - Protocol configuration smart contract
12-
- EntitiesRegistry: [0x4bB51528C83844b509E1152EEb05260eE1bf60e6](https://explorer.public.zkevm-test.net/address/0x4bB51528C83844b509E1152EEb05260eE1bf60e6/read-proxy#address-tabs) - Protocol identity management
13-
- Market: [0xDd5B6ffB3585E109ECddec5293e31cdc1e9DeD57](https://explorer.public.zkevm-test.net/address/0xDd5B6ffB3585E109ECddec5293e31cdc1e9DeD57/read-proxy#address-tabs) - Protocol entry point
14-
- LIF: [0x4d60F4483BaA654CdAF1c5734D9E6B16735efCF8](https://explorer.public.zkevm-test.net/address/0x4d60F4483BaA654CdAF1c5734D9E6B16735efCF8/read-proxy#address-tabs) - Test version of LIF token
11+
- Config ([0x4556d5C1486d799f67FA96c84F1d0552486CAAF4](https://gnosis-chiado.blockscout.com/address/0x4556d5C1486d799f67FA96c84F1d0552486CAAF4?tab=read_proxy)): the protocol configuration smart contract
12+
- EntitiesRegistry ([0x4EcB659060Da61D795D777bb21BAe3599b301C66](https://gnosis-chiado.blockscout.com/address/0x4EcB659060Da61D795D777bb21BAe3599b301C66?tab=read_proxy)): the protocol identity management
13+
- Market ([0xF54784206A53EF19fd3024D8cdc7A6251A4A0d67](https://gnosis-chiado.blockscout.com/address/0xF54784206A53EF19fd3024D8cdc7A6251A4A0d67?tab=read_proxy)): the protocol entry point
14+
- LIF ([0x8CB96383609C56af1Fe44DB7591F94AEE2fa43b2](https://gnosis-chiado.blockscout.com/address/0x8CB96383609C56af1Fe44DB7591F94AEE2fa43b2?tab=read_proxy)): Test version of LIF token
1515

16-
### Testing Tokens
16+
#### Testing tokens
1717

18-
- `STABLE6`: [ERC20, 6 decimals, no permit](https://explorer.public.zkevm-test.net/address/0x8CB96383609C56af1Fe44DB7591F94AEE2fa43b2/read-proxy#address-tabs)
19-
- `STABLE6PERMIT`: [ERC20, 6 decimals, with permit](https://explorer.public.zkevm-test.net/address/0x4556d5C1486d799f67FA96c84F1d0552486CAAF4/read-proxy#address-tabs)
20-
- `STABLE18`: [ERC20, 18 decimals, no permit](https://explorer.public.zkevm-test.net/address/0x4EcB659060Da61D795D777bb21BAe3599b301C66/read-proxy#address-tabs)
21-
- `STABLE18PERMIT`: [ERC20, 18 decimals, with permit](https://explorer.public.zkevm-test.net/address/0xF54784206A53EF19fd3024D8cdc7A6251A4A0d67/read-proxy#address-tabs)
18+
- `STABLE6`: [ERC20, 6 decimals, no permit](https://gnosis-chiado.blockscout.com/address/0xcC28A4e6DEF318A1b88CF34c4F2Eeb2489995513?tab=read_proxy)
19+
- `STABLE6PERMIT`: [ERC20, 6 decimals, with permit](https://gnosis-chiado.blockscout.com/address/0x78F924A7C70213D502E6110567AC556c2EFBBF73?tab=read_proxy)
20+
- `STABLE18`: [ERC20, 18 decimals, no permit](https://gnosis-chiado.blockscout.com/address/0x7067fC74fFCf4096796454747461D098E6bF7241?tab=read_proxy)
21+
- `STABLE18PERMIT`: [ERC20, 18 decimals, with permit](https://gnosis-chiado.blockscout.com/address/0x44947d69A9F06EF48170BD41Da9B27E74ecd0891?tab=read_proxy)
2222

2323
## Protocol Addresses
2424

@@ -53,10 +53,10 @@ Once you have generated a peer key for your server, you can create a server addr
5353
To generate a server address, use the following script:
5454

5555
```bash
56-
pnpm gen:address --ip 127.0.0.1 --port 3333 --id QmQkjfoGnUGdq...8o75N89opSfgBeM
56+
pnpm gen:address --env local --id QmXH7wT21maJx9TrAk3neP45CeD3X5E8QnPLUH6AEYFxRt --address 127.0.0.1 --port 33333
5757
```
5858

59-
The resulting server address will have the format: `/ip4/127.0.0.1/tcp/3333/ws/p2p/QmQkjfoGnUGdq...8o75N89opSfgBeM`.
59+
The resulting server address will have the format: `/ip4/127.0.0.1/tcp/33333/ws/p2p/QmXH7wT21maJx9TrAk3neP45CeD3X5E8QnPLUH6AEYFxRt`.
6060

6161
The address components are as follows:
6262

docs/nodes.md

+121
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,29 @@ With the `ProtocolContracts` utility, you have a powerful tool to work with the
322322

323323
Tasks for checking deal state changes can be implemented in the same way as monitoring the creation of deals (requests processing). For more details, refer to the previous section on "Requests Processing."
324324

325+
Also, monitoring the status of deals can be performed using the `subscribeMarket` method of the `ProtocolContracts` class. Here is a brief example:
326+
327+
```typescript
328+
interface StatusEventLog {
329+
offerId?: `0x${string}` | undefined;
330+
status?: DealStatus | number | undefined;
331+
sender?: `0x${string}` | undefined;
332+
};
333+
334+
const unsubscribe = contractsManager.subscribeMarket(
335+
'Status', // Event name
336+
async (logs, maxBlockNumber) => {
337+
// Process logs here
338+
logs.forEach((log) => {
339+
const { offerId, status } = log.args as StatusEventLog;
340+
// ...
341+
});
342+
// Save `maxBlockNumber` for later use eq re-subscription
343+
},
344+
blockNumber, // Block number to listen from (0 by default)
345+
);
346+
```
347+
325348
With this information, you have a better understanding of how to configure the supplier node, manage incoming requests, build and publish offers, and handle deal state changes.
326349

327350
## The Node Management API
@@ -370,6 +393,104 @@ const apiServer = new NodeApiServer({
370393
apiServer.start(appRouter);
371394
```
372395

396+
### Extending the Node API
397+
398+
You can add your custom routes to the Node API. When creating API routes for your application, following specific guidelines can facilitate the development, maintenance, and use of your API. Below a guide for creating API routes:
399+
400+
#### 1. **Structure of Routes**
401+
402+
- **Naming**: Name your routes in a way that clearly reflects the actions they perform and the resources they handle. For example, use `add`, `update`, `delete`, `get`, and `getAll` for operations related to airplanes.
403+
- **Grouping**: Organize routes into logical groups. In the example, all routes are grouped under `airplanesRouter`, which simplifies understanding their purpose.
404+
405+
```typescript
406+
export const airplanesRouter = router({
407+
add: authAdminProcedure
408+
// Implementation...
409+
update: authAdminProcedure
410+
// Implementation...
411+
delete: authAdminProcedure
412+
// Implementation...
413+
get: authProcedure
414+
// Implementation...
415+
getAll: authProcedure
416+
// Implementation...
417+
});
418+
```
419+
420+
#### 2. **Authentication and Authorization**
421+
422+
- Differentiate access levels for different operations. In the example, `authProcedure` is used for basic authentication and `authAdminProcedure` for operations requiring admin rights.
423+
- Explicitly specify authentication and authorization requirements for each route.
424+
425+
#### 3. **Input Validation**
426+
427+
- Use validation libraries, such as `zod`, to ensure inputs match expected types and formats. This helps prevent errors and vulnerabilities.
428+
- Define schemas for input data, like `AirplaneInputSchema` and `AirplaneUpdateSchema`, and use them in route procedures.
429+
430+
```typescript
431+
.add: authAdminProcedure
432+
.input(AirplaneInputSchema)
433+
.mutation(async ({ input, ctx }) => {
434+
// Implementation...
435+
}),
436+
```
437+
438+
#### 4. **Error Handling**
439+
440+
- Handle exceptions and errors within each route to return understandable error messages.
441+
- Use standardized HTTP error codes, such as `BAD_REQUEST` for validation errors and `NOT_FOUND` for missing resources.
442+
443+
```typescript
444+
.mutation(async ({ input, ctx }) => {
445+
try {
446+
// Operation...
447+
} catch (error) {
448+
throw new TRPCError({
449+
code: 'BAD_REQUEST',
450+
message: (error as Error).message,
451+
});
452+
}
453+
}),
454+
```
455+
456+
#### 5. **Logging**
457+
458+
- Log key operations and errors. This facilitates debugging and monitoring your API.
459+
- Use a created logger, such as `logger`, to record events.
460+
461+
```typescript
462+
logger.trace(`Airplane ${input.name} registered with id ${id}`);
463+
```
464+
465+
#### Integrate Your Custom Route Into the Node API
466+
467+
The following code snippet demonstrates how to set up and start a Node API server using the `@windingtree/sdk-node-api/server` package, integrating multiple routers for different parts of an application. It includes default routers like `adminRouter`, `dealsRouter`, `serviceRouter`, and `userRouter` provided by the `@windingtree/sdk-node-api/router` package, **along with a custom router defined by the developer (`customRouter`)**. The code creates a combined router that aggregates these individual routers under specific namespaces, making it easier to manage and organize routes according to their functionality. Finally, the code initializes a `NodeApiServer` instance with optional server configuration (`apiServerConfig`) and starts the server with the combined router, making the API ready to handle requests. This approach facilitates the modular development of API services, allowing for scalability and ease of maintenance.
468+
469+
```typescript
470+
import { NodeApiServer, router } from '@windingtree/sdk-node-api/server';
471+
import {
472+
adminRouter,
473+
dealsRouter,
474+
serviceRouter,
475+
userRouter,
476+
} from '@windingtree/sdk-node-api/router';
477+
import { customRouter } from '../api/customRoute.js';
478+
479+
// Create the combined router
480+
const appRouter = router({
481+
service: serviceRouter, // Default route
482+
admin: adminRouter, // Default route
483+
user: userRouter, // Default route
484+
deals: dealsRouter, // Default route
485+
custom: customRouter, // Your custom router
486+
});
487+
488+
// Create the Node API server
489+
const apiServer = new NodeApiServer({ /* apiServerConfig */ });
490+
491+
apiServer.start(appRouter); // Initialize the combined router
492+
```
493+
373494
### Remote Node API Usage
374495

375496
Here's a simple example of how to use the `@windingtree/sdk-node-api/client` in a React application:

docs/sidebar.md

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
- [Clients](clients.md)
77
- [Nodes](nodes.md)
88
- [Storage](storage.md)
9-
- [Availability management](availability.md)
109
- [Contribution](contribution.md)

0 commit comments

Comments
 (0)