This part is working fine, it’s just that after connecting to the wallet the app is redirecting to the mobile browser and thus the below function which helps us to get balance is not working
// useCallback ensures that we don't uselessly re-create the _updateWallet function on every render
const _updateWallet = useCallback(async (providedAccounts?: any) => {
// await window.ethereum.enable();
try {
if (typeof window.ethereum !== "undefined") {
console.log("inside");
const accounts =
providedAccounts ||
(await window.ethereum.request({ method: "eth_accounts" }));
console.log("after account", accounts);
console.log("accounts.length", accounts.length);
if (accounts.length === 0) {
// If there are no accounts, then the user is disconnected
setWallet(disconnectedState);
return;
}
// from here this part is not working after redirecting to mobile browser
let balance = await window.ethereum.request({
method: "eth_getBalance",
params: [accounts[0], "latest"],
});
if (typeof balance !== "undefined") {
balance = formatBalance(balance as string);
} else {
throw new Error("Balance is undefined");
}
console.log("after balance", balance);
const chainId = await window.ethereum.request({
method: "eth_chainId",
});
console.log("after chain", chainId);
// Gari balance
let encoded = new ethers.AbiCoder().encode(["address"], [accounts[0]]);
const erc20Bal = await window.ethereum.request({
method: "eth_call",
params: [
{
to: tokenAddresses.gari,
data: ERC20_FUNCTION.balance + encoded.slice(2),
},
],
});
let gari;
if (typeof erc20Bal !== "undefined") {
gari = formatBalance(erc20Bal as string, 9);
} else {
throw new Error("erc20Bal is undefined");
}
console.log("after gari", gari);
// Staking Info
encoded = new ethers.AbiCoder().encode(["address"], [accounts[0]]);
const stakeData = await window.ethereum.request({
method: "eth_call",
params: [
{
to: tokenAddresses.staking,
data: STAKING_FUNCTION.info + encoded.slice(2),
},
],
});
console.log("after stake", stakeData);
let stakedAmount;
let lastStaked;
let reward;
if (typeof stakeData !== "undefined" && stakeData !== null) {
//stake amount
stakedAmount = formatBalance(
"0x" + (stakeData as string).substring(2, 66),
9
);
// last stake date
lastStaked = new Date(
parseInt((stakeData as string).substring(67, 130), 16) * 1000
).toDateString();
// pending rewards
reward = formatBalance(
"0x" + (stakeData as string).substring(131, 194),
9
);
} else {
throw new Error("stakeData is undefined");
}
// Allowance Info
// const allowanceData = await window.ethereum.request({
// method: "eth_call",
// params: [
// {
// to: tokenAddresses.gari,
// data: ERC20_FUNCTION.allowance + encoded.slice(2),
// },
// ],
// });
// const allowance = formatBalance(allowanceData, 9);
setWallet({
accounts,
balance: balance as string, // Type assertion
gari,
allowance: "",
stakedAmount,
lastStaked,
reward,
chainId: chainId as string, // Type assertion
});
}
} catch (error) {
console.log(error);
}
}, []);