Skip to content

Commit 53c0474

Browse files
authored
Merge pull request #15 from windingtree/refactor/new-offer
New offers types and structure
2 parents 529cdba + 71e7ea8 commit 53c0474

24 files changed

+1828
-3227
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ temp
1111
*.level
1212
coverage
1313
typedoc
14+
typechain

examples/client/package.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
12-
"@types/react": "^18.0.33",
13-
"@types/react-dom": "^18.0.11",
14-
"@vitejs/plugin-react": "^3.1.0",
12+
"@types/react": "^18.2.6",
13+
"@types/react-dom": "^18.2.4",
14+
"@vitejs/plugin-react": "^4.0.0",
1515
"eslint-config-react-app": "^7.0.1",
16-
"eslint-plugin-react-refresh": "^0.3.4",
16+
"eslint-plugin-react-refresh": "^0.4.1",
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0",
19-
"typescript": "^5.0.3",
20-
"vite": "^4.2.1",
21-
"@windingtree/sdk": "^0.1.0-beta.12"
19+
"typescript": "^5.0.4",
20+
"vite": "^4.3.4"
2221
},
2322
"eslintConfig": {
2423
"extends": [

examples/client/src/App.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useState, useEffect, useRef } from 'react';
2+
import { RequestQuery, OfferOptions, contractConfig, serverAddress } from '../../shared/index.js';
23
import {
3-
RequestQuery,
4-
OfferOptions,
5-
contractConfig,
6-
serverAddress,
7-
} from '../../shared/index.js';
8-
import { Client, ClientOptions, RequestRecord, createClient, storage, utils } from '@windingtree/sdk';
4+
Client,
5+
ClientOptions,
6+
RequestRecord,
7+
createClient,
8+
storage,
9+
utils,
10+
} from '../../../src/index.js'; //@windingtree/sdk
911

1012
/** Default request expiration time */
1113
const defaultExpire = '30s';
@@ -146,9 +148,7 @@ export const Requests = ({ requests, subscribed, onClear, onCancel }: RequestsPr
146148
export const App = () => {
147149
const client = useRef<Client<RequestQuery, OfferOptions> | undefined>();
148150
const [connected, setConnected] = useState<boolean>(false);
149-
const [requests, setRequests] = useState<RequestsRegistryRecord[]>(
150-
[],
151-
);
151+
const [requests, setRequests] = useState<RequestsRegistryRecord[]>([]);
152152
const [error, setError] = useState<string | undefined>();
153153

154154
/** This hook starts the client that will be available via `client.current` */
@@ -237,6 +237,7 @@ export const App = () => {
237237

238238
client.current.requests.publish(request);
239239
} catch (error) {
240+
console.log('@@@', error);
240241
setError((error as Error).message);
241242
}
242243
};

examples/client/vite.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ export default defineConfig({
77
/** @ignore */
88
react(),
99
],
10+
optimizeDeps: {
11+
exclude: ['ethers'],
12+
},
1013
});

examples/client/yarn.lock

+479-2,161
Large diffs are not rendered by default.

examples/node/index.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ import { EventHandler } from '@libp2p/interfaces/events';
22
import { ZeroAddress } from 'ethers';
33
import { DateTime } from 'luxon';
44
import { RequestQuery, OfferOptions, contractConfig, serverAddress } from '../shared/index.js';
5-
import {
6-
createNode,
7-
Node,
8-
NodeOptions,
9-
Queue,
10-
OfferData,
11-
createJobHandler,
12-
} from '../../src/index.js';
5+
import { createNode, Node, NodeOptions, Queue, createJobHandler } from '../../src/index.js';
6+
import { OfferData } from '../../src/shared/types.js';
137
import { noncePeriod } from '../../src/constants.js';
148
import { memoryStorage } from '../../src/storage/index.js';
159
import { nowSec, parseSeconds } from '../../src/utils/time.js';
16-
import { supplierId as generateSupplierId, randomSalt, simpleUid } from '../../src/utils/uid.js';
10+
import { supplierId as generateSupplierId, randomSalt } from '../../src/utils/uid.js';
1711
import { generateMnemonic, deriveAccount } from '../../src/utils/wallet.js';
1812
import { RequestEvent } from '../../src/node/requestManager.js';
1913
import { createLogger } from '../../src/utils/logger.js';
@@ -94,7 +88,7 @@ const createRequestsHandler =
9488
*/
9589
payment: [
9690
{
97-
id: simpleUid(),
91+
id: randomSalt(),
9892
price: '1',
9993
asset: ZeroAddress,
10094
},
@@ -108,6 +102,7 @@ const createRequestsHandler =
108102
],
109103
/** Check-in time */
110104
checkIn: nowSec() + 1000,
105+
checkOut: nowSec() + 2000,
111106
});
112107

113108
queue.addEventListener('expired', ({ detail: job }) => {
@@ -119,7 +114,7 @@ const createRequestsHandler =
119114
* So, we add a job for detection of deals
120115
*/
121116
queue.addJob('deal', offer, {
122-
expire: offer.expire,
117+
expire: Number(offer.expire),
123118
every: 5000, // 5 sec
124119
});
125120
};
@@ -145,7 +140,7 @@ const main = async (): Promise<void> => {
145140
topics: ['hello'],
146141
contractConfig,
147142
serverAddress,
148-
noncePeriod: parseSeconds(noncePeriod),
143+
noncePeriod: Number(parseSeconds(noncePeriod)),
149144
supplierId,
150145
signerSeedPhrase: signerMnemonic,
151146
};

examples/shared/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { GenericQuery, GenericOfferOptions } from '../../src/index.js';
1+
import { GenericQuery, GenericOfferOptions } from '../../src/shared/types.js';
22
import { ContractConfig } from '../../src/utils/contract.js';
33

44
export interface RequestQuery extends GenericQuery {
55
greeting: string;
66
}
77

88
export interface OfferOptions extends GenericOfferOptions {
9-
date: string;
9+
date: string | null;
1010
buongiorno: boolean;
1111
buonasera: boolean;
1212
}

package.json

+49-29
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,29 @@
2525
"type": "module",
2626
"types": "./lib/index.d.ts",
2727
"main": "./lib/index.js",
28+
"typesVersions": {
29+
"*": {
30+
"*": [
31+
"*",
32+
"lib/*",
33+
"lib/*/index"
34+
]
35+
}
36+
},
2837
"files": [
29-
"lib/**/*",
38+
"src",
39+
"lib",
3040
"!**/*.tsbuildinfo"
3141
],
32-
"scripts": {
33-
"clean": "rm -rf ./lib",
34-
"build": "npm run clean && tsc -p ./tsconfig.build.json",
35-
"test": "mocha -t 60000 --extension spec.ts",
36-
"semantic-release": "semantic-release",
37-
"prepack": "npm run build",
38-
"lint": "eslint . --ext .ts",
39-
"lint:fix": "eslint . --ext .ts --fix && prettier --write .",
40-
"prepare": "husky install",
41-
"commit": "git-cz -S",
42-
"coverage": "c8 --all --exclude coverage --exclude lib --exclude test yarn test && c8 report --all --exclude coverage --exclude lib --exclude test -r html",
43-
"example:server": "node --experimental-specifier-resolution=node --loader ts-node/esm ./examples/server/index.ts",
44-
"example:client": "yarn --cwd ./examples/client dev",
45-
"example:node": "node --experimental-specifier-resolution=node --loader ts-node/esm ./examples/node/index.ts",
46-
"typedoc": "typedoc --options ./typedoc.json"
42+
"exports": {
43+
".": {
44+
"types": "./lib/index.d.ts",
45+
"import": "./lib/index.js"
46+
},
47+
"./utils": {
48+
"types": "./lib/utils/index.d.ts",
49+
"import": "./lib/utils/index.js"
50+
}
4751
},
4852
"release": {
4953
"branches": [
@@ -70,29 +74,29 @@
7074
"devDependencies": {
7175
"@types/node": "^18.15.11",
7276
"@types/mocha": "^10.0.1",
73-
"@types/chai": "^4.3.4",
77+
"@types/chai": "^4.3.5",
7478
"@types/debug": "^4.1.7",
7579
"@types/chai-as-promised": "^7.1.5",
76-
"@types/luxon": "^3.2.0",
80+
"@types/luxon": "^3.3.0",
7781
"mocha": "^10.2.0",
7882
"chai": "^4.3.7",
7983
"chai-as-promised": "^7.1.1",
8084
"ts-node": "^10.9.1",
81-
"typescript": "^5.0.3",
82-
"semantic-release": "^21.0.1",
85+
"typescript": "^5.0.4",
86+
"semantic-release": "^21.0.2",
8387
"semantic-release-cli": "^5.4.4",
8488
"@semantic-release/changelog": "^6.0.3",
85-
"eslint": "^8.37.0",
86-
"@typescript-eslint/eslint-plugin": "^5.57.0",
87-
"@typescript-eslint/parser": "^5.57.0",
88-
"prettier": "^2.8.7",
89+
"eslint": "^8.40.0",
90+
"@typescript-eslint/eslint-plugin": "^5.59.5",
91+
"@typescript-eslint/parser": "^5.59.5",
92+
"prettier": "^2.8.8",
8993
"husky": "^8.0.3",
9094
"git-cz": "^4.9.0",
91-
"@commitlint/config-conventional": "^17.4.4",
92-
"@commitlint/cli": "^17.5.1",
93-
"lint-staged": "^13.2.0",
95+
"@commitlint/config-conventional": "^17.6.3",
96+
"@commitlint/cli": "^17.6.3",
97+
"lint-staged": "^13.2.2",
9498
"c8": "^7.13.0",
95-
"typedoc": "^0.24.0-beta.7"
99+
"typedoc": "^0.24.7"
96100
},
97101
"dependencies": {
98102
"libp2p": "^0.43.3",
@@ -103,6 +107,22 @@
103107
"ethers": "^6.2.3",
104108
"luxon": "^3.3.0",
105109
"h3-js": "^4.1.0",
106-
"debug": "^4.3.4"
110+
"debug": "^4.3.4",
111+
"@windingtree/contracts": "^1.0.0-beta.5"
112+
},
113+
"scripts": {
114+
"clean": "rm -rf ./lib",
115+
"build": "npm run clean && tsc -p ./tsconfig.build.json",
116+
"test": "mocha -t 60000 --extension spec.ts",
117+
"semantic-release": "semantic-release",
118+
"lint": "eslint . --ext .ts",
119+
"lint:fix": "eslint . --ext .ts --fix && prettier --write .",
120+
"prepare": "husky install",
121+
"commit": "git-cz -S",
122+
"coverage": "c8 --all --exclude coverage --exclude lib --exclude test yarn test && c8 report --all --exclude coverage --exclude lib --exclude test -r html",
123+
"example:server": "node --experimental-specifier-resolution=node --loader ts-node/esm ./examples/server/index.ts",
124+
"example:client": "yarn --cwd ./examples/client dev",
125+
"example:node": "node --experimental-specifier-resolution=node --loader ts-node/esm ./examples/node/index.ts",
126+
"typedoc": "typedoc --options ./typedoc.json"
107127
}
108128
}

src/client/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
12
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events';
23
import { createLibp2p, Libp2pOptions, Libp2p } from 'libp2p';
34
import { noise } from '@chainsafe/libp2p-noise';
@@ -9,15 +10,15 @@ import { peerIdFromString } from '@libp2p/peer-id';
910
import { PeerId } from '@libp2p/interface-peer-id';
1011
import { OPEN } from '@libp2p/interface-connection/status';
1112
import { AbstractProvider } from 'ethers';
12-
import { centerSub, CenterSub } from '../shared/pubsub.js';
1313
import {
14-
buildRequest,
1514
BuildRequestOptions,
1615
OfferData,
1716
GenericOfferOptions,
1817
GenericQuery,
1918
RequestData,
20-
} from '../shared/messages.js';
19+
} from '../shared/types.js';
20+
import { centerSub, CenterSub } from '../shared/pubsub.js';
21+
import { buildRequest } from '../shared/messages.js';
2122
import { ClientOptions } from '../shared/options.js';
2223
import { RequestRecord, RequestsRegistry } from './requestsRegistry.js';
2324
import { decodeText } from '../utils/text.js';

src/client/requestsRegistry.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events';
22
import { Client } from '../index.js';
3-
import { GenericOfferOptions, GenericQuery, RequestData, OfferData } from '../shared/messages.js';
3+
import { GenericOfferOptions, GenericQuery, RequestData, OfferData } from '../shared/types.js';
44
import { Storage } from '../storage/index.js';
55
import { createLogger } from '../utils/logger.js';
66
import { encodeText } from '../utils/text.js';
77
import { nowSec } from '../utils/time.js';
8+
import { stringify } from '../utils/hash.js';
89

910
const logger = createLogger('RequestsRegistry');
1011

@@ -272,9 +273,9 @@ export class RequestsRegistry<
272273
throw new Error('Client not connected to the coordination server yet');
273274
}
274275

275-
const now = nowSec();
276+
const now = BigInt(nowSec());
276277

277-
if (record.data.expire < nowSec() || record.cancelled) {
278+
if (BigInt(record.data.expire) < nowSec() || record.cancelled) {
278279
return;
279280
}
280281

@@ -288,7 +289,7 @@ export class RequestsRegistry<
288289
}),
289290
);
290291
this._unsubscribe(record.data.id);
291-
}, (record.data.expire - now) * 1000),
292+
}, Number((BigInt(record.data.expire) - now) * BigInt(1000))),
292293
);
293294
this.dispatchEvent(
294295
new CustomEvent<string>('subscribe', {
@@ -339,7 +340,7 @@ export class RequestsRegistry<
339340
this._subscribe(requestRecord);
340341

341342
this.client.libp2p.pubsub
342-
.publish(request.topic, encodeText(JSON.stringify(request)))
343+
.publish(request.topic, encodeText(stringify(request)))
343344
.then(() => {
344345
this.dispatchEvent(
345346
new CustomEvent<string>('published', {

src/node/index.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ import { PeerId } from '@libp2p/interface-peer-id';
1010
import { peerIdFromString } from '@libp2p/peer-id';
1111
import { AbstractProvider, AbstractSigner, Wallet } from 'ethers';
1212
import { noncePeriod as defaultNoncePeriod } from '../constants.js';
13-
import {
14-
buildOffer,
15-
BuildOfferOptions,
16-
GenericOfferOptions,
17-
GenericQuery,
18-
OfferData,
19-
} from '../shared/messages.js';
13+
import { GenericOfferOptions, GenericQuery, OfferData } from '../shared/types.js';
14+
import { buildOffer, BuildOfferOptions } from '../shared/messages.js';
2015
import { CenterSub, centerSub } from '../shared/pubsub.js';
2116
import { RequestManager, RequestEvent } from './requestManager.js';
2217
import { decodeText, encodeText } from '../utils/text.js';
2318
import { ContractConfig } from '../utils/contract.js';
2419
import { NodeOptions } from '../shared/options.js';
2520
import { parseSeconds } from '../utils/time.js';
2621
import { createLogger } from '../utils/logger.js';
22+
import { stringify } from '../utils/hash.js';
2723

2824
const logger = createLogger('Node');
2925

@@ -156,7 +152,7 @@ export class Node<
156152

157153
this.serverPeerId = peerIdFromString(serverPeerIdString);
158154
this.requestManager = new RequestManager<CustomRequestQuery>({
159-
noncePeriod: parseSeconds(noncePeriod ?? defaultNoncePeriod),
155+
noncePeriod: Number(parseSeconds(noncePeriod ?? defaultNoncePeriod)),
160156
});
161157
this.requestManager.addEventListener('request', (e) => this.handleRequest(e));
162158
logger.trace('Node instantiated');
@@ -261,7 +257,7 @@ export class Node<
261257
});
262258
logger.trace(`Offer #${offer.id} is built`);
263259

264-
await this.libp2p.pubsub.publish(offer.request.id, encodeText(JSON.stringify(offer)));
260+
await this.libp2p.pubsub.publish(offer.request.id, encodeText(stringify(offer)));
265261
logger.trace(`Offer #${offer.id} is published`);
266262

267263
return offer;

src/node/requestManager.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events';
2-
import { RequestData, GenericQuery } from '../shared/messages.js';
2+
import { RequestData, GenericQuery } from '../shared/types.js';
33
import { RequestManagerOptions } from '../shared/options.js';
44
import { isExpired, nowSec, parseSeconds } from '../utils/time.js';
55
import { createLogger } from '../utils/logger.js';
@@ -40,7 +40,7 @@ export class RequestManager<CustomRequestQuery extends GenericQuery> extends Eve
4040

4141
this.cache = new Map<string, RequestData<CustomRequestQuery>>(); // requestId => request
4242
this.cacheTopic = new Map<string, string>(); // requestId => topic
43-
this.noncePeriod = parseSeconds(noncePeriod);
43+
this.noncePeriod = Number(parseSeconds(noncePeriod));
4444
}
4545

4646
add(topic: string, data: string) {
@@ -51,7 +51,7 @@ export class RequestManager<CustomRequestQuery extends GenericQuery> extends Eve
5151
return;
5252
}
5353

54-
if (nowSec() + this.noncePeriod > requestData.expire) {
54+
if (BigInt(nowSec() + this.noncePeriod) > BigInt(requestData.expire)) {
5555
logger.trace(`Request #${requestData.id} will expire before it can bee processed`);
5656
return;
5757
}

0 commit comments

Comments
 (0)