diff --git a/packages/backend/src/database/schemas/purchase.schema.ts b/packages/backend/src/database/schemas/purchase.schema.ts index 4c7e017..b047356 100644 --- a/packages/backend/src/database/schemas/purchase.schema.ts +++ b/packages/backend/src/database/schemas/purchase.schema.ts @@ -40,6 +40,7 @@ export class Purchase { assetName?: string; industry?: string; riskTier?: string; + type?: 'PURCHASE' | 'DEPOSIT'; // Track if this is a purchase or deposit }; // Timestamps (automatically added by Mongoose with timestamps: true) diff --git a/packages/backend/src/modules/marketplace/dto/notify-purchase.dto.ts b/packages/backend/src/modules/marketplace/dto/notify-purchase.dto.ts index 66490f2..f68edef 100644 --- a/packages/backend/src/modules/marketplace/dto/notify-purchase.dto.ts +++ b/packages/backend/src/modules/marketplace/dto/notify-purchase.dto.ts @@ -1,4 +1,4 @@ -import { IsString, IsNotEmpty, IsNumberString, IsOptional } from 'class-validator'; +import { IsString, IsNotEmpty, IsNumberString, IsOptional, Matches } from 'class-validator'; export class NotifyPurchaseDto { @IsString() @@ -9,9 +9,9 @@ export class NotifyPurchaseDto { @IsNotEmpty() assetId!: string; - @IsNumberString() + @Matches(/^-?\d+$/, { message: 'amount must be a valid number string (can be negative)' }) @IsNotEmpty() - amount!: string; // Token amount purchased (in wei) + amount!: string; // Token amount purchased (in wei) - can be negative @IsOptional() @IsNumberString() diff --git a/packages/backend/src/modules/marketplace/services/purchase-tracker.service.ts b/packages/backend/src/modules/marketplace/services/purchase-tracker.service.ts index fb465bc..cc313d1 100644 --- a/packages/backend/src/modules/marketplace/services/purchase-tracker.service.ts +++ b/packages/backend/src/modules/marketplace/services/purchase-tracker.service.ts @@ -81,6 +81,8 @@ export class PurchaseTrackerService { assetName: `${asset.metadata?.invoiceNumber} - ${asset.metadata?.buyerName}`, industry: asset.metadata?.industry, riskTier: asset.metadata?.riskTier, + type: 'PURCHASE', // Mark as purchase + }, }); diff --git a/packages/backend/src/modules/solvency/controllers/solvency.controller.ts b/packages/backend/src/modules/solvency/controllers/solvency.controller.ts index 0aba9ce..e3c2ce2 100644 --- a/packages/backend/src/modules/solvency/controllers/solvency.controller.ts +++ b/packages/backend/src/modules/solvency/controllers/solvency.controller.ts @@ -244,15 +244,15 @@ export class SolvencyController { throw new BadRequestException('Position does not belong to authenticated user'); } - // Auto-detect token type based on Asset collection - const tokenType = await this.positionService.determineTokenType(positionData.collateralToken); + // Use token type directly from blockchain (0 = RWA, 1 = PRIVATE_ASSET) + const tokenType = positionData.tokenType === 0 ? 'RWA' : 'PRIVATE_ASSET'; // Sync database record (update if exists, create if not) const position = await this.positionService.syncPosition( positionId, positionData.user, positionData.collateralToken, - tokenType, + tokenType as any, positionData.collateralAmount.toString(), positionData.tokenValueUSD.toString(), dto.txHash, diff --git a/packages/backend/src/modules/solvency/services/solvency-blockchain.service.ts b/packages/backend/src/modules/solvency/services/solvency-blockchain.service.ts index 8dc3dc3..af663bd 100644 --- a/packages/backend/src/modules/solvency/services/solvency-blockchain.service.ts +++ b/packages/backend/src/modules/solvency/services/solvency-blockchain.service.ts @@ -154,6 +154,8 @@ export class SolvencyBlockchainService { usdcBorrowed: bigint; tokenValueUSD: bigint; createdAt: bigint; + liquidatedAt: bigint; + creditLineId: bigint; active: boolean; tokenType: number; }> { @@ -169,6 +171,17 @@ export class SolvencyBlockchainService { args: [BigInt(positionId)], }) as any; + this.logger.log(`Position user retrieved for ID ${position[0]}`); + this.logger.log(`Position collateral token: ${position[1]}`); + this.logger.log(`Position collateral amount: ${position[2]}`); + this.logger.log(`Position USDC borrowed: ${position[3]}`); + this.logger.log(`Position token value USD: ${position[4]}`); + this.logger.log(`Position created at: ${position[5]}`); + this.logger.log(`Position liquidated at: ${position[6]}`); + this.logger.log(`Position credit line ID: ${position[7]}`); + this.logger.log(`Position active: ${position[8]}`); + this.logger.log(`Position token type: ${position[9]}`); + return { user: position[0], collateralToken: position[1], @@ -176,8 +189,10 @@ export class SolvencyBlockchainService { usdcBorrowed: position[3], tokenValueUSD: position[4], createdAt: position[5], - active: position[6], - tokenType: position[7], + liquidatedAt: position[6], + creditLineId: position[7], + active: position[8], + tokenType: position[9], }; }