Remove redundant code
This commit is contained in:
parent
1124805bb7
commit
fc90fa49bb
@ -60,18 +60,12 @@ const useGetOrCreateAccounts = () => {
|
||||
|
||||
window.addEventListener('message', handleCreateAccounts);
|
||||
|
||||
const isAndroidWebView = !!(window.Android);
|
||||
if (!isAndroidWebView) {
|
||||
const defaultChainId = networksData[0]?.chainId;
|
||||
if (defaultChainId) {
|
||||
getOrCreateAccountsForChain(defaultChainId);
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('message', handleCreateAccounts);
|
||||
};
|
||||
}, [networksData, getAccountsData, getOrCreateAccountsForChain]);
|
||||
|
||||
return { getOrCreateAccountsForChain };
|
||||
};
|
||||
|
||||
export default useGetOrCreateAccounts;
|
||||
|
||||
@ -8,37 +8,27 @@ import useAccountsData from "../hooks/useAccountsData";
|
||||
|
||||
import { StackParamsList } from '../types';
|
||||
import useGetOrCreateAccounts from './useGetOrCreateAccounts';
|
||||
import { retrieveAccountsForNetwork, createWallet } from '../utils/accounts';
|
||||
import { retrieveAccountsForNetwork } from '../utils/accounts';
|
||||
|
||||
export const useWebViewHandler = () => {
|
||||
// Navigation and context hooks
|
||||
const navigation = useNavigation<NativeStackNavigationProp<StackParamsList>>();
|
||||
const { selectedNetwork, networksData } = useNetworks();
|
||||
const { selectedNetwork } = useNetworks();
|
||||
const { accounts, currentIndex } = useAccounts();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
|
||||
const handleGetOrCreateAccount = useCallback(async (namespace: string, chainId: string) => {
|
||||
try {
|
||||
// Find the requested network
|
||||
const network = networksData.find(net =>
|
||||
net.namespace === namespace && net.chainId === chainId
|
||||
);
|
||||
// Initialize accounts
|
||||
const { getOrCreateAccountsForChain } = useGetOrCreateAccounts();
|
||||
|
||||
if (!network) {
|
||||
window.Android?.onAccountError?.('Network configuration not found');
|
||||
return;
|
||||
}
|
||||
const handleGetOrCreateAccount = useCallback(async (chainId: string) => {
|
||||
try {
|
||||
|
||||
await getOrCreateAccountsForChain(chainId);
|
||||
|
||||
let accountsData = await getAccountsData(chainId);
|
||||
|
||||
if (accountsData.length === 0) {
|
||||
console.log("Account not found, creating wallet...");
|
||||
await createWallet(networksData);
|
||||
accountsData = await getAccountsData(chainId);
|
||||
}
|
||||
|
||||
if (!accountsData || accountsData.length === 0) {
|
||||
window.Android?.onAccountError?.('Failed to create/retrieve account');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -47,10 +37,7 @@ export const useWebViewHandler = () => {
|
||||
console.error('Account operation error:', error);
|
||||
window.Android?.onAccountError?.(`Operation failed: ${error}`);
|
||||
}
|
||||
}, [networksData, getAccountsData]);
|
||||
|
||||
// Initialize accounts
|
||||
useGetOrCreateAccounts();
|
||||
}, [getOrCreateAccountsForChain, getAccountsData]);
|
||||
|
||||
// Core navigation handler
|
||||
const navigateToSignRequest = useCallback((message: string) => {
|
||||
@ -114,13 +101,13 @@ export const useWebViewHandler = () => {
|
||||
|
||||
try {
|
||||
// TODO: Pass the account info for transferring tokens
|
||||
// Get all accounts
|
||||
const chainAccounts = await retrieveAccountsForNetwork(
|
||||
// Get first account
|
||||
const [chainAccount] = await retrieveAccountsForNetwork(
|
||||
`${namespace}:${chainId}`,
|
||||
'0' // Use the first account
|
||||
'0'
|
||||
);
|
||||
|
||||
if (!chainAccounts || chainAccounts.length === 0) {
|
||||
if (!chainAccount) {
|
||||
console.error('Accounts not found');
|
||||
if (window.Android?.onTransferError) {
|
||||
window.Android.onTransferError('Accounts not found');
|
||||
@ -128,7 +115,6 @@ export const useWebViewHandler = () => {
|
||||
|
||||
return;
|
||||
}
|
||||
const chainAccount = chainAccounts[0]; // Use the first account
|
||||
|
||||
const path = `/transfer/${namespace}/${chainId}/${chainAccount.address}/${to}/${amount}`;
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ import {
|
||||
Appbar,
|
||||
TextInput,
|
||||
} from 'react-native-paper';
|
||||
import JSONbig from 'json-bigint';
|
||||
import { providers, BigNumber } from 'ethers';
|
||||
import { Deferrable } from 'ethers/lib/utils';
|
||||
|
||||
@ -62,7 +63,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
const requestEvent = route.params.requestEvent;
|
||||
const chainId = requestEvent?.params.chainId || route.params.chainId;
|
||||
const requestMethod = requestEvent?.params.request.method;
|
||||
const finalMemo = route.params.memo || MEMO;
|
||||
const txMemo = route.params.memo || MEMO;
|
||||
|
||||
const [account, setAccount] = useState<Account>();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
@ -216,7 +217,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
to: transaction.to,
|
||||
amount: transaction.value,
|
||||
denom: requestedNetwork!.nativeDenom,
|
||||
memo: finalMemo,
|
||||
memo: txMemo,
|
||||
gas: cosmosGasLimit,
|
||||
fees: fees
|
||||
});
|
||||
@ -241,15 +242,13 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
],
|
||||
gas: cosmosGasLimit,
|
||||
},
|
||||
finalMemo,
|
||||
txMemo,
|
||||
);
|
||||
|
||||
console.log('Transaction result:', result);
|
||||
|
||||
// Convert BigInt values to strings before sending to Android
|
||||
const serializedResult = JSON.stringify(result, (key, value) =>
|
||||
typeof value === 'bigint' ? value.toString() : value
|
||||
);
|
||||
const serializedResult = JSONbig.stringify(result);
|
||||
|
||||
// Send the result back to Android and close dialog
|
||||
if (window.Android?.onTransferComplete) {
|
||||
@ -390,11 +389,14 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
|
||||
const { topic } = requestEvent;
|
||||
await web3wallet!.respondSessionRequest({ topic, response });
|
||||
navigation.navigate('Home');
|
||||
} else {
|
||||
await handleIntent();
|
||||
navigation.navigate('Home');
|
||||
}
|
||||
} catch (error) {
|
||||
if (window.Android?.onTransferError) {
|
||||
window.Android.onTransferError(`Transaction Failed: ${error}`);
|
||||
}
|
||||
if (!(error instanceof Error)) {
|
||||
throw error;
|
||||
}
|
||||
@ -545,7 +547,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
const gasEstimation = await cosmosStargateClient.simulate(
|
||||
transaction.from!,
|
||||
[sendMsg],
|
||||
finalMemo,
|
||||
txMemo,
|
||||
);
|
||||
|
||||
setCosmosGasLimit(
|
||||
@ -563,7 +565,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
}
|
||||
};
|
||||
getCosmosGas();
|
||||
}, [cosmosStargateClient, isSufficientFunds, sendMsg, transaction,finalMemo]);
|
||||
}, [cosmosStargateClient, isSufficientFunds, sendMsg, transaction,txMemo]);
|
||||
|
||||
useEffect(() => {
|
||||
if (balance && !isSufficientFunds) {
|
||||
@ -623,7 +625,7 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
{namespace === COSMOS && (
|
||||
<DataBox
|
||||
label="Memo"
|
||||
data={finalMemo}
|
||||
data={txMemo}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user