@@ -57,6 +57,8 @@ export const CryptoPayForm = ({
5757 const { user } = useAppSelector ( ( state ) => state . auth ) ;
5858 const [ paymentTokenRate , setPaymentTokenRate ] = useState < number > ( 0 ) ;
5959 const [ fundTokenRate , setFundTokenRate ] = useState < number > ( 0 ) ;
60+ const [ decimals , setDecimals ] = useState < number > ( 6 ) ;
61+ const [ tokenDecimals , setTokenDecimals ] = useState < number > ( 18 ) ;
6062
6163 useEffect ( ( ) => {
6264 const fetchJobLauncherData = async ( ) => {
@@ -91,6 +93,19 @@ export const CryptoPayForm = ({
9193 fetchRates ( ) ;
9294 } , [ paymentTokenSymbol , fundTokenSymbol ] ) ;
9395
96+ useEffect ( ( ) => {
97+ if ( amount ) {
98+ const [ integerPart , decimalPart ] = amount . split ( '.' ) ;
99+ if ( decimalPart && decimalPart . length > decimals ) {
100+ setAmount ( `${ integerPart } .${ decimalPart . slice ( 0 , decimals ) } ` ) ;
101+ }
102+ }
103+ } , [ decimals , amount ] ) ;
104+
105+ useEffect ( ( ) => {
106+ setDecimals ( Math . min ( tokenDecimals , 6 ) ) ;
107+ } , [ tokenDecimals ] ) ;
108+
94109 const { data : jobLauncherFee } = useReadContract ( {
95110 address : NETWORKS [ jobRequest . chainId ! ] ?. kvstoreAddress as Address ,
96111 abi : KVStoreABI ,
@@ -113,13 +128,17 @@ export const CryptoPayForm = ({
113128 if ( ! amount ) return 0 ;
114129 const amountDecimal = new Decimal ( amount ) ;
115130 const feeDecimal = new Decimal ( jobLauncherFee as string ) . div ( 100 ) ;
116- return Decimal . max ( minFeeToken , amountDecimal . mul ( feeDecimal ) ) . toNumber ( ) ;
117- } , [ amount , minFeeToken , jobLauncherFee ] ) ;
131+ return Number (
132+ Decimal . max ( minFeeToken , amountDecimal . mul ( feeDecimal ) ) . toFixed ( decimals ) ,
133+ ) ;
134+ } , [ amount , jobLauncherFee , minFeeToken , decimals ] ) ;
118135
119136 const totalAmount = useMemo ( ( ) => {
120137 if ( ! amount ) return 0 ;
121- return new Decimal ( amount ) . plus ( feeAmount ) . toNumber ( ) ;
122- } , [ amount , feeAmount ] ) ;
138+ return Number (
139+ new Decimal ( amount ) . plus ( feeAmount ) . toNumber ( ) . toFixed ( decimals ) ,
140+ ) ;
141+ } , [ amount , decimals , feeAmount ] ) ;
123142
124143 const totalUSDAmount = useMemo ( ( ) => {
125144 if ( ! totalAmount || ! paymentTokenRate ) return 0 ;
@@ -135,7 +154,7 @@ export const CryptoPayForm = ({
135154
136155 const fundAmount = useMemo ( ( ) => {
137156 if ( ! amount || ! conversionRate ) return 0 ;
138- return new Decimal ( amount ) . mul ( conversionRate ) . toNumber ( ) ;
157+ return Number ( new Decimal ( amount ) . mul ( conversionRate ) ) ;
139158 } , [ amount , conversionRate ] ) ;
140159
141160 const currentBalance = useMemo ( ( ) => {
@@ -146,24 +165,19 @@ export const CryptoPayForm = ({
146165 ) ;
147166 } , [ user , paymentTokenSymbol ] ) ;
148167
149- const accountAmount = useMemo (
150- ( ) => new Decimal ( currentBalance ) ,
151- [ currentBalance ] ,
152- ) ;
153-
154168 const balancePayAmount = useMemo ( ( ) => {
155- if ( ! payWithAccountBalance ) return new Decimal ( 0 ) ;
169+ if ( ! payWithAccountBalance ) return 0 ;
156170 const totalAmountDecimal = new Decimal ( totalAmount ) ;
157- if ( totalAmountDecimal . lessThan ( accountAmount ) ) return totalAmountDecimal ;
158- return accountAmount ;
159- } , [ payWithAccountBalance , totalAmount , accountAmount ] ) ;
171+ if ( totalAmountDecimal . lessThan ( currentBalance ) ) return totalAmountDecimal ;
172+ return currentBalance ;
173+ } , [ payWithAccountBalance , totalAmount , currentBalance ] ) ;
160174
161175 const walletPayAmount = useMemo ( ( ) => {
162- if ( ! payWithAccountBalance ) return new Decimal ( totalAmount ) ;
176+ if ( ! payWithAccountBalance ) return totalAmount ;
163177 const totalAmountDecimal = new Decimal ( totalAmount ) ;
164- if ( totalAmountDecimal . lessThan ( accountAmount ) ) return new Decimal ( 0 ) ;
165- return totalAmountDecimal . minus ( accountAmount ) ;
166- } , [ payWithAccountBalance , totalAmount , accountAmount ] ) ;
178+ if ( totalAmountDecimal . lessThan ( currentBalance ) ) return 0 ;
179+ return Number ( totalAmountDecimal . minus ( currentBalance ) ) ;
180+ } , [ payWithAccountBalance , totalAmount , currentBalance ] ) ;
167181
168182 const handlePay = async ( ) => {
169183 if (
@@ -176,14 +190,14 @@ export const CryptoPayForm = ({
176190 ) {
177191 setIsLoading ( true ) ;
178192 try {
179- if ( walletPayAmount . greaterThan ( 0 ) ) {
193+ if ( walletPayAmount > 0 ) {
180194 const hash = await signer . writeContract ( {
181195 address : paymentTokenAddress as Address ,
182196 abi : HMTokenABI ,
183197 functionName : 'transfer' ,
184198 args : [
185199 jobLauncherAddress ,
186- ethers . parseUnits ( walletPayAmount . toString ( ) , 18 ) ,
200+ ethers . parseUnits ( walletPayAmount . toString ( ) , tokenDecimals ) ,
187201 ] ,
188202 } ) ;
189203
@@ -294,16 +308,32 @@ export const CryptoPayForm = ({
294308 value = { paymentTokenSymbol }
295309 label = { 'Payment token' }
296310 labelId = { 'payment-token' }
297- onTokenChange = { ( symbol , address ) => {
311+ onTokenChange = { ( symbol , address , decimals ) => {
298312 setPaymentTokenSymbol ( symbol ) ;
299313 setPaymentTokenAddress ( address ) ;
314+ 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+ }
300324 } }
301325 />
302326 < FormControl fullWidth >
303327 < TextField
304328 value = { amount }
305329 type = "number"
306- onChange = { ( e ) => setAmount ( e . target . value as string ) }
330+ onChange = { ( e ) => {
331+ let value = e . target . value ;
332+ const regex = new RegExp ( `^\\d*\\.?\\d{0,${ decimals } }$` ) ;
333+ if ( regex . test ( value ) ) {
334+ setAmount ( value ) ;
335+ }
336+ } }
307337 placeholder = "Amount"
308338 />
309339 </ FormControl >
@@ -333,8 +363,9 @@ export const CryptoPayForm = ({
333363 >
334364 < Typography > Balance</ Typography >
335365 < Typography color = "text.secondary" >
336- ~ { currentBalance ?. toFixed ( 2 ) ?? '0' } { ' ' }
337- { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
366+ { paymentTokenSymbol
367+ ? `${ Number ( currentBalance ?. toFixed ( 6 ) ) } ${ paymentTokenSymbol ?. toUpperCase ( ) } `
368+ : '' }
338369 </ Typography >
339370 </ Box >
340371 < Box
@@ -351,7 +382,9 @@ export const CryptoPayForm = ({
351382 >
352383 < Typography > Amount</ Typography >
353384 < Typography color = "text.secondary" >
354- { amount } { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
385+ { paymentTokenSymbol
386+ ? `${ amount } ${ paymentTokenSymbol ?. toUpperCase ( ) } `
387+ : '' }
355388 </ Typography >
356389 </ Stack >
357390 < Stack
@@ -361,8 +394,10 @@ export const CryptoPayForm = ({
361394 >
362395 < Typography > Fee</ Typography >
363396 < Typography color = "text.secondary" >
364- ({ Number ( jobLauncherFee ) } %) { feeAmount } { ' ' }
365- { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
397+ ({ Number ( jobLauncherFee ) } %){ ' ' }
398+ { paymentTokenSymbol
399+ ? `${ Number ( feeAmount . toFixed ( 6 ) ) } ${ paymentTokenSymbol ?. toUpperCase ( ) } `
400+ : '' }
366401 </ Typography >
367402 </ Stack >
368403 < Stack
@@ -372,8 +407,9 @@ export const CryptoPayForm = ({
372407 >
373408 < Typography > Total payment</ Typography >
374409 < Typography >
375- { totalAmount } { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' } { ' ' }
376- { `(~${ totalUSDAmount . toFixed ( 2 ) } USD)` }
410+ { paymentTokenSymbol
411+ ? `${ Number ( totalAmount ?. toFixed ( 6 ) ) } ${ paymentTokenSymbol ?. toUpperCase ( ) } (~${ totalUSDAmount . toFixed ( 2 ) } USD)`
412+ : '' }
377413 </ Typography >
378414 </ Stack >
379415 </ Stack >
@@ -388,8 +424,9 @@ export const CryptoPayForm = ({
388424 >
389425 < Typography color = "text.secondary" > Balance</ Typography >
390426 < Typography color = "text.secondary" >
391- { balancePayAmount . toString ( ) } { ' ' }
392- { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
427+ { paymentTokenSymbol
428+ ? `${ Number ( balancePayAmount ?. toFixed ( 6 ) ) } ${ paymentTokenSymbol ?. toUpperCase ( ) } `
429+ : '' }
393430 </ Typography >
394431 </ Stack >
395432 < Stack
@@ -399,8 +436,9 @@ export const CryptoPayForm = ({
399436 >
400437 < Typography color = "text.secondary" > Crypto Wallet</ Typography >
401438 < Typography color = "text.secondary" >
402- { walletPayAmount . toString ( ) } { ' ' }
403- { paymentTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
439+ { paymentTokenSymbol
440+ ? `${ Number ( walletPayAmount ?. toFixed ( 6 ) ) } ${ paymentTokenSymbol ?. toUpperCase ( ) } `
441+ : '' }
404442 </ Typography >
405443 </ Stack >
406444 </ Stack >
@@ -415,7 +453,9 @@ export const CryptoPayForm = ({
415453 >
416454 < Typography > Fund Amount</ Typography >
417455 < Typography color = "text.secondary" >
418- { fundAmount } { fundTokenSymbol ?. toUpperCase ( ) ?? 'HMT' }
456+ { fundTokenSymbol && fundAmount
457+ ? `${ Number ( fundAmount ?. toFixed ( 6 ) ) } ${ fundTokenSymbol ?. toUpperCase ( ) } `
458+ : '' }
419459 </ Typography >
420460 </ Box >
421461 </ Box >
0 commit comments