Skip to content

Commit 3fd6672

Browse files
committed
refactor: enhance payment forms with dynamic decimal handling and validation
1 parent 3a1a3cd commit 3fd6672

8 files changed

Lines changed: 70 additions & 25 deletions

File tree

packages/apps/job-launcher/client/src/components/Jobs/Create/CryptoPayForm.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@ export const CryptoPayForm = ({
312312
setPaymentTokenSymbol(symbol);
313313
setPaymentTokenAddress(address);
314314
setTokenDecimals(decimals);
315+
if (amount) {
316+
const maxDecimals = Math.min(decimals, 6);
317+
const [integerPart, decimalPart] = amount.split('.');
318+
if (decimalPart && decimalPart.length > maxDecimals) {
319+
setAmount(
320+
`${integerPart}.${decimalPart.slice(0, maxDecimals)}`,
321+
);
322+
}
323+
}
315324
}}
316325
/>
317326
<FormControl fullWidth>
@@ -320,7 +329,8 @@ export const CryptoPayForm = ({
320329
type="number"
321330
onChange={(e) => {
322331
let value = e.target.value;
323-
if (/^\d*\.?\d{0,6}$/.test(value)) {
332+
const regex = new RegExp(`^\\d*\\.?\\d{0,${decimals}}$`);
333+
if (regex.test(value)) {
324334
setAmount(value);
325335
}
326336
}}

packages/apps/job-launcher/client/src/components/Jobs/Create/FiatPayForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export const FiatPayForm = ({
330330
type="number"
331331
onChange={(e) => {
332332
let value = e.target.value;
333-
if (/^\d*\.?\d{0,6}$/.test(value)) {
333+
if (/^\d*\.?\d{0,2}$/.test(value)) {
334334
setAmount(value);
335335
}
336336
}}

packages/apps/job-launcher/client/src/components/TopUpAccount/CryptoTopUpForm.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ export const CryptoTopUpForm = () => {
6060
setTokenSymbol(symbol);
6161
setTokenAddress(address);
6262
setTokenDecimals(decimals);
63+
64+
if (amount) {
65+
const maxDecimals = Math.min(decimals, 6);
66+
const [integerPart, decimalPart] = amount.split('.');
67+
if (decimalPart && decimalPart.length > maxDecimals) {
68+
setAmount(`${integerPart}.${decimalPart.slice(0, maxDecimals)}`);
69+
}
70+
}
6371
};
6472

6573
const handleTopUpAccount = async () => {
@@ -148,7 +156,9 @@ export const CryptoTopUpForm = () => {
148156
value={amount}
149157
onChange={(e) => {
150158
let value = e.target.value;
151-
if (/^\d*\.?\d{0,6}$/.test(value)) {
159+
const maxDecimals = Math.min(tokenDecimals, 6);
160+
const regex = new RegExp(`^\\d*\\.?\\d{0,${maxDecimals}}$`);
161+
if (regex.test(value)) {
152162
setAmount(value);
153163
}
154164
}}

packages/apps/job-launcher/client/src/components/TopUpAccount/FiatTopUpForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const FiatTopUpForm = () => {
147147
type="number"
148148
onChange={(e) => {
149149
let value = e.target.value;
150-
if (/^\d*\.?\d{0,6}$/.test(value)) {
150+
if (/^\d*\.?\d{0,2}$/.test(value)) {
151151
setAmount(value);
152152
}
153153
}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ChainId } from '@human-protocol/sdk';
2+
import { TOKEN_ADDRESSES } from '../constants/tokens';
3+
import { EscrowFundToken } from '../enums/job';
4+
5+
export function getTokenDecimals(
6+
chainId: ChainId,
7+
symbol: EscrowFundToken,
8+
defaultDecimals = 6,
9+
): number {
10+
return Math.min(
11+
TOKEN_ADDRESSES[chainId]?.[symbol]?.decimals ?? defaultDecimals,
12+
defaultDecimals,
13+
);
14+
}

packages/apps/job-launcher/server/src/modules/job/job.service.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import { WhitelistService } from '../whitelist/whitelist.service';
117117
import { UserEntity } from '../user/user.entity';
118118
import { RoutingProtocolService } from '../routing-protocol/routing-protocol.service';
119119
import { TOKEN_ADDRESSES } from '../../common/constants/tokens';
120+
import { getTokenDecimals } from '../../common/utils/tokens';
120121

121122
@Injectable()
122123
export class JobService {
@@ -799,21 +800,14 @@ export class JobService {
799800
dto.escrowFundToken,
800801
);
801802

802-
const paymentTokenDecimals = Math.min(
803-
(
804-
TOKEN_ADDRESSES[chainId]?.[dto.paymentCurrency as EscrowFundToken] ?? {
805-
decimals: 6,
806-
}
807-
).decimals,
808-
6,
803+
const paymentTokenDecimals = getTokenDecimals(
804+
chainId,
805+
dto.paymentCurrency as EscrowFundToken,
809806
);
810-
const fundTokenDecimals = Math.min(
811-
(
812-
TOKEN_ADDRESSES[chainId]?.[dto.escrowFundToken as EscrowFundToken] ?? {
813-
decimals: 6,
814-
}
815-
).decimals,
816-
6,
807+
808+
const fundTokenDecimals = getTokenDecimals(
809+
chainId,
810+
dto.escrowFundToken as EscrowFundToken,
817811
);
818812

819813
const paymentCurrencyFee = Number(

packages/apps/job-launcher/server/src/modules/payment/payment.controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ import {
4444
PaymentFiatConfirmDto,
4545
PaymentFiatCreateDto,
4646
PaymentMethodIdDto,
47+
TokensResponseDto,
4748
UserBalanceDto,
4849
} from './payment.dto';
4950
import { PaymentService } from './payment.service';
51+
import { TokenDto } from './payment.dto';
5052

5153
@ApiBearerAuth()
5254
@UseGuards(JwtAuthGuard)
@@ -440,19 +442,16 @@ export class PaymentController {
440442
@ApiResponse({
441443
status: 200,
442444
description: 'Tokens retrieved successfully',
443-
type: [Object],
445+
type: [TokenDto],
444446
})
445447
@ApiResponse({
446448
status: 400,
447449
description: 'Bad Request. Invalid chainId.',
448450
})
449451
@Get('/tokens/:chainId')
450-
public async getTokens(@Param('chainId') chainId: ChainId): Promise<{
451-
[key: string]: {
452-
address: string;
453-
decimals: number;
454-
};
455-
}> {
452+
public async getTokens(
453+
@Param('chainId') chainId: ChainId,
454+
): Promise<TokensResponseDto> {
456455
const tokens = TOKEN_ADDRESSES[chainId];
457456
if (!tokens) {
458457
throw new ControlledError(

packages/apps/job-launcher/server/src/modules/payment/payment.dto.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,21 @@ export class UserBalanceDto {
242242
@ApiProperty({ name: 'total_usd_amount' })
243243
totalUsdAmount: number;
244244
}
245+
246+
export class TokenDto {
247+
@ApiProperty({
248+
description: 'The address of the token contract',
249+
example: '0x1234567890abcdef1234567890abcdef12345678',
250+
})
251+
address: string;
252+
253+
@ApiProperty({
254+
description: 'The number of decimals used by the token',
255+
example: 18,
256+
})
257+
decimals: number;
258+
}
259+
260+
export class TokensResponseDto {
261+
[key: string]: TokenDto;
262+
}

0 commit comments

Comments
 (0)