From 70f6bb198a3b7209c78b77061df8490aa69f6f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20L=C3=B3pez?= Date: Thu, 22 May 2025 11:59:17 +0200 Subject: [PATCH] Refactor token validation to use PaymentCurrency enum and add support for USD decimal checks --- .../src/common/validators/token-decimals.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/apps/job-launcher/server/src/common/validators/token-decimals.ts b/packages/apps/job-launcher/server/src/common/validators/token-decimals.ts index 44d1340c9f..94ed779493 100644 --- a/packages/apps/job-launcher/server/src/common/validators/token-decimals.ts +++ b/packages/apps/job-launcher/server/src/common/validators/token-decimals.ts @@ -7,7 +7,7 @@ import { } from 'class-validator'; import { TOKEN_ADDRESSES } from '../constants/tokens'; import { ChainId } from '@human-protocol/sdk'; -import { EscrowFundToken } from '../enums/job'; +import { PaymentCurrency } from '../enums/payment'; @ValidatorConstraint({ async: false }) export class IsValidTokenDecimalsConstraint @@ -17,12 +17,19 @@ export class IsValidTokenDecimalsConstraint const [tokenProperty] = args.constraints; const dto = args.object as Record; const chainId = dto.chainId as ChainId; - const token = dto[tokenProperty] as EscrowFundToken; + const token = dto[tokenProperty] as PaymentCurrency; if (!chainId || !token) { return false; } + // Check if the token is a fiat currency + if (token === PaymentCurrency.USD) { + if (typeof value !== 'number') return false; + const [, decimals] = value.toString().split('.'); + return !decimals || decimals.length <= 2; + } + const tokenInfo = TOKEN_ADDRESSES[chainId]?.[token]; if (!tokenInfo) { @@ -35,7 +42,7 @@ export class IsValidTokenDecimalsConstraint return false; } - const [_, decimals] = value.toString().split('.'); + const [, decimals] = value.toString().split('.'); return !decimals || decimals.length <= maxDecimals; } @@ -43,7 +50,12 @@ export class IsValidTokenDecimalsConstraint const [tokenProperty] = args.constraints; const dto = args.object as Record; const chainId = dto.chainId as ChainId; - const token = dto[tokenProperty] as EscrowFundToken; + const token = dto[tokenProperty] as PaymentCurrency; + + if (token === PaymentCurrency.USD) { + return `${args.property} must have at most 2 decimal places for USD payments.`; + } + const tokenInfo = TOKEN_ADDRESSES[chainId]?.[token]; const maxDecimals = tokenInfo?.decimals || 'unknown';