Skip to content

Commit 0c341f8

Browse files
committed
feat: 🎸 Deal claim feature added to the Node example
1 parent ef80b8e commit 0c341f8

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

‎examples/node/index.ts

Lines changed: 23 additions & 8 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 } from 'viem';
4+
import { Hash, Hex, zeroAddress } from 'viem';
55
import { hardhat, polygonZkEvmTestnet } from 'viem/chains';
66
import { randomSalt } from '@windingtree/contracts';
77
import {
@@ -13,6 +13,7 @@ import {
1313
} from '../shared/index.js';
1414
import { createNode, Node, NodeOptions, Queue, createJobHandler } from '../../src/index.js';
1515
import { OfferData } from '../../src/shared/types.js';
16+
import { DealStatus } from '../../src/shared/contracts.js';
1617
import { noncePeriod } from '../../src/constants.js';
1718
import { memoryStorage } from '../../src/storage/index.js';
1819
import { nowSec, parseSeconds } from '../../src/utils/time.js';
@@ -58,21 +59,35 @@ process.once('unhandledRejection', (error) => {
5859
*/
5960
interface DealHandlerOptions {
6061
node: Node<RequestQuery, OfferOptions>;
62+
[key: string]: unknown;
6163
}
6264

6365
/**
6466
* This handler looking up for a deal
6567
*/
6668
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 }) => {
6970
logger.trace(`Job "${name}" #${id} Checking for a deal. Offer #${offer.id}`);
70-
// const { node } = options;
7171

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
7691
},
7792
);
7893

0 commit comments

Comments
 (0)