Skip to content

Commit

Permalink
feat: solowallet receive ln payments
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Dec 1, 2024
1 parent e7bf69c commit 41228e0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
57 changes: 56 additions & 1 deletion apps/solowallet/src/solowallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
Currency,
DepositFundsRequestDto,
DepositFundsResponse,
fedimint_receive_failure,
fedimint_receive_success,
FedimintService,
fiatToBtc,
FindUserTxsRequestDto,
Expand All @@ -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 {
Expand All @@ -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<SwapServiceClient>(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<{
Expand Down Expand Up @@ -152,6 +169,7 @@ export class SolowalletService {
...deposit,
lightning,
id: deposit._id,
status: deposit.status,
createdAt: deposit.createdAt.toDateString(),
updatedAt: deposit.updatedAt.toDateString(),
};
Expand Down Expand Up @@ -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 },
Expand All @@ -223,4 +244,38 @@ export class SolowalletService {
}: FindUserTxsRequestDto): Promise<PaginatedSolowalletTxsResponse> {
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,
},
);
}
}
2 changes: 1 addition & 1 deletion apps/swap/src/swap.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/swap/src/swap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/types/fedimint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export enum ReceiveContext {
FUNDING,
OFFRAMP,
SOLOWALLET,
}

export interface ReceivePaymentSuccessEvent {
Expand Down

0 comments on commit 41228e0

Please sign in to comment.