From 7bab3704f3cac11e03cbb1039627430d0e519203 Mon Sep 17 00:00:00 2001 From: okjodom Date: Sat, 9 Nov 2024 11:07:37 +0300 Subject: [PATCH] fix: validating offramp dtos --- apps/swap/src/fedimint/fedimint.service.ts | 5 ++--- apps/swap/src/swap.service.ts | 19 ++++++++++++------- .../common/src/dto/create-offramp-swap.dto.ts | 9 ++++++++- libs/common/src/dto/quote.dto.ts | 6 ++++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/apps/swap/src/fedimint/fedimint.service.ts b/apps/swap/src/fedimint/fedimint.service.ts index beb9a90..b193031 100644 --- a/apps/swap/src/fedimint/fedimint.service.ts +++ b/apps/swap/src/fedimint/fedimint.service.ts @@ -101,8 +101,7 @@ export class FedimintService { } async pay(invoice: string): Promise<{ operationId: string; fee: number }> { - this.logger.log('Paying invoice'); - this.logger.log('Invoice', invoice); + this.logger.log(`Paying Invoice : ${invoice}`); const { operationId, fee }: LightningPayResponse = await this.post< { paymentInfo: string } & WithFederationId & WithGatewayId, @@ -113,7 +112,7 @@ export class FedimintService { gatewayId: this.gatewayId, }); - this.logger.log('Paid Invoice : ', operationId); + this.logger.log(`Paid Invoice : ${invoice} : ${operationId}`); return { operationId, fee, diff --git a/apps/swap/src/swap.service.ts b/apps/swap/src/swap.service.ts index 3fa4ce4..f55b569 100644 --- a/apps/swap/src/swap.service.ts +++ b/apps/swap/src/swap.service.ts @@ -252,7 +252,7 @@ export class SwapService { rate, lightning, retryCount: 0, - reference: ref, + reference: 'ref', phone: target.payout.phone, amountSats: amountSats.toFixed(2), state: SwapTransactionState.PENDING, @@ -399,7 +399,7 @@ export class SwapService { const mpesa = await this.intasendService.getMpesaTrackerFromPaymentUpdate(update); - const swap = await this.onramp.findOne({ + const swap = await this.offramp.findOne({ paymentTracker: update.file_id, }); @@ -423,9 +423,6 @@ export class SwapService { context, operationId, }: ReceivePaymentSuccessEvent) { - this.logger.log('Successfully received payment'); - this.logger.log(`Context : ${context}, OperationId: ${operationId}`); - const swap = await this.offramp.findOne({ _id: operationId }); const amount = Number(swap.amountSats) * Number(swap.rate); @@ -444,6 +441,8 @@ export class SwapService { state: SwapTransactionState.PROCESSING, }, ); + + this.logger.log(`Received lightning payment for ${context} : ${operationId}`); } @OnEvent(fedimint_receive_failure) @@ -451,8 +450,14 @@ export class SwapService { context, operationId, }: ReceivePaymentFailureEvent) { - this.logger.log('Failed to receive payment'); - this.logger.log(`Context : ${context}, OperationId: ${operationId}`); + this.logger.log(`Failed to eceive lightning payment for ${context} : ${operationId}`); + + await this.offramp.findOneAndUpdate( + { _id: operationId }, + { + state: SwapTransactionState.FAILED, + }, + ); } } diff --git a/libs/common/src/dto/create-offramp-swap.dto.ts b/libs/common/src/dto/create-offramp-swap.dto.ts index d0aa25d..48cd596 100644 --- a/libs/common/src/dto/create-offramp-swap.dto.ts +++ b/libs/common/src/dto/create-offramp-swap.dto.ts @@ -6,6 +6,7 @@ import { Validate, IsEnum, IsDefined, + ValidateNested, } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; import { @@ -21,17 +22,21 @@ import { TransformToCurrency } from './transforms'; class OfframpSwapTargetDto implements OfframpSwapTarget { @IsEnum(Currency) - @ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' }) @TransformToCurrency() + @ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' }) currency: Currency; @IsDefined() + @ValidateNested() + @Type(() => MobileMoneyDto) @ApiProperty({ type: MobileMoneyDto }) payout: MobileMoneyDto; } export class CreateOfframpSwapDto implements OfframpSwapRequest { @IsOptional() + @ValidateNested() + @Type(() => QuoteDto) @ApiProperty({ type: QuoteDto }) quote: QuoteDto; @@ -49,6 +54,8 @@ export class CreateOfframpSwapDto implements OfframpSwapRequest { amountFiat: string; @IsNotEmpty() + @ValidateNested() + @Type(() => OfframpSwapTargetDto) @ApiProperty({ type: OfframpSwapTargetDto }) target: OfframpSwapTargetDto; } diff --git a/libs/common/src/dto/quote.dto.ts b/libs/common/src/dto/quote.dto.ts index e74a826..88bb8da 100644 --- a/libs/common/src/dto/quote.dto.ts +++ b/libs/common/src/dto/quote.dto.ts @@ -1,9 +1,15 @@ import { ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; +import { IsBoolean, IsString } from 'class-validator'; export class QuoteDto { + @IsString() + @Type(() => String) @ApiProperty() id: string; + @IsBoolean() + @Type(() => Boolean) @ApiProperty() refreshIfExpired: boolean; }