11import 'dotenv/config' ;
22import { EventHandler } from '@libp2p/interfaces/events' ;
33import { DateTime } from 'luxon' ;
4- import { Hash , Hex } from 'viem' ;
4+ import { Hash , Hex , zeroAddress } from 'viem' ;
5+ import { hardhat , polygonZkEvmTestnet } from 'viem/chains' ;
56import { randomSalt } from '@windingtree/contracts' ;
67import {
78 RequestQuery ,
89 OfferOptions ,
9- chainConfig ,
10+ contractsConfig ,
1011 stableCoins ,
1112 serverAddress ,
1213} from '../shared/index.js' ;
1314import { createNode , Node , NodeOptions , Queue , createJobHandler } from '../../src/index.js' ;
1415import { OfferData } from '../../src/shared/types.js' ;
16+ import { DealStatus } from '../../src/shared/contracts.js' ;
1517import { noncePeriod } from '../../src/constants.js' ;
1618import { memoryStorage } from '../../src/storage/index.js' ;
1719import { nowSec , parseSeconds } from '../../src/utils/time.js' ;
@@ -20,6 +22,11 @@ import { createLogger } from '../../src/utils/logger.js';
2022
2123const logger = createLogger ( 'NodeMain' ) ;
2224
25+ /**
26+ * Chain config
27+ */
28+ const chain = process . env . LOCAL_NODE === 'true' ? hardhat : polygonZkEvmTestnet ;
29+
2330/**
2431 * The supplier signer credentials
2532 */
@@ -52,21 +59,35 @@ process.once('unhandledRejection', (error) => {
5259 */
5360interface DealHandlerOptions {
5461 node : Node < RequestQuery , OfferOptions > ;
62+ [ key : string ] : unknown ;
5563}
5664
5765/**
5866 * This handler looking up for a deal
5967 */
6068const dealHandler = createJobHandler < OfferData < RequestQuery , OfferOptions > , DealHandlerOptions > (
61- // eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
62- async ( { name, id, data : offer } , options ) => {
69+ async ( { name, id, data : offer } , { node } ) => {
6370 logger . trace ( `Job "${ name } " #${ id } Checking for a deal. Offer #${ offer . id } ` ) ;
64- // const { node } = options;
6571
66- // Makes request to the smart contract, checks for a deal
67- // If the deal is found - check for double booking in the availability system
68- // If double booking detected - rejects (and refunds) the deal
69- // If not detected - claims the deal
72+ if ( node ) {
73+ // Check for a deal
74+ const [ , , , buyer , , , status ] = await node . deals . get ( offer ) ;
75+
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
80+
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+ } ) ;
85+
86+ return true ; // Returning true means that the job must be stopped
87+ }
88+ }
89+
90+ return ; // Job continuing
7091 } ,
7192) ;
7293
@@ -154,7 +175,8 @@ const main = async (): Promise<void> => {
154175
155176 const options : NodeOptions = {
156177 topics : [ 'hello' ] ,
157- chain : chainConfig ,
178+ chain,
179+ contracts : contractsConfig ,
158180 serverAddress,
159181 noncePeriod : Number ( parseSeconds ( noncePeriod ) ) ,
160182 supplierId,
0 commit comments