add connect to ethers provider function and use pinia as ether params store
Co-authored-by: geovanne97 <geovannessaraiva97@gmail.com> Co-authored-by: brunoedcf <brest.dallacosta@outlook.com>
This commit is contained in:
23
src/utils/ethers.ts
Normal file
23
src/utils/ethers.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import { ethers } from "ethers";
|
||||
|
||||
const getProvider = async (): Promise<ethers.providers.Web3Provider | null> => {
|
||||
const etherStore = useEtherStore();
|
||||
const window_ = window as any;
|
||||
let provider: ethers.providers.Web3Provider | null = null;
|
||||
|
||||
if (window_.ethereum) {
|
||||
provider = new ethers.providers.Web3Provider(window_.ethereum);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
const balance = await provider.getBalance(walletAddress[0]);
|
||||
|
||||
etherStore.setProvider(provider);
|
||||
etherStore.setWalletAddress(walletAddress);
|
||||
etherStore.setBalance(Number(balance));
|
||||
} else console.log("Browser não suporta conexão com metamask");
|
||||
|
||||
return provider;
|
||||
};
|
||||
|
||||
export default { getProvider };
|
||||
Reference in New Issue
Block a user