Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/meson #27

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useTronWeb } from '@/core/hooks/useTronWeb';

export const useTronContract = () => {
const tronWeb = useTronWeb();

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

return {
isTronContractInfo,
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, useColorMode, useIntl, useTheme } from '@bnb-chain/space';

import { useAppSelector } from '@/modules/store/StoreProvider';
import { useAppDispatch, useAppSelector } from '@/modules/store/StoreProvider';
import { useFromChains } from '@/modules/aggregator/hooks/useFromChains';
import { VirtualList } from '@/core/components/VirtualList';
import { useSelection } from '@/modules/aggregator/hooks/useSelection';
Expand All @@ -10,6 +10,7 @@ import { BaseModal } from '@/modules/aggregator/components/SelectModal/component
import { useSearch } from '@/modules/aggregator/components/SelectModal/hooks/useSearch';
import { ListItem } from '@/modules/aggregator/components/SelectModal/components/ListItem';
import { reportEvent } from '@/core/utils/gtm';
import { setToAccount } from '@/modules/transfer/action';

interface SourceNetworkModalProps {
isOpen: boolean;
Expand All @@ -19,6 +20,7 @@ interface SourceNetworkModalProps {
export function SourceNetworkModal(props: SourceNetworkModalProps) {
const { isOpen, onClose } = props;
const { formatMessage } = useIntl();
const dispatch = useAppDispatch();

const fromChain = useAppSelector((state) => state.transfer.fromChain);
const toChain = useAppSelector((state) => state.transfer.toChain);
Expand Down Expand Up @@ -67,6 +69,7 @@ export function SourceNetworkModal(props: SourceNetworkModalProps) {
openLink(item.externalBridgeUrl);
} else {
selectFromChain(item);
dispatch(setToAccount({ address: '' }));
onClose();
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ import {
InputRightElement,
} from '@bnb-chain/space';
import { ChangeEvent, useRef, useState } from 'react';
import { useEffect } from 'react';

import { setIsToAddressChecked, setToAccount } from '@/modules/transfer/action';
import { useTronTransferInfo } from '@/modules/transfer/hooks/tron/useTronTransferInfo';
import { ErrorIcon } from '@/core/components/icons/ErrorIcon';
import { CorrectIcon } from '@/core/components/icons/CorrectIcon';
import { useAppDispatch, useAppSelector } from '@/modules/store/StoreProvider';
import { ConfirmCheckbox } from '@/core/components/ConfirmCheckbox';
import { useTronContract } from '@/modules/aggregator/adapters/meson/hooks/useTronContract';

export function ToAccount(props: FlexProps) {
const { colorMode } = useColorMode();
const { formatMessage } = useIntl();
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 timerRef = useRef<any>();
const [inputValue, setInputValue] = useState(toAccount.address);
Expand All @@ -37,20 +41,29 @@ export function ToAccount(props: FlexProps) {
setInputValue(value);

clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => {
timerRef.current = setTimeout(async () => {
dispatch(
setToAccount({
address: value,
}),
);
if (toChain?.chainType === 'tron') {
const result = await isTronContractInfo(value);
setIsTronContract(result === true);
}
}, 500);
};

useEffect(() => {
// Clear input value when toAccount is cleared
if (!toAccount.address) setInputValue('');
}, [toAccount.address]);

if (!isTronTransfer) {
return null;
}

const isInvalid = !isAvailableAccount && !!toAccount.address;
const isInvalid = (!isAvailableAccount && !!toAccount.address) || isTronContract === false;

const onCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.checked === true) {
Expand Down Expand Up @@ -101,11 +114,17 @@ export function ToAccount(props: FlexProps) {
{(isInvalid || isAvailableAccount) && (
<InputRightElement h="100%" w="auto" pr={'16px'} pl={'8px'}>
{isInvalid && <ErrorIcon boxSize={'16px'} />}
{isAvailableAccount && <CorrectIcon boxSize={'16px'} />}
{isAvailableAccount && !isInvalid && <CorrectIcon boxSize={'16px'} />}
</InputRightElement>
)}
</InputGroup>

{isInvalid && (
<Flex mt={'8px'} color={theme.colors[colorMode].text.danger}>
{formatMessage({ id: 'to.section.account.invalid' })}
</Flex>
)}

<ConfirmCheckbox
isChecked={isChecked}
onChange={onCheckboxChange}
Expand All @@ -116,12 +135,6 @@ export function ToAccount(props: FlexProps) {
>
{formatMessage({ id: 'to.section.confirm.text' })}
</ConfirmCheckbox>

{isInvalid && (
<Flex mt={'8px'} color={theme.colors[colorMode].text.danger}>
{formatMessage({ id: 'to.section.account.invalid' })}
</Flex>
)}
</Flex>
);
}
Loading