diff --git a/backend/package.json b/backend/package.json index 601ef50..3e1675e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,6 +19,7 @@ "@fastify/cors": "^9.0.1", "@fastify/multipart": "^8.1.0", "@fastify/sensible": "^5.5.0", + "@fastify/websocket": "^10.0.1", "@golem-sdk/golem-js": "workspace:*", "@golem-sdk/task-executor": "workspace:*", "@types/jsonwebtoken": "^9.0.6", @@ -41,6 +42,7 @@ }, "devDependencies": { "@types/node": "^20.11.20", + "@types/ws": "^8.5.10", "concurrently": "^8.2.2", "nodemon": "^3.1.0", "prisma": "^5.10.2", diff --git a/backend/src/fastify.ts b/backend/src/fastify.ts index ab2db3a..eb923fd 100644 --- a/backend/src/fastify.ts +++ b/backend/src/fastify.ts @@ -9,6 +9,7 @@ import { userService } from "./services/user/routes.js"; import { FastifySSEPlugin } from "fastify-sse-v2"; import fastifyMultipart from "@fastify/multipart"; import { Yagna } from "./services/yagna/routes.js"; +import websocket from '@fastify/websocket' export const startupFastifyServer = async (): Promise => { const fastify = Fastify({ @@ -22,7 +23,7 @@ export const startupFastifyServer = async (): Promise => { fastify.register(fastifySensible); fastify.register(fastifyMultipart); fastify.register(FastifySSEPlugin); - + fastify.register(websocket); fastify.register(cors, { origin: "*", }); diff --git a/backend/src/services/file/routes.ts b/backend/src/services/file/routes.ts index 94d1b11..b2b15da 100644 --- a/backend/src/services/file/routes.ts +++ b/backend/src/services/file/routes.ts @@ -55,14 +55,21 @@ export const fileService = fastifyPlugin( reply.send({ message: "File uploaded" }); }, }); - fastify.get("/scan-result", { - handler: (request, reply) => { + fastify.get("/ws", {websocket : true}, (socket) => { + setInterval( () => { + console.log("sending message") + socket.send("lalala"); + },2000) + } + ), + fastify.get("/scan-result", {websocket : true}, + (socket) => { container.cradle.fileService.resultStream.subscribe((result) => { debugLog("publishing result on sse channel",result); - reply.sse({ data: JSON.stringify(result), id: uuidv4() }); + socket.send(JSON.stringify(result)); }); }, - }); + ); done(); } diff --git a/backend/src/services/payment/routes.ts b/backend/src/services/payment/routes.ts index a5ac045..04123fb 100644 --- a/backend/src/services/payment/routes.ts +++ b/backend/src/services/payment/routes.ts @@ -18,6 +18,7 @@ export const paymentService = fastifyPlugin( //@ts-ignore TODO: add declaration in auth module so ts-ignore is not needed onRequest: [fastify.authenticate], handler: async (request, reply) => { + console.log("wtf creating deposit"); const paymentService = container.cradle.paymentService; // @ts-ignore // TODO: make sure request.body is the right type @@ -26,6 +27,8 @@ export const paymentService = fastifyPlugin( // @ts-ignore request.body.nonce ); + + console.log("res",res); return res; }, }); diff --git a/backend/src/services/payment/service.ts b/backend/src/services/payment/service.ts index a0a2e56..433543e 100644 --- a/backend/src/services/payment/service.ts +++ b/backend/src/services/payment/service.ts @@ -30,10 +30,11 @@ export const paymentService = ( saveDeposit: async (userId: string, nonce: string) => { const userService = container.cradle.userService; const user = await userService.findById(userId); + console.log("saving deposit"); if (!user) { throw new Error(`User not found with id ${userId}`); } - + console.log('has user') const data = await publicClient.readContract({ address: contractAddress, abi: abi, @@ -45,8 +46,8 @@ export const paymentService = ( `Deposit not found with nonce ${nonce} and funder ${user.walletAddress}` ); } + console.log("has data",data); // @ts-ignore - const { amount, feeAmount } = data; const feeRatio = Number(BigDecimal.default.divide(feeAmount, amount)); @@ -59,6 +60,8 @@ export const paymentService = ( if (feeRatio < Number(serviceFee)) { documentDeposit.isValid = false; } + + console.log("eeeee"); userService.addDeposit(userId, documentDeposit); }, }; diff --git a/backend/src/services/user/service.ts b/backend/src/services/user/service.ts index ac1b9e8..57f3e99 100644 --- a/backend/src/services/user/service.ts +++ b/backend/src/services/user/service.ts @@ -100,6 +100,7 @@ export const userService: IUserService = { }, addDeposit: async (userId: string, deposit: Deposit) => { + console.log("adding deposit"); await userModel.updateOne( { _id: userId }, { diff --git a/frontend/src/hooks/useScanResults.tsx b/frontend/src/hooks/useScanResults.tsx index e0a39a3..5f4bdbc 100644 --- a/frontend/src/hooks/useScanResults.tsx +++ b/frontend/src/hooks/useScanResults.tsx @@ -6,10 +6,12 @@ export const useScanResults = () => { const scanResults = useRef([]); const { removeFile } = useFileUploader(); const { data, error } = useSWRSubscription("scanResult", (key, { next }) => { - const eventSource = new EventSource( - `${import.meta.env.VITE_BACKEND_URL}/scan-result` + const eventSource = new WebSocket( + `${import.meta.env.VITE_BACKEND_URL.replace('http','ws')}/scan-result` ); - eventSource.onmessage = (event) => { + + eventSource.addEventListener('message',(event) => { + console.log("event",event); const file = JSON.parse(event.data); const newResults = R.uniqWith( // @ts-ignore @@ -19,7 +21,7 @@ export const useScanResults = () => { scanResults.current = newResults; removeFile(file.id); next(null, newResults); - }; + }); return () => eventSource.close(); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9a8657f..0a39e86 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -39,6 +39,9 @@ importers: '@fastify/sensible': specifier: ^5.5.0 version: 5.5.0 + '@fastify/websocket': + specifier: ^10.0.1 + version: 10.0.1 '@golem-sdk/golem-js': specifier: workspace:* version: link:../golem-js @@ -100,6 +103,9 @@ importers: '@types/node': specifier: ^20.11.20 version: 20.12.4 + '@types/ws': + specifier: ^8.5.10 + version: 8.5.10 concurrently: specifier: ^8.2.2 version: 8.2.2 @@ -3169,6 +3175,17 @@ packages: vary: 1.1.2 dev: false + /@fastify/websocket@10.0.1: + resolution: {integrity: sha512-8/pQIxTPRD8U94aILTeJ+2O3el/r19+Ej5z1O1mXlqplsUH7KzCjAI0sgd5DM/NoPjAi5qLFNIjgM5+9/rGSNw==} + dependencies: + duplexify: 4.1.3 + fastify-plugin: 4.5.1 + ws: 8.16.0 + transitivePeerDependencies: + - bufferutil + - utf-8-validate + dev: false + /@gar/promisify@1.1.3: resolution: {integrity: sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw==} dev: true @@ -3307,7 +3324,7 @@ packages: dependencies: '@jest/fake-timers': 29.7.0 '@jest/types': 29.6.3 - '@types/node': 12.20.55 + '@types/node': 20.12.4 jest-mock: 29.7.0 /@jest/expect-utils@29.7.0: @@ -3331,7 +3348,7 @@ packages: dependencies: '@jest/types': 29.6.3 '@sinonjs/fake-timers': 10.3.0 - '@types/node': 12.20.55 + '@types/node': 20.12.4 jest-message-util: 29.7.0 jest-mock: 29.7.0 jest-util: 29.7.0 @@ -6282,9 +6299,6 @@ packages: /@web3modal/scaffold@4.0.13(@types/react@18.2.74)(react@18.2.0)(valtio@1.13.2): resolution: {integrity: sha512-ur543j3KunFUIoSAT4GGwAX0GxVPHNkuCEUGJL52KSfXG6rqHlPoVMeGyZKN4Mo2FyQCh76vFgm9Vv2GribuDQ==} - peerDependenciesMeta: - '@web3modal/siwe': - optional: true dependencies: '@web3modal/common': 4.0.13 '@web3modal/core': 4.0.13(@types/react@18.2.74)(react@18.2.0) @@ -6345,15 +6359,6 @@ packages: '@wagmi/connectors': '>=4.0.0' '@wagmi/core': '>=2.0.0' viem: '>=2.0.0' - peerDependenciesMeta: - '@web3modal/siwe': - optional: true - react: - optional: true - react-dom: - optional: true - vue: - optional: true dependencies: '@wagmi/connectors': 4.1.24(@types/react@18.2.74)(@wagmi/core@2.6.15)(react-dom@18.2.0)(react-native@0.73.6)(react@18.2.0)(typescript@5.4.3)(viem@2.7.22) '@wagmi/core': 2.6.15(@types/react@18.2.74)(react@18.2.0)(typescript@5.4.3)(viem@2.7.22) @@ -10834,7 +10839,7 @@ packages: '@jest/environment': 29.7.0 '@jest/fake-timers': 29.7.0 '@jest/types': 29.6.3 - '@types/node': 12.20.55 + '@types/node': 20.12.4 jest-mock: 29.7.0 jest-util: 29.7.0 @@ -10905,7 +10910,7 @@ packages: engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: '@jest/types': 29.6.3 - '@types/node': 12.20.55 + '@types/node': 20.12.4 jest-util: 29.7.0 /jest-pnp-resolver@1.2.3(jest-resolve@29.7.0): @@ -11078,7 +11083,7 @@ packages: resolution: {integrity: sha512-eIz2msL/EzL9UFTFFx7jBTkeZfku0yUAyZZZmJ93H2TYEiroIx2PQjEXcwYtYl8zXCxb+PAmA2hLIt/6ZEkPHw==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dependencies: - '@types/node': 12.20.55 + '@types/node': 20.12.4 jest-util: 29.7.0 merge-stream: 2.0.0 supports-color: 8.1.1