Skip to content

Commit

Permalink
[Issue-1850] Fix bug for Ton transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhquang committed Dec 23, 2024
1 parent 5ad343a commit 871db00
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 45 deletions.
34 changes: 3 additions & 31 deletions src/components/MetaInfo/parts/AccountItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import MetaInfoStyles from 'components/MetaInfo/style';
import useGeneralStyles from 'components/MetaInfo/hooks/useGeneralStyles';
import { getSchemaColor, renderColContent } from 'components/MetaInfo/shared';
import { View } from 'react-native';
import { useSelector } from 'react-redux';
import useGetAccountByAddress from 'hooks/screen/useGetAccountByAddress';
import { RootState } from 'stores/index';
import { findNetworkJsonByGenesisHash } from 'utils/getNetworkJsonByGenesisHash';
import { toShort } from 'utils/index';
import Typography from '../../design-system-ui/typography';
import { AccountProxyAvatar } from 'components/design-system-ui/avatar/account-proxy-avatar';
import { isAddress } from '@subwallet/keyring';
import { reformatAddress } from '@subwallet/extension-base/utils';

export interface AccountInfoItem extends InfoItemBase {
address: string;
Expand All @@ -26,41 +21,18 @@ const AccountItem: React.FC<AccountInfoItem> = ({
label,
address: accountAddress,
name: accountName,
networkPrefix: addressPrefix,
}: AccountInfoItem) => {
const theme = useSubWalletTheme().swThemes;
const _style = MetaInfoStyles(theme);
const { labelGeneralStyle, valueGeneralStyle } = useGeneralStyles(theme);

const chainInfoMap = useSelector((state: RootState) => state.chainStore.chainInfoMap);

const account = useGetAccountByAddress(accountAddress);

const name = useMemo(() => {
return accountName || account?.name;
}, [account?.name, accountName]);

const address = useMemo(() => {
let addPrefix = 42;

if (addressPrefix !== undefined) {
addPrefix = addressPrefix;
}

if (account?.originGenesisHash) {
const network = findNetworkJsonByGenesisHash(chainInfoMap, account.originGenesisHash);

if (network) {
addPrefix = network.substrateInfo?.addressPrefix ?? addPrefix;
}
}

if (!accountAddress || !isAddress(accountAddress)) {
return accountAddress;
}

return reformatAddress(accountAddress, addPrefix);
}, [account, accountAddress, addressPrefix, chainInfoMap]);
const shortAddress = toShort(accountAddress);

const valueStyle = useMemo(() => {
return {
Expand All @@ -87,15 +59,15 @@ const AccountItem: React.FC<AccountInfoItem> = ({
</View>
<View style={[_style.col, _style['col.grow'], _style['col.to-right']]}>
<View style={[_style.valueWrapper, { gap: theme.sizeXS, alignItems: 'flex-start' }]}>
<AccountProxyAvatar value={address} size={24} />
<AccountProxyAvatar value={account?.proxyId || accountAddress} size={24} />
<View style={{ flexShrink: 1 }}>
{!!name && (
<Typography.Text ellipsis style={valueStyle}>
{name}
</Typography.Text>
)}
<Typography.Text ellipsis style={!!name ? subValueStyle : valueStyle}>
{toShort(address)}
{shortAddress}
</Typography.Text>
</View>
</View>
Expand Down
7 changes: 7 additions & 0 deletions src/providers/DataContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
subscribeCampaignPopupData,
subscribeCampaignBannerData,
subscribeCampaignConfirmationData,
subscribeConfirmationRequestsTon,
} from 'stores/utils';
import React, { useContext, useEffect, useRef } from 'react';
import { Provider } from 'react-redux';
Expand Down Expand Up @@ -355,6 +356,12 @@ export const DataContextProvider = ({ children }: DataContextProviderProps) => {
relatedStores: ['requestState'],
isStartImmediately: true,
});
_DataContext.addHandler({
...subscribeConfirmationRequestsTon,
name: 'subscribeConfirmationRequestsTon',
relatedStores: ['requestState'],
isStartImmediately: true,
});
_DataContext.addHandler({
...subscribeTransactionRequests,
name: 'subscribeTransactionRequests',
Expand Down
19 changes: 12 additions & 7 deletions src/screens/Confirmations/parts/Sign/Ton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ export const TonSignArea = (props: Props) => {
show('Transaction expired', { type: 'danger' });
onCancel();
}

setLoading(true);
Platform.OS === 'android' && setTimeout(() => DeviceEventEmitter.emit(OPEN_UNLOCK_FROM_MODAL), 250);
onConfirmPassword(onApprovePassword)()?.catch(() => {
setLoading(false);
});
}

setLoading(true);
Platform.OS === 'android' && setTimeout(() => DeviceEventEmitter.emit(OPEN_UNLOCK_FROM_MODAL), 250);
onConfirmPassword(onApprovePassword)()?.catch(() => {
setLoading(false);
});
}, [hideAll, onApprovePassword, onCancel, onConfirmPassword, show, txExpirationTime]);

useEffect(() => {
Expand All @@ -111,7 +111,12 @@ export const TonSignArea = (props: Props) => {
<Button disabled={loading} block icon={getButtonIcon(XCircle)} type={'secondary'} onPress={onCancel}>
{i18n.common.cancel}
</Button>
<Button block disabled={showQuoteExpired} icon={getButtonIcon(approveIcon)} loading={loading} onPress={onConfirm}>
<Button
block
disabled={showQuoteExpired || loading}
icon={getButtonIcon(approveIcon)}
loading={loading}
onPress={onConfirm}>
{i18n.buttonTitles.approve}
</Button>
</ConfirmationFooter>
Expand Down
17 changes: 10 additions & 7 deletions src/screens/Transaction/SendFund/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export const SendFund = ({
);

const handleBasicSubmit = useCallback(
(values: TransferFormValues): Promise<SWTransactionResponse> => {
(values: TransferFormValues, options: TransferOptions): Promise<SWTransactionResponse> => {
const { asset, chain, destChain, from: _from, to, value } = values;

let sendPromise: Promise<SWTransactionResponse>;
Expand All @@ -527,6 +527,7 @@ export const SendFund = ({
tokenSlug: asset,
value: value,
transferAll: isTransferAll,
transferBounceable: options.isTransferBounceable,
});
} else {
// Make cross chain transfer
Expand All @@ -538,6 +539,7 @@ export const SendFund = ({
to,
value,
transferAll: isTransferAll,
transferBounceable: options.isTransferBounceable,
});
}

Expand Down Expand Up @@ -580,7 +582,7 @@ export const SendFund = ({
);

const doSubmit = useCallback(
(values: TransferFormValues) => {
(values: TransferFormValues, options: TransferOptions) => {
if (isShowWarningOnSubmit(values)) {
return;
}
Expand Down Expand Up @@ -612,7 +614,7 @@ export const SendFund = ({
const submitPromise: Promise<SWTransactionResponse> | undefined =
stepType === CommonStepType.TOKEN_APPROVAL
? handleSnowBridgeSpendingApproval(values)
: handleBasicSubmit(values);
: handleBasicSubmit(values, options);

const rs = await submitPromise;
const success = onSuccess(isLastStep, needRollback)(rs);
Expand Down Expand Up @@ -684,7 +686,7 @@ export const SendFund = ({
completeBtnTitle: 'Continue',
customIcon: <PageIcon icon={Warning} color={theme.colorWarning} />,
onCompleteModal: () => {
doSubmit(values);
doSubmit(values, options);
confirmModal.hideConfirmModal();
},
onCancelModal: () => confirmModal.hideConfirmModal(),
Expand Down Expand Up @@ -713,7 +715,7 @@ export const SendFund = ({
shouldTouch: true,
});
options.isTransferBounceable = true;
doSubmit({ ...values, to: formattedAddress });
doSubmit({ ...values, to: formattedAddress }, options);
confirmModal.hideConfirmModal();
},
onCancelModal: () => {
Expand All @@ -732,7 +734,7 @@ export const SendFund = ({
return;
}

doSubmit(values);
doSubmit(values, options);
},
[isTransferAll, chainValue, destChainValue, doSubmit, chainInfoMap, confirmModal, theme.colorWarning, setValue],
);
Expand Down Expand Up @@ -994,7 +996,7 @@ export const SendFund = ({
let currentValues = getValues();
setForceTransferAll(false);
setLoading(true);
doSubmit(currentValues);
onSubmit(currentValues);
},
},
],
Expand All @@ -1009,6 +1011,7 @@ export const SendFund = ({
nativeTokenBalance.decimals,
nativeTokenBalance.symbol,
nativeTokenSlug,
onSubmit,
tokenBalance.decimals,
tokenBalance.symbol,
]);
Expand Down
3 changes: 3 additions & 0 deletions src/stores/base/RequestState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const CONFIRMATIONS_FIELDS: Array<keyof RequestState> = [
'evmWatchTransactionRequest',
'errorConnectNetwork',
'connectWCRequest',
'tonSignatureRequest',
'tonSendTransactionRequest',
'tonWatchTransactionRequest',
];

export interface ConfirmationQueueItem {
Expand Down
12 changes: 12 additions & 0 deletions src/stores/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CampaignBanner,
ChainStakingMetadata,
ConfirmationsQueue,
ConfirmationsQueueTon,
CrowdloanJson,
KeyringState,
MantaPayConfig,
Expand Down Expand Up @@ -187,6 +188,17 @@ export const subscribeConfirmationRequests = lazySubscribeMessage(
updateConfirmationRequests,
);

export const updateConfirmationRequestsTon = (data: ConfirmationsQueueTon) => {
store.dispatch({ type: 'requestState/updateConfirmationRequestsTon', payload: data });
};

export const subscribeConfirmationRequestsTon = lazySubscribeMessage(
'pri(confirmationsTon.subscribe)',
null,
updateConfirmationRequestsTon,
updateConfirmationRequestsTon,
);

export const updateTransactionRequests = (data: Record<string, SWTransactionResult>) => {
// Convert data to object with key as id

Expand Down

0 comments on commit 871db00

Please sign in to comment.