Refactor of wallet and provider methods

Co-authored-by: brunoedcf <brest.dallacosta@outlook.com>
This commit is contained in:
RcleydsonR
2023-01-12 19:41:04 -03:00
parent 8b0a212b78
commit 86131b5641
5 changed files with 99 additions and 77 deletions

43
src/blockchain/wallet.ts Normal file
View File

@@ -0,0 +1,43 @@
import { ethers } from "ethers";
import { getProvider } from "./provider";
import blockchain from "../utils/blockchain"
import { useEtherStore } from "@/store/ether";
import mockToken from "../utils/smart_contract_files/MockToken.json";
import addresses from "../utils/smart_contract_files/localhost.json";
const updateWalletStatus = 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));
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
}