Fixing provider state and wallet change listener, formatting balance
Co-authored-by: RcleydsonR <rafael.cleydson@gmail.com>
This commit is contained in:
parent
85d76c9e42
commit
d7f6294e6d
@ -5,7 +5,7 @@ import ethers from "../utils/ethers";
|
|||||||
|
|
||||||
const etherStore = useEtherStore();
|
const etherStore = useEtherStore();
|
||||||
|
|
||||||
const { walletAddress } = storeToRefs(etherStore);
|
const { walletAddress, balance } = storeToRefs(etherStore);
|
||||||
|
|
||||||
const connectMetaMask = () => {
|
const connectMetaMask = () => {
|
||||||
ethers.connectProvider();
|
ethers.connectProvider();
|
||||||
@ -20,6 +20,13 @@ const formatWalletAddress = (): string => {
|
|||||||
);
|
);
|
||||||
return `${initialText} ... ${finalText}`;
|
return `${initialText} ... ${finalText}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formatWalletBalance = (): string => {
|
||||||
|
const formattedBalance = ethers.formatEther(balance.value);
|
||||||
|
const fixed = formattedBalance.substring(0, 8);
|
||||||
|
|
||||||
|
return fixed;
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -41,9 +48,12 @@ const formatWalletAddress = (): string => {
|
|||||||
>
|
>
|
||||||
Conectar carteira
|
Conectar carteira
|
||||||
</button>
|
</button>
|
||||||
<span v-if="walletAddress" class="text-gray-50">
|
<div v-if="walletAddress" class="flex gap-4">
|
||||||
|
<span class="text-gray-50">
|
||||||
{{ formatWalletAddress() }}
|
{{ formatWalletAddress() }}
|
||||||
</span>
|
</span>
|
||||||
|
<span class="text-gray-50"> ETH: {{ formatWalletBalance() }} </span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
</template>
|
</template>
|
||||||
|
@ -1,21 +1,16 @@
|
|||||||
import type { ethers } from "ethers";
|
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
|
|
||||||
export const useEtherStore = defineStore("ether", {
|
export const useEtherStore = defineStore("ether", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
walletAddress: "",
|
walletAddress: "",
|
||||||
balance: 0,
|
balance: "",
|
||||||
provider: null as ethers.providers.Web3Provider | null,
|
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
setWalletAddress(walletAddress: string) {
|
setWalletAddress(walletAddress: string) {
|
||||||
this.walletAddress = walletAddress;
|
this.walletAddress = walletAddress;
|
||||||
},
|
},
|
||||||
setBalance(balance: number) {
|
setBalance(balance: string) {
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
},
|
},
|
||||||
setProvider(provider: ethers.providers.Web3Provider | null) {
|
|
||||||
this.provider = provider;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,21 @@
|
|||||||
import { useEtherStore } from "@/store/ether";
|
import { useEtherStore } from "@/store/ether";
|
||||||
import { ethers } from "ethers";
|
import { ethers } from "ethers";
|
||||||
|
|
||||||
const connectProvider =
|
const updateWalletStatus = async (walletAddress: string) => {
|
||||||
async (): Promise<ethers.providers.Web3Provider | null> => {
|
const etherStore = useEtherStore();
|
||||||
|
const window_ = window as any;
|
||||||
|
const connection = window_.ethereum;
|
||||||
|
|
||||||
|
if (!connection) return;
|
||||||
|
|
||||||
|
const provider = new ethers.providers.Web3Provider(connection);
|
||||||
|
const balance = await provider.getBalance(walletAddress);
|
||||||
|
|
||||||
|
etherStore.setBalance(String(balance));
|
||||||
|
etherStore.setWalletAddress(walletAddress);
|
||||||
|
};
|
||||||
|
|
||||||
|
const connectProvider = async () => {
|
||||||
const etherStore = useEtherStore();
|
const etherStore = useEtherStore();
|
||||||
const window_ = window as any;
|
const window_ = window as any;
|
||||||
const connection = window_.ethereum;
|
const connection = window_.ethereum;
|
||||||
@ -14,16 +27,18 @@ const connectProvider =
|
|||||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||||
const balance = await provider.getBalance(walletAddress[0]);
|
const balance = await provider.getBalance(walletAddress[0]);
|
||||||
|
|
||||||
etherStore.setProvider(provider);
|
|
||||||
etherStore.setWalletAddress(walletAddress[0]);
|
etherStore.setWalletAddress(walletAddress[0]);
|
||||||
etherStore.setBalance(Number(balance));
|
etherStore.setBalance(String(balance));
|
||||||
|
|
||||||
connection.on("accountsChanged", (accounts: string[]) => {
|
connection.on("accountsChanged", (accounts: string[]) => {
|
||||||
etherStore.setWalletAddress(accounts[0]);
|
updateWalletStatus(accounts[0]);
|
||||||
});
|
});
|
||||||
} else console.log("Browser não suporta conexão com metamask");
|
}
|
||||||
|
|
||||||
return provider;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default { connectProvider };
|
const formatEther = (balance: string) => {
|
||||||
|
const formatted = ethers.utils.formatEther(balance);
|
||||||
|
return formatted;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { connectProvider, formatEther };
|
||||||
|
@ -2,9 +2,7 @@
|
|||||||
import ethers from "../utils/ethers";
|
import ethers from "../utils/ethers";
|
||||||
|
|
||||||
const connectMetaMask = () => {
|
const connectMetaMask = () => {
|
||||||
ethers.connectProvider().then((web3Provider) => {
|
ethers.connectProvider();
|
||||||
console.log(web3Provider);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user