164 lines
5.2 KiB
JavaScript
164 lines
5.2 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import { markRaw } from "vue";
|
|
import { useTokenStore } from "./tokens";
|
|
import { useRewardLockerStore } from "./rewardlocker";
|
|
import MemeToken from "@/assets/contracts/MemeToken.json";
|
|
|
|
import {
|
|
createConfig,
|
|
configureChains,
|
|
getAccount,
|
|
getContract,
|
|
connect,
|
|
disconnect,
|
|
InjectedConnector,
|
|
watchAccount,
|
|
getWalletClient,
|
|
fetchBalance,
|
|
multicall,
|
|
getNetwork,
|
|
} 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, mainnet } 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,
|
|
provider: null,
|
|
client: null,
|
|
publicClient: null,
|
|
walletClient: null,
|
|
config: null,
|
|
chain: null,
|
|
token: [],
|
|
initialized1: false,
|
|
}),
|
|
getters: {
|
|
sacrificeToken: (state) => state.token.filter((obj) => {
|
|
const tokenStore = useTokenStore();
|
|
|
|
return obj.contractAddress !== tokenStore.memeToken.contractAddress;
|
|
}),
|
|
memeToken: (state) => state.token.find((obj) => {
|
|
const tokenStore = useTokenStore();
|
|
|
|
return obj.contractAddress === tokenStore.memeToken.contractAddress;
|
|
}),
|
|
},
|
|
actions: {
|
|
async init() {
|
|
console.log("wallet init");
|
|
|
|
|
|
const account = getAccount();
|
|
|
|
if (account.address) {
|
|
this.address = account.address;
|
|
this.connected = true;
|
|
}
|
|
|
|
// 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;
|
|
// }
|
|
// });
|
|
|
|
},
|
|
async connectWallet(connector) {
|
|
try {
|
|
console.log("connector.name", connector.name);
|
|
// if (!account.isConnected) {
|
|
const connection = await connect({
|
|
chainId: goerli.id,
|
|
connector: connector,
|
|
});
|
|
|
|
console.log("connection", connection);
|
|
} catch (error) {
|
|
if (error.message.includes("Connector not found")) {
|
|
console.warn("Connector not found");
|
|
}
|
|
console.error(error);
|
|
}
|
|
// this.address = connection.account;
|
|
},
|
|
async getTokenBalance(token) {
|
|
console.log("token", token);
|
|
console.log("address", this.address);
|
|
const result = await token.contract.read.balanceOf([this.address]);
|
|
return result;
|
|
},
|
|
|
|
async getTokenBalances() {
|
|
console.log("getTokenBalances");
|
|
this.token = []
|
|
const tokenStore = useTokenStore();
|
|
console.log("tokenStore.token.length", tokenStore.token.length);
|
|
var contracts = [];
|
|
for (let index = 0; index < tokenStore.token.length; index++) {
|
|
const token = tokenStore.token[index];
|
|
contracts.push({ ...token.contract, functionName: "balanceOf", args: [this.address] });
|
|
}
|
|
|
|
const data = await multicall({
|
|
contracts,
|
|
});
|
|
console.log("data", data);
|
|
for (let index = 0; index < tokenStore.token.length; index++) {
|
|
const token = tokenStore.token[index];
|
|
console.log("token", token);
|
|
console.log("data[index].result", data[index].result);
|
|
const balanceOfDecimal = data[index].result / BigInt(Math.pow(10, token.decimals));
|
|
this.token.push({
|
|
contractAddress: token.contractAddress,
|
|
balanceOf: Number(balanceOfDecimal),
|
|
});
|
|
}
|
|
},
|
|
async loadWalletData() {
|
|
if (!this.connected) {
|
|
return
|
|
}
|
|
console.log("loadWalletData");
|
|
const rewardlockerStore = useRewardLockerStore();
|
|
|
|
await this.getTokenBalances();
|
|
//MemeToken Staked
|
|
const tokenStore = useTokenStore();
|
|
console.log("tokenStore.memeToken.decimals", tokenStore.memeToken.decimals);
|
|
const staked = await rewardlockerStore.getStaked(this.address);
|
|
console.log("staked", staked);
|
|
console.log("ztrsdz", staked / BigInt(Math.pow(10, tokenStore.memeToken.decimals)));
|
|
this.memeToken.staked = Number(staked / BigInt(Math.pow(10, tokenStore.memeToken.decimals)));
|
|
const unclaimedRewardsArray = await rewardlockerStore.getTotalRewards(this.address);
|
|
console.log("unclaimedRewardsArray", unclaimedRewardsArray);
|
|
const unclaimedRewards = unclaimedRewardsArray[0] + unclaimedRewardsArray[1];
|
|
this.memeToken.unclaimedRewards = Number(unclaimedRewards/ BigInt(Math.pow(10, tokenStore.memeToken.decimals)))
|
|
|
|
}
|
|
},
|
|
persist: true,
|
|
});
|