Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,12 +17,19 @@ export class IsValidTokenDecimalsConstraint
const [tokenProperty] = args.constraints;
const dto = args.object as Record<string, any>;
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) {
Expand All @@ -35,15 +42,20 @@ export class IsValidTokenDecimalsConstraint
return false;
}

const [_, decimals] = value.toString().split('.');
const [, decimals] = value.toString().split('.');
return !decimals || decimals.length <= maxDecimals;
}

defaultMessage(args: ValidationArguments) {
const [tokenProperty] = args.constraints;
const dto = args.object as Record<string, any>;
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';

Expand Down
Loading