From a0977f910df03236b8abeece4059aa059f1ffe7c Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Mon, 10 Feb 2025 16:48:48 +0530 Subject: [PATCH] Use first account to check for balance --- src/App.tsx | 29 +++++++++++++++++++++++------ src/utils/misc.ts | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a14e2f7..0d812f2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -24,7 +24,7 @@ import SignRequest from "./screens/SignRequest"; import AddSession from "./screens/AddSession"; import WalletConnect from "./screens/WalletConnect"; import ApproveTransaction from "./screens/ApproveTransaction"; -import { StackParamsList } from "./types"; +import { Account, StackParamsList } from "./types"; import { EIP155_SIGNING_METHODS } from "./utils/wallet-connect/EIP155Data"; import { getSignParamsMessage } from "./utils/wallet-connect/helpers"; import ApproveTransfer from "./screens/ApproveTransfer"; @@ -39,7 +39,7 @@ import { Header } from "./components/Header"; import { WalletEmbed } from "./screens/WalletEmbed"; import { AutoSignIn } from "./screens/AutoSignIn"; import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc"; -import { retrieveSingleAccount } from "./utils/accounts"; +import { retrieveAccounts } from "./utils/accounts"; const Stack = createStackNavigator(); @@ -55,6 +55,23 @@ const App = (): React.JSX.Element => { SignClientTypes.EventArguments["session_proposal"] | undefined >(); + const getAccountsData = useCallback(async (chainId: string): Promise => { + const targetNetwork = networksData.find(network => network.chainId === chainId); + + if (!targetNetwork) { + return []; + } + + const accounts = await retrieveAccounts(targetNetwork); + + if (!accounts || accounts.length === 0) { + return []; + } + + return accounts + }, [networksData]); + + const onSessionProposal = useCallback( async (proposal: SignClientTypes.EventArguments["session_proposal"]) => { if (!accounts.length || !accounts.length) { @@ -227,7 +244,7 @@ const App = (): React.JSX.Element => { const handleCheckBalance = async (event: MessageEvent) => { if (event.data.type !== 'CHECK_BALANCE') return; - const { chainId, address, amount } = event.data; + const { chainId, amount } = event.data; const network = networksData.find(net => net.chainId === chainId); if (!network) { @@ -235,7 +252,7 @@ const App = (): React.JSX.Element => { throw new Error('Requested network not supported.'); } - const account = await retrieveSingleAccount(network.namespace, network.chainId, address); + const account = (await getAccountsData(chainId))[0]; if (!account) { throw new Error('Account not found for the requested address.'); } @@ -256,7 +273,7 @@ const App = (): React.JSX.Element => { network.nativeDenom!.toLowerCase() ); - const areFundsSufficient = !checkSufficientFunds(amount, balance.amount); + const areFundsSufficient = checkSufficientFunds(amount, balance.amount); sendMessage(event.source as Window, 'IS_SUFFICIENT', areFundsSufficient, event.origin); }; @@ -266,7 +283,7 @@ const App = (): React.JSX.Element => { return () => { window.removeEventListener('message', handleCheckBalance); }; - }, [networksData]); + }, [networksData, getAccountsData]); const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]); diff --git a/src/utils/misc.ts b/src/utils/misc.ts index afbd824..d7846de 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -162,7 +162,7 @@ const checkSufficientFunds = (amount: string, balance: string) => { const amountBigNum = BigNumber.from(String(amount)); const balanceBigNum = BigNumber.from(balance); - return balanceBigNum.gt(amountBigNum); + return balanceBigNum.gte(amountBigNum); }; export {