From d870c48809c264ef5e2f287f72685d4a4e76e6ad Mon Sep 17 00:00:00 2001 From: okjodom Date: Sun, 1 Dec 2024 14:18:32 +0300 Subject: [PATCH] feat: solowallet receive ln payments --- apps/solowallet/src/solowallet.service.ts | 57 ++++++++++++++++++++++- apps/swap/src/swap.controller.spec.ts | 2 +- apps/swap/src/swap.service.spec.ts | 4 +- libs/common/src/types/fedimint.ts | 1 + 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/apps/solowallet/src/solowallet.service.ts b/apps/solowallet/src/solowallet.service.ts index b830001..7a283a5 100644 --- a/apps/solowallet/src/solowallet.service.ts +++ b/apps/solowallet/src/solowallet.service.ts @@ -4,6 +4,8 @@ import { Currency, DepositFundsRequestDto, DepositFundsResponse, + fedimint_receive_failure, + fedimint_receive_success, FedimintService, fiatToBtc, FindUserTxsRequestDto, @@ -12,14 +14,18 @@ import { QuoteDto, QuoteRequestDto, QuoteResponse, + ReceiveContext, + type ReceivePaymentFailureEvent, + type ReceivePaymentSuccessEvent, SWAP_SERVICE_NAME, SwapResponse, SwapServiceClient, TransactionStatus, } from '@bitsacco/common'; -import { SolowalletRepository } from './db'; import { type ClientGrpc } from '@nestjs/microservices'; import { catchError, firstValueFrom, map, of, tap } from 'rxjs'; +import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; +import { SolowalletRepository } from './db'; @Injectable() export class SolowalletService { @@ -29,11 +35,22 @@ export class SolowalletService { constructor( private readonly wallet: SolowalletRepository, private readonly fedimintService: FedimintService, + private readonly eventEmitter: EventEmitter2, @Inject(SWAP_SERVICE_NAME) private readonly swapGrpc: ClientGrpc, ) { this.logger.log('SolowalletService created'); this.swapService = this.swapGrpc.getService(SWAP_SERVICE_NAME); + + this.eventEmitter.on( + fedimint_receive_success, + this.handleSuccessfulReceive.bind(this), + ); + this.eventEmitter.on( + fedimint_receive_failure, + this.handleFailedReceive.bind(this), + ); + this.logger.log('SwapService initialized'); } private async getQuote({ from, to, amount }: QuoteRequestDto): Promise<{ @@ -152,6 +169,7 @@ export class SolowalletService { ...deposit, lightning, id: deposit._id, + status: deposit.status, createdAt: deposit.createdAt.toDateString(), updatedAt: deposit.updatedAt.toDateString(), }; @@ -206,6 +224,9 @@ export class SolowalletService { reference, }); + // listen for payment + this.fedimintService.receive(ReceiveContext.SOLOWALLET, deposit._id); + const deposits = await this.getPaginatedUserDeposits({ userId, pagination: { page: 0, size: 10 }, @@ -223,4 +244,38 @@ export class SolowalletService { }: FindUserTxsRequestDto): Promise { return this.getPaginatedUserDeposits({ userId, pagination }); } + + @OnEvent(fedimint_receive_success) + private async handleSuccessfulReceive({ + context, + operationId, + }: ReceivePaymentSuccessEvent) { + await this.wallet.findOneAndUpdate( + { _id: operationId }, + { + status: TransactionStatus.COMPLETE, + }, + ); + + this.logger.log( + `Received lightning payment for ${context} : ${operationId}`, + ); + } + + @OnEvent(fedimint_receive_failure) + private async handleFailedReceive({ + context, + operationId, + }: ReceivePaymentFailureEvent) { + this.logger.log( + `Failed to eceive lightning payment for ${context} : ${operationId}`, + ); + + await this.wallet.findOneAndUpdate( + { _id: operationId }, + { + state: TransactionStatus.FAILED, + }, + ); + } } diff --git a/apps/swap/src/swap.controller.spec.ts b/apps/swap/src/swap.controller.spec.ts index 0ab10e0..731efc3 100644 --- a/apps/swap/src/swap.controller.spec.ts +++ b/apps/swap/src/swap.controller.spec.ts @@ -48,7 +48,7 @@ describe('SwapController', () => { id: 'dadad-bdjada-dadad', refreshIfExpired: false, }, - ref: 'test-onramp-swap', + reference: 'test-onramp-swap', amountFiat: '100', source: { currency: Currency.KES, diff --git a/apps/swap/src/swap.service.spec.ts b/apps/swap/src/swap.service.spec.ts index 30b86ff..26c283d 100644 --- a/apps/swap/src/swap.service.spec.ts +++ b/apps/swap/src/swap.service.spec.ts @@ -208,7 +208,7 @@ describe.skip('SwapService', () => { id: 'dadad-bdjada-dadad', refreshIfExpired: false, }, - ref: 'test-onramp-swap', + reference: 'test-onramp-swap', amountFiat: '100', source: { currency: Currency.KES, @@ -380,7 +380,7 @@ describe.skip('SwapService', () => { id: 'dadad-bdjada-dadad', refreshIfExpired: false, }, - ref: 'test-onramp-swap', + reference: 'test-onramp-swap', amountFiat: '100', target: { currency: Currency.KES, diff --git a/libs/common/src/types/fedimint.ts b/libs/common/src/types/fedimint.ts index 64ccc24..ad85f9d 100644 --- a/libs/common/src/types/fedimint.ts +++ b/libs/common/src/types/fedimint.ts @@ -2,6 +2,7 @@ export enum ReceiveContext { FUNDING, OFFRAMP, + SOLOWALLET, } export interface ReceivePaymentSuccessEvent {