walletv2 + connectwallet

This commit is contained in:
Michel Fromme
2023-07-27 11:25:34 +02:00
parent 34a445eaa3
commit 0bab85fe75
2 changed files with 293 additions and 0 deletions
+167
View File
@@ -0,0 +1,167 @@
import { defineStore } from "pinia";
import { markRaw } from "vue";
import { useTokenStore } from "./tokens";
import {
createConfig,
configureChains,
mainnet,
getAccount,
getContract,
connect,
getWalletClient,
disconnect,
InjectedConnector,
watchAccount,
} from "@wagmi/core";
import { WalletConnectConnector } from "@wagmi/core/connectors/walletConnect";
import { MetaMaskConnector } from "@wagmi/core/connectors/metaMask";
import { CoinbaseWalletConnector } from "@wagmi/core/connectors/coinbaseWallet";
import { goerli } from "@wagmi/core/chains";
import { publicProvider } from "@wagmi/core/providers/public";
import { alchemyProvider } from "@wagmi/core/providers/alchemy";
export const useWalletStoreV2 = defineStore("walletV2", {
state: () => ({
count: 0,
alchemy: null,
connected: false,
address: null,
web3: null,
provider: null,
client: null,
pubilcClient: null,
walletClient: null,
config: null
}),
getters: {},
actions: {
async init() {
console.log("wallet init");
const goerliKey = import.meta.env.VITE_GOERLI_KEY;
const projectId = "d8e5ecb0353c02e21d4c0867d4473ac5";
const { chains, publicClient, webSocketPublicClient } = configureChains(
[goerli],
[alchemyProvider({ apiKey: goerliKey }), publicProvider()]
);
this.config = createConfig({
autoConnect: true,
connectors: [
new CoinbaseWalletConnector({
chains,
options: {
appName: "wagmi demo",
},
}),
new MetaMaskConnector({ chains }),
new WalletConnectConnector({
chains,
options: {
projectId: projectId,
showQrModal: true,
metadata: {
name: "Bloodline-App",
description: "TODO Bloodline Description",
url: "https://gitea.loseyourip.com/",
icons: ["https://gitea.loseyourip.com/assets/img/vampir.png"],
},
},
}),
],
publicClient,
webSocketPublicClient,
});
// await this.isConnected();
// const goerliKey = import.meta.env.VITE_GOERLI_KEY;
// if (this.connected) {
// const web3 = new Web3(window.ethereum);
// this.web3 = markRaw(web3);
// console.log("hierabc");
// window.ethereum.on("accountsChanged", async (data) => {
// console.log("accountsChanged", data);
// console.log("data", data);
// if (data.length > 0) {
// this.address = data[0];
// this.connected = true;
// } else {
// this.address = null;
// this.connected = false;
// }
// });
// // provider = new ethers.BrowserProvider(window.ethereum);
// } else {
// const url = `https://eth-goerli.g.alchemy.com/v2/${goerliKey}`;
// this.web3 = new Web3(url);
// // provider = new ethers.AlchemyProvider("goerli", goerliKey);
// }
// //Websocket
// this.web3socket = new Web3(
// new Web3.providers.WebsocketProvider(`wss://eth-goerli.g.alchemy.com/v2/${goerliKey}`)
// );
},
// connectWalletV2(connector) {
// this.address = data[0];
// },
async connectWallet(connector) {
console.log("connector.name", connector.name);
// if (!account.isConnected) {
const connection = await connect({
chainId: goerli.id,
connector: connector,
});
// this.address = connection.account;
},
async getTokenBalance(token) {
const tokenStore = useTokenStore();
const contract = tokenStore.getTokenContract(token);
const result = await contract.methods.balanceOf(this.address).call();
return result;
},
async isConnected() {
try {
this.loading = true;
if (!window.ethereum) {
console.warn("Metamask not installed"), (this.connected = false);
return;
}
const accounts = await window.ethereum.request({ method: "eth_accounts" });
if (accounts.length) {
this.connected = true;
this.address = accounts[0];
console.log(`You're connected to: ${accounts[0]}`);
// const web3 = new Web3(window.ethereum);
// const tokenStore = useTokenStore();
// console.log("tokenStore.activeToken", tokenStore.activeToken);
// if (tokenStore.activeToken.contractAddress) {
// tokenStore.activeToken.balance = await this.getTokenBalance(
// tokenStore.activeToken
// );
// tokenStore.activeToken.allowance = await tokenStore.allowance();
// }
// tokenStore.memeBurner.contract = new web3.eth.Contract(
// tokenStore.memeBurner.config.abi,
// tokenStore.memeBurner.config.contractAddress
// );
} else {
console.log("Metamask is not connected");
this.connected = false;
}
} catch (error) {
console.log("error", error);
} finally {
this.loading = false;
return this.connected;
}
},
},
persist: true,
});