import { defineStore } from "pinia"; import { useMemeburnerStore } from "./memeburner"; import { useTokenStore } from "./tokens"; import { useRewardLockerStore } from "./rewardlocker"; import { useWalletStoreV2 } from "./walletV2"; import { multicall } from "@wagmi/core"; export const useCycleStore = defineStore("cycles", { state: () => ({ cycles: [], activeCycle: null, currentCycle: null, }), getters: { //cycle winner is the token who reached the goal //todo winner is now in the cycle object, no reason for that function anymore getCycleWinner() { const goal = this.getCurrentCycleData?.goal; if (!goal) { return; } const winnerCoin = this.cycles[this.activeCycle]?.token.find((token) => token.amount === goal); if (winnerCoin) { return winnerCoin; } else { return false; } }, getCurrentCycleData() { return this.cycles[this.activeCycle]; }, }, actions: { //todo check if still needed // async loadRewardsCycles() { // const walletStore = useWalletStoreV2(); // const rewardLockerStore = useRewardLockerStore(); // console.log("this.currentCycle", this.currentCycle); // const contracts = []; // for (let index = 0; index < this.currentCycle; index++) { // console.log("load cycle: " + index); // console.log({ index, address: walletStore.address }); // contracts.push({ // ...rewardLockerStore.contract, // functionName: "getTotalRewardsOf", // args: [walletStore.address, [index + 1]], // }); // } // const data = await multicall({ // contracts, // }); // for (let index = 0; index < data.length; index++) { // const element = data[index]; // if (!this.cycles[index]) { // this.cycles[index] = {}; // } // } // }, //todo check if needed, or use fillcycle async updateCurrentCycle(sacrificedToken) { console.log("updateCurrentCycle"); const tokenStore = useTokenStore(); const memeBurnerStore = useMemeburnerStore(); var data = await memeBurnerStore.getSacrificedCycle(this.currentCycle); console.log("this.currentCycle", this.currentCycle); console.log("this.cycles", this.cycles); if (this.cycles[this.currentCycle]) { var tokenIndex = this.cycles[this.currentCycle].token.findIndex( (tokenData) => tokenData.contractAddress === sacrificedToken ); console.log("tokenIndex", tokenIndex); const token = tokenStore.getToken(sacrificedToken); this.cycles[this.currentCycle].token[tokenIndex].amount = Number(data[tokenIndex].result[1]) / Math.pow(10, token.decimals); } }, /** * main function to fill a round, will not be filled when already loaded * you can use the refillcycle to overwrite information, because if you claim or stake the values will change */ async fillCycle(cycleNumber, refillCycle = false) { console.log("fillCycle"); const memeBurnerStore = useMemeburnerStore(); const tokenStore = useTokenStore(); const walletStore = useWalletStoreV2(); const rewardLockerStore = useRewardLockerStore(); if (!this.cycles[cycleNumber]?.goal || refillCycle || !this.cycles[cycleNumber]?.connected) { console.log("fill cycle"); //multicall getSacrificedMeme for each token for this cycle var data = await memeBurnerStore.getSacrificedCycle(cycleNumber); let rewardData = null; if (walletStore.connected) { //getRewards multicall for all tokens rewardData = await rewardLockerStore.getRewardsForAllToken(cycleNumber); } //getTotalSacrificeRewardsForToken multicall for all tokens const tokenTotalSacrificed = await rewardLockerStore.getTotalSacrificeRewardsForAllToken(cycleNumber); var goal = await memeBurnerStore.getGoal(cycleNumber); var tokenArray = []; var rewards_clan = 0; var rewards_sacrifice = 0; var rewards_staking = 0; for (let index = 0; index < data.length; index++) { var tokenData = {}; const token = tokenStore.token[index]; //why no multicall? todo var coefficent = await rewardLockerStore.getSacrificedTokenCoefficient( cycleNumber, token.contractAddress ); console.log("coefficent", coefficent); tokenData.name = token.name; tokenData.contractAddress = token.contractAddress; tokenData.vamplogo = token.vamp.logo; tokenData.vampwinner = token.vamp.winner; tokenData.vampwinner2 = token.vamp.winner2; tokenData.vampname = token.vamp.name; tokenData.amount = Number(data[index].result[1]) / Math.pow(10, token.decimals); tokenData.amountInBLD = Number(tokenTotalSacrificed[index].result) / Math.pow(10, token.decimals); if (walletStore.connected) { //if user is connected fill user specific data for the token const sacrificeClanReward = Number(rewardData[index].result[1]); let clanwin = 0; if (cycleNumber !== this.currentCycle) { clanwin = (Number(coefficent) / 10000 - 1) * sacrificeClanReward; console.log("clanwin", clanwin); } const sacrificeRewards = Number(sacrificeClanReward); tokenData.rewards_ges = Number( rewardData[index].result[1] / BigInt(Math.pow(10, token.decimals)) + rewardData[index].result[0] / BigInt(Math.pow(10, token.decimals)) ); tokenData.rewards_clan = clanwin / Math.pow(10, token.decimals); tokenData.rewards_staking = Number( rewardData[index].result[0] / BigInt(Math.pow(10, token.decimals)) ); tokenData.rewards_sacrifice = sacrificeRewards / Math.pow(10, token.decimals); rewards_clan = rewards_clan + tokenData.rewards_clan; rewards_sacrifice = rewards_sacrifice + tokenData.rewards_sacrifice; rewards_staking = rewards_staking + tokenData.rewards_staking; } tokenArray.push(tokenData); } const goalDecimal = Number(goal[0]) / Math.pow(10, tokenStore.memeToken.decimals); const winner = tokenArray.find((token) => token.amount === goalDecimal); const goalInBLD = await memeBurnerStore.getGoalInBLOOD(cycleNumber); //replace vamplogo cause it will automatically change the token for the cycle view (new design) if (winner) { winner.vamplogo = winner.vampwinner; } this.cycles[cycleNumber] = { goal: Number(goal[0]) / Math.pow(10, tokenStore.memeToken.decimals), goalInBLD: Number(goalInBLD / BigInt(Math.pow(10, tokenStore.memeToken.decimals))), expirationTimestamp: Number(goal[2]), rewards_clan: Number(rewards_clan), rewards_staking: Number(rewards_staking), rewards_sacrifice: Number(rewards_sacrifice), rewards_total: Number(rewards_clan) + Number(rewards_sacrifice) + Number(rewards_staking), token: tokenArray, connected: walletStore.connected, winner: winner, }; console.log("this.cycles[cycleNumber]", this.cycles[cycleNumber]); } else { console.log("cycle already filled"); } }, async getFinshedCycleNumbers() { const array = []; const rewardLockerStore = useRewardLockerStore(); const lastCompletedCycle = await rewardLockerStore.lastCompletedCycle(); for (let index = 0; index <= Number(lastCompletedCycle); index++) { array.push(index); } return array; }, }, });