Skip to content

Commit

Permalink
fix: validating offramp dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 9, 2024
1 parent 487ef5f commit 7bab370
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 2 additions & 3 deletions apps/swap/src/fedimint/fedimint.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});

Expand All @@ -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);
Expand All @@ -444,15 +441,23 @@ export class SwapService {
state: SwapTransactionState.PROCESSING,
},
);

this.logger.log(`Received lightning payment for ${context} : ${operationId}`);
}

@OnEvent(fedimint_receive_failure)
private async handleFailedReceive({
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,
},
);
}
}

Expand Down
9 changes: 8 additions & 1 deletion libs/common/src/dto/create-offramp-swap.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Validate,
IsEnum,
IsDefined,
ValidateNested,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import {
Expand All @@ -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;

Expand All @@ -49,6 +54,8 @@ export class CreateOfframpSwapDto implements OfframpSwapRequest {
amountFiat: string;

@IsNotEmpty()
@ValidateNested()
@Type(() => OfframpSwapTargetDto)
@ApiProperty({ type: OfframpSwapTargetDto })
target: OfframpSwapTargetDto;
}
6 changes: 6 additions & 0 deletions libs/common/src/dto/quote.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7bab370

Please sign in to comment.