From 6372232cfb573aa525760f5db9136f8fd5093b5e Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 21 Oct 2024 21:15:47 +1000 Subject: [PATCH] fix: Disable send button on invalid to address --- .../adapters/meson/hooks/useTronContract.ts | 30 +++++++++++++++---- .../components/Button/TransferButton.tsx | 5 +++- .../transfer/components/ToAccount/index.tsx | 7 +---- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/canonical-bridge-widget/src/modules/aggregator/adapters/meson/hooks/useTronContract.ts b/packages/canonical-bridge-widget/src/modules/aggregator/adapters/meson/hooks/useTronContract.ts index bb98d879..23123ef8 100644 --- a/packages/canonical-bridge-widget/src/modules/aggregator/adapters/meson/hooks/useTronContract.ts +++ b/packages/canonical-bridge-widget/src/modules/aggregator/adapters/meson/hooks/useTronContract.ts @@ -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); + 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, }; }; diff --git a/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx b/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx index 2593b169..ea72d1e1 100644 --- a/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx +++ b/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx @@ -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, @@ -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(); @@ -416,7 +418,8 @@ export function TransferButton({ !Number(sendValue) || !transferActionInfo || !isTransferable || - (isTronTransfer && !isToAddressChecked) + (isTronTransfer && !isToAddressChecked) || + isTronContract === true } > {isApproveNeeded diff --git a/packages/canonical-bridge-widget/src/modules/transfer/components/ToAccount/index.tsx b/packages/canonical-bridge-widget/src/modules/transfer/components/ToAccount/index.tsx index 9edfc1c8..0130967e 100644 --- a/packages/canonical-bridge-widget/src/modules/transfer/components/ToAccount/index.tsx +++ b/packages/canonical-bridge-widget/src/modules/transfer/components/ToAccount/index.tsx @@ -26,13 +26,12 @@ export function ToAccount(props: FlexProps) { const dispatch = useAppDispatch(); const [isChecked, setIsChecked] = useState(false); - const [isTronContract, setIsTronContract] = useState(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(); const [inputValue, setInputValue] = useState(toAccount.address); @@ -47,10 +46,6 @@ export function ToAccount(props: FlexProps) { address: value, }), ); - if (toChain?.chainType === 'tron') { - const result = await isTronContractInfo(value); - setIsTronContract(result === true); - } }, 500); };