@@ -11,9 +11,16 @@ import {
11
11
stableCoins ,
12
12
serverAddress ,
13
13
} from '../shared/index.js' ;
14
- import { createNode , Node , NodeOptions , Queue , createJobHandler } from '../../src/index.js' ;
14
+ import {
15
+ Node ,
16
+ NodeOptions ,
17
+ NodeRequestManager ,
18
+ Queue ,
19
+ createNode ,
20
+ createJobHandler ,
21
+ } from '../../src/index.js' ;
15
22
import { OfferData } from '../../src/shared/types.js' ;
16
- import { DealStatus } from '../../src/shared/contracts.js' ;
23
+ import { DealStatus , ProtocolContracts } from '../../src/shared/contracts.js' ;
17
24
import { noncePeriod } from '../../src/constants.js' ;
18
25
import { memoryStorage } from '../../src/storage/index.js' ;
19
26
import { nowSec , parseSeconds } from '../../src/utils/time.js' ;
@@ -34,7 +41,9 @@ const signerMnemonic = process.env.EXAMPLE_ENTITY_SIGNER_MNEMONIC;
34
41
const signerPk = process . env . EXAMPLE_ENTITY_SIGNER_PK as Hex ;
35
42
36
43
if ( ! signerMnemonic && ! signerPk ) {
37
- throw new Error ( 'Either signerMnemonic or signerPk must be provided with env' ) ;
44
+ throw new Error (
45
+ 'Either signerMnemonic or signerPk must be provided with env' ,
46
+ ) ;
38
47
}
39
48
40
49
/**
@@ -58,38 +67,42 @@ process.once('unhandledRejection', (error) => {
58
67
* This is interface of object that you want to pass to the job handler as options
59
68
*/
60
69
interface DealHandlerOptions {
61
- node : Node < RequestQuery , OfferOptions > ;
62
- [ key : string ] : unknown ;
70
+ contracts : ProtocolContracts ;
63
71
}
64
72
65
73
/**
66
74
* This handler looking up for a deal
67
75
*/
68
- const dealHandler = createJobHandler < OfferData < RequestQuery , OfferOptions > , DealHandlerOptions > (
69
- async ( { name, id, data : offer } , { node } ) => {
70
- logger . trace ( `Job "${ name } " #${ id } Checking for a deal. Offer #${ offer . id } ` ) ;
76
+ const dealHandler = createJobHandler <
77
+ OfferData < RequestQuery , OfferOptions > ,
78
+ DealHandlerOptions
79
+ > ( async ( { name, id, data : offer } , { contracts } ) => {
80
+ logger . trace ( `Job "${ name } " #${ id } Checking for a deal. Offer #${ offer . id } ` ) ;
71
81
72
- if ( node ) {
73
- // Check for a deal
74
- const [ , , , buyer , , , status ] = await node . deals . get ( offer ) ;
82
+ // Check for a deal
83
+ const [ , , , buyer , , , status ] = await contracts . getDeal ( offer ) ;
75
84
76
- // Deal must be exists and not cancelled
77
- if ( buyer !== zeroAddress && status === DealStatus . Created ) {
78
- // check for double booking in the availability system
79
- // If double booking detected - rejects (and refunds) the deal
85
+ // Deal must be exists and not cancelled
86
+ if ( buyer !== zeroAddress && status === DealStatus . Created ) {
87
+ // check for double booking in the availability system
88
+ // If double booking detected - rejects (and refunds) the deal
80
89
81
- // If not detected - claims the deal
82
- await node . deals . claim ( offer , undefined , ( txHash : string , txSubj ?: string ) => {
83
- logger . trace ( `Offer #${ offer . payload . id } ${ txSubj ?? 'claim' } tx hash: ${ txHash } ` ) ;
84
- } ) ;
90
+ // If not detected - claims the deal
91
+ await contracts . claimDeal (
92
+ offer ,
93
+ undefined ,
94
+ ( txHash : string , txSubj ?: string ) => {
95
+ logger . trace (
96
+ `Offer #${ offer . payload . id } ${ txSubj ?? 'claim' } tx hash: ${ txHash } ` ,
97
+ ) ;
98
+ } ,
99
+ ) ;
85
100
86
- return true ; // Returning true means that the job must be stopped
87
- }
88
- }
101
+ return true ; // Returning true means that the job must be stopped
102
+ }
89
103
90
- return ; // Job continuing
91
- } ,
92
- ) ;
104
+ return ; // Job continuing
105
+ } ) ;
93
106
94
107
/**
95
108
* This handler creates offer then publishes it and creates a job for deal handling
@@ -103,7 +116,7 @@ const createRequestsHandler =
103
116
const handler = async ( ) => {
104
117
logger . trace ( `📨 Request on topic #${ detail . topic } :` , detail . data ) ;
105
118
106
- const offer = await node . buildOffer ( {
119
+ const offer = await node . makeOffer ( {
107
120
/** Offer expiration time */
108
121
expire : '15m' ,
109
122
/** Copy of request */
@@ -165,28 +178,17 @@ const createRequestsHandler =
165
178
* @returns {Promise<void> }
166
179
*/
167
180
const main = async ( ) : Promise < void > => {
168
- const storage = await memoryStorage . createInitializer ( ) ( ) ;
169
-
170
- const queue = new Queue ( {
171
- storage,
172
- hashKey : 'jobs' ,
173
- concurrentJobsNumber : 3 ,
174
- } ) ;
175
-
176
181
const options : NodeOptions = {
177
182
topics : [ 'hello' ] ,
178
183
chain,
179
184
contracts : contractsConfig ,
180
185
serverAddress,
181
- noncePeriod : Number ( parseSeconds ( noncePeriod ) ) ,
182
186
supplierId,
183
187
signerSeedPhrase : signerMnemonic ,
184
188
signerPk : signerPk ,
185
189
} ;
186
190
const node = createNode < RequestQuery , OfferOptions > ( options ) ;
187
191
188
- queue . addJobHandler ( 'deal' , dealHandler ( { node } ) ) ;
189
-
190
192
node . addEventListener ( 'start' , ( ) => {
191
193
logger . trace ( '🚀 Node started at' , new Date ( ) . toISOString ( ) ) ;
192
194
} ) ;
@@ -199,7 +201,36 @@ const main = async (): Promise<void> => {
199
201
logger . trace ( '👋 Node stopped at:' , new Date ( ) . toISOString ( ) ) ;
200
202
} ) ;
201
203
202
- node . addEventListener ( 'request' , createRequestsHandler ( node , queue ) ) ;
204
+ const contractsManager = new ProtocolContracts ( {
205
+ contracts : contractsConfig ,
206
+ publicClient : node . publicClient ,
207
+ walletClient : node . walletClient ,
208
+ } ) ;
209
+
210
+ const storage = await memoryStorage . createInitializer ( ) ( ) ;
211
+
212
+ const queue = new Queue ( {
213
+ storage,
214
+ hashKey : 'jobs' ,
215
+ concurrentJobsNumber : 3 ,
216
+ } ) ;
217
+
218
+ const requestManager = new NodeRequestManager < RequestQuery > ( {
219
+ noncePeriod : Number ( parseSeconds ( noncePeriod ) ) ,
220
+ } ) ;
221
+
222
+ requestManager . addEventListener (
223
+ 'request' ,
224
+ createRequestsHandler ( node , queue ) ,
225
+ ) ;
226
+
227
+ node . addEventListener ( 'message' , ( e ) => {
228
+ const { topic, data } = e . detail ;
229
+ // here you are able to pre-validate arrived messages
230
+ requestManager . add ( topic , data ) ;
231
+ } ) ;
232
+
233
+ queue . addJobHandler ( 'deal' , dealHandler ( { contracts : contractsManager } ) ) ;
203
234
204
235
/**
205
236
* Graceful Shutdown handler
0 commit comments