136 lines
3.6 KiB
JavaScript
136 lines
3.6 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import { markRaw } from "vue";
|
|
import { useTokenStore } from "./tokens";
|
|
import { useRewardLockerStore } from "./rewardlocker";
|
|
|
|
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: () => ({
|
|
alchemy: null,
|
|
connected: false,
|
|
address: null,
|
|
provider: null,
|
|
client: null,
|
|
publicClient: null,
|
|
walletClient: null,
|
|
config: null,
|
|
chain: null,
|
|
token: [],
|
|
}),
|
|
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;
|
|
}),
|
|
unclaimedRewards() {
|
|
const tokenStore = useTokenStore();
|
|
if (!this.memeToken?.unclaimedRewards) {
|
|
return 0n;
|
|
}
|
|
return this.memeToken.unclaimedRewards / 10n ** tokenStore.memeToken.decimals;
|
|
},
|
|
},
|
|
actions: {
|
|
async init() {
|
|
console.log("wallet init");
|
|
|
|
const account = getAccount();
|
|
|
|
if (account.address) {
|
|
this.address = account.address;
|
|
this.connected = true;
|
|
}
|
|
},
|
|
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) {
|
|
const result = await token.contract.read.balanceOf([this.address]);
|
|
return result;
|
|
},
|
|
async getTokenBalances() {
|
|
console.log("getTokenBalances");
|
|
this.token = [];
|
|
const tokenStore = useTokenStore();
|
|
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,
|
|
});
|
|
for (let index = 0; index < tokenStore.token.length; index++) {
|
|
const token = tokenStore.token[index];
|
|
this.token.push({
|
|
contractAddress: token.contractAddress,
|
|
balanceOf: data[index].result,
|
|
});
|
|
}
|
|
},
|
|
async loadWalletData() {
|
|
if (!this.connected) {
|
|
return;
|
|
}
|
|
console.log("loadWalletData");
|
|
const rewardlockerStore = useRewardLockerStore();
|
|
|
|
await this.getTokenBalances();
|
|
//MemeToken Staked
|
|
const tokenStore = useTokenStore();
|
|
this.memeToken.staked = await rewardlockerStore.getStaked(this.address);
|
|
const unclaimedRewardsArray = await rewardlockerStore.getTotalRewards(this.address);
|
|
const unclaimedRewards = unclaimedRewardsArray[0] + unclaimedRewardsArray[1];
|
|
this.memeToken.unclaimedRewards = unclaimedRewards;
|
|
},
|
|
},
|
|
// persist: true,
|
|
});
|