Use first account to check for balance

This commit is contained in:
Shreerang Kale 2025-02-10 16:48:48 +05:30
parent d209ed6099
commit a0977f910d
2 changed files with 24 additions and 7 deletions

View File

@ -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<StackParamsList>();
@ -55,6 +55,23 @@ const App = (): React.JSX.Element => {
SignClientTypes.EventArguments["session_proposal"] | undefined
>();
const getAccountsData = useCallback(async (chainId: string): Promise<Account[]> => {
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]);

View File

@ -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 {