Skip to content

Commit

Permalink
fix: transfer notification for tron in twa
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev committed Jan 6, 2025
1 parent a61c951 commit 5c9036b
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions apps/twa/src/components/transfer/SendNotifications.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TransferInitParams } from '@tonkeeper/core/dist/AppSdk';
import { BLOCKCHAIN_NAME } from '@tonkeeper/core/dist/entries/crypto';
import { AssetAmount } from '@tonkeeper/core/dist/entries/crypto/asset/asset-amount';
import { toTronAsset } from '@tonkeeper/core/dist/entries/crypto/asset/constants';
import { jettonToTonAsset } from '@tonkeeper/core/dist/entries/crypto/asset/ton-asset';
import { RecipientData } from '@tonkeeper/core/dist/entries/send';
import {
Expand Down Expand Up @@ -34,7 +33,7 @@ import { useAppSdk } from '@tonkeeper/uikit/dist/hooks/appSdk';
import { openIosKeyboard } from '@tonkeeper/uikit/dist/hooks/ios';
import { useTranslation } from '@tonkeeper/uikit/dist/hooks/translation';
import { useJettonList } from '@tonkeeper/uikit/dist/state/jetton';
import { useTronBalances } from '@tonkeeper/uikit/dist/state/tron/tron';
import { useActiveTronWallet, useTronBalances } from "@tonkeeper/uikit/dist/state/tron/tron";
import BigNumber from 'bignumber.js';
import { FC, PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { CSSTransition, TransitionGroup } from 'react-transition-group';
Expand All @@ -52,6 +51,8 @@ import {
RecipientTwaHeaderBlock
} from './SendNotificationHeader';
import { useAnalyticsTrack } from '@tonkeeper/uikit/dist/hooks/amplitude';
import { TRON_USDT_ASSET } from "@tonkeeper/core/dist/entries/crypto/asset/constants";
import { seeIfValidTronAddress } from "@tonkeeper/core/dist/utils/common";

const Body = styled.div`
padding: 0 16px 16px;
Expand Down Expand Up @@ -93,10 +94,10 @@ const SendContent: FC<{
}
}, []);

const { data: tronBalances } = useTronBalances();

const { mutateAsync: getAccountAsync, isLoading: isAccountLoading } = useGetToAccount();

const activeTronWallet = useActiveTronWallet();

const setRecipient = (value: RecipientData) => {
if (
amountViewState?.token?.blockchain &&
Expand All @@ -106,8 +107,8 @@ const SendContent: FC<{
}

_setRecipient(value);
if (tronBalances && value.address.blockchain === BLOCKCHAIN_NAME.TRON) {
setAmountViewState({ token: toTronAsset(tronBalances.balances[0]) });
if (activeTronWallet && value.address.blockchain === BLOCKCHAIN_NAME.TRON) {
setAmountViewState({ token: TRON_USDT_ASSET });
}
};

Expand Down Expand Up @@ -152,6 +153,9 @@ const SendContent: FC<{
};

const processTron = (address: string) => {
if (!activeTronWallet) {
return;
}
const item = { address: address, blockchain: BLOCKCHAIN_NAME.TRON } as const;

setRecipient({
Expand Down Expand Up @@ -228,10 +232,9 @@ const SendContent: FC<{
return;
}

// TODO: ENABLE TRON
// if (seeIfValidTronAddress(signature)) {
// return processTron(signature);
// }
if (seeIfValidTronAddress(signature)) {
return processTron(signature);
}

return sdk.uiEvents.emit('copy', {
method: 'copy',
Expand All @@ -258,6 +261,19 @@ const SendContent: FC<{
});
}, [amountViewState?.token, amountViewState?.coinValue]);

let acceptBlockchains: BLOCKCHAIN_NAME[] = [];
if (chain) {
if (chain === BLOCKCHAIN_NAME.TRON && !activeTronWallet) {
acceptBlockchains = [BLOCKCHAIN_NAME.TON];
} else {
acceptBlockchains = [chain];
}
} else {
acceptBlockchains = activeTronWallet
? [BLOCKCHAIN_NAME.TON, BLOCKCHAIN_NAME.TRON]
: [BLOCKCHAIN_NAME.TON];
}

return (
<Wrapper standalone={false} extension={true}>
<HideTwaMainButton />
Expand All @@ -279,7 +295,7 @@ const SendContent: FC<{
onScan={onScan}
keyboard="decimal"
isExternalLoading={isAccountLoading}
acceptBlockchains={chain ? [chain] : undefined}
acceptBlockchains={acceptBlockchains}
MainButton={RecipientTwaMainButton}
HeaderBlock={() => <RecipientTwaHeaderBlock onClose={onClose} />}
fitContent
Expand Down Expand Up @@ -348,6 +364,13 @@ export const TwaSendNotification: FC<PropsWithChildren> = ({ children }) => {
reset();
const transfer = options.params;
setChain(chain);

if (transfer.chain === BLOCKCHAIN_NAME.TRON) {
setOpen(true);
track('send_open', { from: transfer.from });
return;
}

if (transfer.address) {
getAccountAsync({ address: transfer.address }).then(account => {
setTonTransfer(makeTransferInitData(transfer, account, jettons));
Expand Down

0 comments on commit 5c9036b

Please sign in to comment.