Skip to content

Commit

Permalink
feat: Input upper limit checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Halibao-Lala committed Jan 9, 2025
1 parent 0043ef1 commit faddfa8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/canonical-bridge-sdk/src/shared/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const formatNumber = (
useGrouping = true
) => {
const num = removeAfterDecimals(value, decimals);
return num.toLocaleString('fullwide', {
return Number(num).toLocaleString('fullwide', {
maximumFractionDigits: decimals,
useGrouping,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/canonical-bridge-widget/src/core/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const formatNumber = (value: number, decimals = 18, useGrouping = true) => {
const num = removeAfterDecimals(value, decimals);
return num.toLocaleString('fullwide', {
return Number(num).toLocaleString('fullwide', {
maximumFractionDigits: decimals,
useGrouping,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const LayerZeroOption = () => {
? `${formatNumber(
Number(formatUnits(BigInt(estimatedAmount?.['layerZero']), getToDecimals()['layerZero'])),
8,
false,
)}`
: '--';
}, [estimatedAmount, toTokenInfo, sendValue, getToDecimals]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import { useAppSelector } from '@/modules/store/StoreProvider';
import { useSolanaBalance } from '@/modules/wallet/hooks/useSolanaBalance';
import { MIN_SOL_TO_ENABLED_TX } from '@/core/constants';
import { useIsWalletCompatible } from '@/modules/wallet/hooks/useIsWalletCompatible';
import { useTokenUpperLimit } from '@/modules/aggregator/hooks/useTokenUpperLimit';
import { useBridgeConfig } from '@/index';

export const useInputValidation = () => {
const { data } = useSolanaBalance();
const isWalletCompatible = useIsWalletCompatible();
const {
transfer: { dollarUpperLimit },
} = useBridgeConfig();
const solBalance = Number(data?.formatted);
const fromChain = useAppSelector((state) => state.transfer.fromChain);
const selectedToken = useAppSelector((state) => state.transfer.selectedToken);

const priceInfo = useTokenUpperLimit(selectedToken);
const validateInput = useCallback(
({
balance,
Expand All @@ -39,6 +47,15 @@ export const useInputValidation = () => {
isError: true,
};
}
// Check upper limit
if (priceInfo?.upperLimit && Number(value) >= Number(priceInfo?.upperLimit)) {
return {
text: `Transfer value over $${formatNumber(dollarUpperLimit)} (${formatNumber(
priceInfo.upperLimit,
)} ${selectedToken?.symbol}) or equivalent is not allowed`,
isError: true,
};
}
// check if send amount is greater than token balance
if (!!balance && value > balance) {
return { text: `You have insufficient balance`, isError: true };
Expand Down Expand Up @@ -71,7 +88,14 @@ export const useInputValidation = () => {
console.log(e);
}
},
[fromChain?.chainType, solBalance, isWalletCompatible],
[
fromChain?.chainType,
solBalance,
isWalletCompatible,
priceInfo,
dollarUpperLimit,
selectedToken?.symbol,
],
);

return {
Expand Down

0 comments on commit faddfa8

Please sign in to comment.