1
1
import 'dotenv/config' ;
2
2
import { EventHandler } from '@libp2p/interfaces/events' ;
3
3
import { DateTime } from 'luxon' ;
4
- import { Hash , Hex } from 'viem' ;
4
+ import { Hash , Hex , zeroAddress } from 'viem' ;
5
5
import { hardhat , polygonZkEvmTestnet } from 'viem/chains' ;
6
6
import { randomSalt } from '@windingtree/contracts' ;
7
7
import {
@@ -13,6 +13,7 @@ import {
13
13
} from '../shared/index.js' ;
14
14
import { createNode , Node , NodeOptions , Queue , createJobHandler } from '../../src/index.js' ;
15
15
import { OfferData } from '../../src/shared/types.js' ;
16
+ import { DealStatus } from '../../src/shared/contracts.js' ;
16
17
import { noncePeriod } from '../../src/constants.js' ;
17
18
import { memoryStorage } from '../../src/storage/index.js' ;
18
19
import { nowSec , parseSeconds } from '../../src/utils/time.js' ;
@@ -58,21 +59,35 @@ process.once('unhandledRejection', (error) => {
58
59
*/
59
60
interface DealHandlerOptions {
60
61
node : Node < RequestQuery , OfferOptions > ;
62
+ [ key : string ] : unknown ;
61
63
}
62
64
63
65
/**
64
66
* This handler looking up for a deal
65
67
*/
66
68
const dealHandler = createJobHandler < OfferData < RequestQuery , OfferOptions > , DealHandlerOptions > (
67
- // eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
68
- async ( { name, id, data : offer } , options ) => {
69
+ async ( { name, id, data : offer } , { node } ) => {
69
70
logger . trace ( `Job "${ name } " #${ id } Checking for a deal. Offer #${ offer . id } ` ) ;
70
- // const { node } = options;
71
71
72
- // Makes request to the smart contract, checks for a deal
73
- // If the deal is found - check for double booking in the availability system
74
- // If double booking detected - rejects (and refunds) the deal
75
- // 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
76
91
} ,
77
92
) ;
78
93
0 commit comments