Adding request to user change network in wallet
This commit is contained in:
@@ -1,38 +1,33 @@
|
||||
import { ethers } from "ethers";
|
||||
import { getProvider } from "./provider";
|
||||
import blockchain from "../utils/blockchain";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
|
||||
import { getProvider } from "./provider";
|
||||
import { getTokenAddress, possibleChains } from "./addresses";
|
||||
|
||||
import mockToken from "../utils/smart_contract_files/MockToken.json";
|
||||
import addresses from "../utils/smart_contract_files/localhost.json";
|
||||
|
||||
import { ethers } from "ethers";
|
||||
import { formatEther } from "ethers/lib/utils";
|
||||
|
||||
const updateWalletStatus = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
const provider = getProvider();
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const { chainId } = await provider.getNetwork();
|
||||
etherStore.setNetworkName(possibleChains[chainId]);
|
||||
|
||||
const mockTokenContract = new ethers.Contract(
|
||||
getTokenAddress(),
|
||||
mockToken.abi,
|
||||
signer
|
||||
);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
const balance = await mockTokenContract.balanceOf(walletAddress[0]);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(blockchain.formatBigNumber(balance));
|
||||
etherStore.setBalance(formatEther(balance));
|
||||
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
|
||||
};
|
||||
|
||||
const updateWalletBalance = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(blockchain.formatBigNumber(balance));
|
||||
};
|
||||
|
||||
export { updateWalletStatus, updateWalletBalance };
|
||||
export { updateWalletStatus };
|
||||
|
||||
Reference in New Issue
Block a user