Skip to content

Commit

Permalink
fix: Disable send button on invalid to address
Browse files Browse the repository at this point in the history
  • Loading branch information
Halibao-Lala committed Oct 21, 2024
1 parent 2b888d5 commit 6372232
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { useState } from 'react';
import { useEffect } from 'react';
import { useCallback } from 'react';

import { useAppSelector } from '@/modules/store/StoreProvider';
import { useTronWeb } from '@/core/hooks/useTronWeb';

export const useTronContract = () => {
const tronWeb = useTronWeb();
const [isTronContract, setIsTronContract] = useState<null | boolean>(null);
const toAccount = useAppSelector((state) => state.transfer.toAccount);

const isTronContractInfo = async (address: string) => {
if (!tronWeb) return;
const isTronContractInfo = useCallback(async () => {
if (!tronWeb || !toAccount?.address) return;
try {
const contractInfo = await tronWeb.trx.getContract(address);
return !!contractInfo?.bytecode;
const contractInfo = await tronWeb.trx.getContract(toAccount?.address);
setIsTronContract(!!contractInfo?.bytecode);
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
setIsTronContract(false);
return false;
}
};
}, [toAccount, tronWeb]);

useEffect(() => {
let mount = true;
if (mount) {
isTronContractInfo();
}
return () => {
mount = false;
};
}, [toAccount, isTronContractInfo]);

return {
isTronContractInfo,
isTronContract,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useGetTronAllowance } from '@/modules/aggregator/adapters/meson/hooks/u
import { useCurrentWallet } from '@/modules/wallet/CurrentWalletProvider';
import { useTronTransferInfo } from '@/modules/transfer/hooks/tron/useTronTransferInfo';
import { utf8ToHex } from '@/core/utils/string';
import { useTronContract } from '@/modules/aggregator/adapters/meson/hooks/useTronContract';

export function TransferButton({
onOpenSubmittedModal,
Expand Down Expand Up @@ -62,6 +63,7 @@ export function TransferButton({
tokenAddress: selectedToken?.address as `0x${string}`,
sender: transferActionInfo?.bridgeAddress as `0x${string}`,
});
const { isTronContract } = useTronContract();

const tronAllowance = useGetTronAllowance();
const { isTronConnected, isEvmConnected } = useCurrentWallet();
Expand Down Expand Up @@ -416,7 +418,8 @@ export function TransferButton({
!Number(sendValue) ||
!transferActionInfo ||
!isTransferable ||
(isTronTransfer && !isToAddressChecked)
(isTronTransfer && !isToAddressChecked) ||
isTronContract === true
}
>
{isApproveNeeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ export function ToAccount(props: FlexProps) {
const dispatch = useAppDispatch();

const [isChecked, setIsChecked] = useState(false);
const [isTronContract, setIsTronContract] = useState<boolean | null>(null);

const toAccount = useAppSelector((state) => state.transfer.toAccount);
const toChain = useAppSelector((state) => state.transfer.toChain);

const { isTronTransfer, isAvailableAccount } = useTronTransferInfo();
const { isTronContractInfo } = useTronContract();
const { isTronContract } = useTronContract();

const timerRef = useRef<any>();
const [inputValue, setInputValue] = useState(toAccount.address);
Expand All @@ -47,10 +46,6 @@ export function ToAccount(props: FlexProps) {
address: value,
}),
);
if (toChain?.chainType === 'tron') {
const result = await isTronContractInfo(value);
setIsTronContract(result === true);
}
}, 500);
};

Expand Down

0 comments on commit 6372232

Please sign in to comment.