Skip to content

Commit

Permalink
Refactor input handling in Transfer component
Browse files Browse the repository at this point in the history
  • Loading branch information
shibatales committed Jan 10, 2024
1 parent 64cb3f6 commit 77a69ce
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/routes/xtransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,17 @@ const Transfer = () => {
// Remove all non-numeric characters except for the decimal point
const sanitizedInput = e.replace(/[^\d.]/g, '');

// Remove leading zeroes after the tens digit
const inputWithoutLeadingZeroes = sanitizedInput.replace(/^0+([0-9]+)/, '$1');

// Format the available balance with 12 decimals
const formattedBalance = new BigNumber(availableBalance.toString()).dividedBy(new BigNumber(10).pow(12));

if (sanitizedInput === '') {
setAmount('0');
} else if (Number(sanitizedInput) >= 0 && Number(sanitizedInput) <= formattedBalance.toNumber()) {
setAmount(sanitizedInput);
} else if (Number(sanitizedInput) > formattedBalance.toNumber()) {
if (inputWithoutLeadingZeroes === '') {
setAmount('');
} else if (Number(inputWithoutLeadingZeroes) >= 0 && Number(inputWithoutLeadingZeroes) <= formattedBalance.toNumber()) {
setAmount(inputWithoutLeadingZeroes);
} else if (Number(inputWithoutLeadingZeroes) > formattedBalance.toNumber()) {
setAmount(formattedBalance.toString());
}
};
Expand Down Expand Up @@ -469,10 +472,6 @@ const Transfer = () => {
};
}, [api, apiBasilisk, selectedAccount, setupSubscriptions]);

useEffect(() => {
setAmount("0");
}, [pair.from, pair.to]);

return (
<div className="mx-auto w-full flex max-w-7xl flex-col justify-between p-4 sm:px-6 lg:px-8 mt-14 md:mt-0 gap-3">
<div className="z-10 w-full">
Expand Down

0 comments on commit 77a69ce

Please sign in to comment.