initial commit
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { useTokenStore } from "./tokens";
|
||||
import Web3 from "web3";
|
||||
export const useWalletStore = defineStore("wallet", {
|
||||
state: () => ({
|
||||
count: 0,
|
||||
alchemy: null,
|
||||
connected: false,
|
||||
address: null,
|
||||
}),
|
||||
getters: {
|
||||
double: (state) => state.count * 2,
|
||||
},
|
||||
actions: {
|
||||
increment() {
|
||||
this.count++;
|
||||
},
|
||||
async disconnectWallet() {
|
||||
// const permissions = await window.ethereum.request({
|
||||
// method: 'wallet_requestPermissions',
|
||||
// params: [{
|
||||
// eth_accounts: {},
|
||||
// }]
|
||||
// });
|
||||
// console.log("permissions", permissions);
|
||||
this.connected = false;
|
||||
this.address = null;
|
||||
},
|
||||
connectWallet() {
|
||||
console.log("window.ethereum", window.ethereum);
|
||||
// if (window.ethereum) {
|
||||
window.ethereum
|
||||
.request({ method: "eth_requestAccounts" })
|
||||
.then((data) => {
|
||||
console.log("data", data);
|
||||
this.address = data[0];
|
||||
this.connected = true;
|
||||
const tokenStore = useTokenStore();
|
||||
if (tokenStore.activeToken.address) {
|
||||
//Token selected but not refreshed
|
||||
this.getTokenBalance(tokenStore.activeToken.address).then(
|
||||
(data) => (tokenStore.activeToken.balance = data)
|
||||
);
|
||||
}
|
||||
// console.log("permissions", permissions);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.code === 4001) {
|
||||
// EIP-1193 userRejectedRequest error
|
||||
console.log("Please connect to MetaMask.");
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
// }
|
||||
},
|
||||
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 {
|
||||
const accounts = await window.ethereum.request({ method: "eth_accounts" });
|
||||
if (accounts.length) {
|
||||
this.connected = true;
|
||||
|
||||
console.log(`You're connected to: ${accounts[0]}`);
|
||||
const web3 = new Web3(window.ethereum);
|
||||
const tokenStore = useTokenStore();
|
||||
tokenStore.memeBurner.contract = new web3.eth.Contract(
|
||||
tokenStore.memeBurner.config.abi,
|
||||
tokenStore.memeBurner.config.contractAddress
|
||||
);
|
||||
} else {
|
||||
this.disconnectWallet();
|
||||
console.log("Metamask is not connected");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
persist: true,
|
||||
});
|
||||
Reference in New Issue
Block a user