Skip to content

Commit c47bd83

Browse files
committed
feat: 🎸 Added the Node API initialization logic to the example
1 parent e72ad8a commit c47bd83

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

‎examples/node/index.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dotenv/config';
22
import { EventHandler } from '@libp2p/interfaces/events';
33
import { DateTime } from 'luxon';
4-
import { Hash, Hex, zeroAddress } from 'viem';
4+
import { Address, Hash, Hex, zeroAddress } from 'viem';
55
import { hardhat, polygonZkEvmTestnet } from 'viem/chains';
66
import { randomSalt } from '@windingtree/contracts';
77
import {
@@ -25,6 +25,12 @@ import { noncePeriod } from '../../src/constants.js';
2525
import { memoryStorage } from '../../src/storage/index.js';
2626
import { nowSec, parseSeconds } from '../../src/utils/time.js';
2727
import { RequestEvent } from '../../src/node/requestManager.js';
28+
import {
29+
router,
30+
userRouter,
31+
adminRouter,
32+
NodeApiServer,
33+
} from '../../src/node/index.js';
2834
import { createLogger } from '../../src/utils/logger.js';
2935

3036
const logger = createLogger('NodeMain');
@@ -57,6 +63,17 @@ if (!supplierId) {
5763
throw new Error('Entity Id must be provided with EXAMPLE_ENTITY_ID env');
5864
}
5965

66+
/**
67+
* The Ethereum account address of the entity owner (supplier)
68+
*/
69+
const entityOwnerAddress = process.env.EXAMPLE_ENTITY_OWNER_ADDRESS as Address;
70+
71+
if (!entityOwnerAddress) {
72+
throw new Error(
73+
'Entity owner address must be provided with EXAMPLE_ENTITY_OWNER_ADDRESS env',
74+
);
75+
}
76+
6077
/** Handles UFOs */
6178
process.once('unhandledRejection', (error) => {
6279
logger.trace('🛸 Unhandled rejection', error);
@@ -116,7 +133,7 @@ const dealHandler = createJobHandler<
116133
},
117134
);
118135

119-
return false; // Returning true means that the job must be stopped
136+
return false; // Returning false means that the job must be stopped
120137
}
121138

122139
return true; // Job continuing
@@ -228,10 +245,36 @@ const main = async (): Promise<void> => {
228245
walletClient: node.walletClient,
229246
});
230247

231-
const storage = await memoryStorage.createInitializer()();
248+
const queueStorage = await memoryStorage.createInitializer({
249+
scope: 'queue',
250+
})();
251+
const usersStorage = await memoryStorage.createInitializer({
252+
scope: 'users',
253+
})();
254+
const dealsStorage = await memoryStorage.createInitializer({
255+
scope: 'deals',
256+
})();
257+
258+
const apiServer = new NodeApiServer({
259+
usersStorage,
260+
dealsStorage,
261+
prefix: 'test',
262+
port: 3456,
263+
secret: 'secret',
264+
ownerAccount: entityOwnerAddress,
265+
protocolContracts: contractsManager,
266+
});
267+
268+
apiServer.start(
269+
router({
270+
user: userRouter,
271+
admin: adminRouter,
272+
// You can add your own routes here
273+
}),
274+
);
232275

233276
const queue = new Queue({
234-
storage,
277+
storage: queueStorage,
235278
idsKeyName: 'jobsIds',
236279
concurrencyLimit: 3,
237280
});
@@ -262,6 +305,7 @@ const main = async (): Promise<void> => {
262305
*/
263306
const shutdown = () => {
264307
const stopHandler = async () => {
308+
await apiServer.stop();
265309
await node.stop();
266310
};
267311
stopHandler()

0 commit comments

Comments
 (0)