Files
ImmortalClanVue/src/stores/rounds.js
T

201 lines
8.2 KiB
JavaScript

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 useRoundsStore = defineStore("rounds", {
state: () => ({
rounds: [],
activeRound: null,
currentRound: null,
}),
getters: {
getRoundWinner() {
const goal = this.getCurrentRoundData?.goal;
if (!goal) {
return;
}
const winnerCoin = this.rounds[this.activeRound]?.token.find((token) => token.amount === goal);
if (winnerCoin) {
return winnerCoin;
} else {
return false;
}
},
getCurrentRoundData() {
console.log("this.rounds", this.rounds);
console.log("this.rounds[this.activeRound]", this.rounds[this.activeRound]);
return this.rounds[this.activeRound];
},
},
actions: {
async init() {
//fill initial round
},
async loadRewardsRounds() {
const walletStore = useWalletStoreV2();
const rewardLockerStore = useRewardLockerStore();
console.log("this.currentRound", this.currentRound);
const contracts = [];
for (let index = 0; index < this.currentRound; index++) {
console.log("load round: " + index);
// await this.fillRound(index)
console.log({ index, address: walletStore.address });
contracts.push({
...rewardLockerStore.contract,
functionName: "getTotalRewardsOf",
args: [walletStore.address, [index + 1]],
});
}
const data = await multicall({
contracts,
});
// var test = await rewardLockerStore.getTotalRewardsOf(walletStore.address, rounds)
for (let index = 0; index < data.length; index++) {
const element = data[index];
if (!this.rounds[index]) {
this.rounds[index] = {};
}
console.log("vor Fehler");
// this.rounds[index].round = index + 1;
// this.rounds[index].rewards_total = element.result;
}
},
async updateCurrentRound(sacrificedToken) {
console.log("updateCurrentRound");
const tokenStore = useTokenStore();
const memeBurnerStore = useMemeburnerStore();
var data = await memeBurnerStore.getSacrificedRound(this.currentRound);
var tokenIndex = this.rounds[this.currentRound].token.findIndex(
(tokenData) => tokenData.contractAddress === sacrificedToken
);
const token = tokenStore.getToken(sacrificedToken);
// console.log(
// "this.rounds[this.currentRound].token[tokenIndex].amount",
// this.rounds[this.currentRound].token[tokenIndex].amount
// );
// console.log("Number(data[tokenIndex].result[1] / BigInt(Math.pow(10, token.decimals))", Number(data[tokenIndex].result[1] / BigInt(Math.pow(10, token.decimals))));
this.rounds[this.currentRound].token[tokenIndex].amount =
Number(data[tokenIndex].result[1] / BigInt(Math.pow(10, token.decimals)))
},
async fillRound(roundNumber) {
console.log("fillRound");
const memeBurnerStore = useMemeburnerStore();
const tokenStore = useTokenStore();
const walletStore = useWalletStoreV2();
const rewardLockerStore = useRewardLockerStore();
// if (this.activeRound === this.currentRound) {
// console.log("activeRound is not completed");
// } else
if (!this.rounds[roundNumber]?.goal) {
console.log("round not filled --> fill round");
console.log("vor error");
var data = await memeBurnerStore.getSacrificedRound(roundNumber);
let rewardData = null;
if (walletStore.connected) {
console.log("hier1");
rewardData = await rewardLockerStore.getRewardsForAllToken(roundNumber);
console.log("hier2", rewardData);
}
var goal = await memeBurnerStore.getGoal(roundNumber);
var tokenArray = [];
var rewards_clan = 0;
var rewards_sacrifice = 0;
var rewards_staking = 0;
console.log("data", data);
for (let index = 0; index < data.length; index++) {
var tokenData = {};
const token = tokenStore.token[index];
var coefficent = await rewardLockerStore.getSacrificedTokenCoefficient(
roundNumber,
token.contractAddress
);
console.log("coefficent", coefficent);
tokenData.name = token.name;
tokenData.contractAddress = token.contractAddress;
tokenData.vamplogo = token.vamp.logo;
tokenData.vampname = token.vamp.name;
// tokenData.amount = data[index].result[1];
tokenData.amount = Number(data[index].result[1]) / Math.pow(10, token.decimals);
// tokenData.getAmountToReachGoal =
// Number(await memeBurnerStore.getAmountToReachGoal(token.contractAddress)) /
// Math.pow(10, token.decimals);
if (this.currentRound !== roundNumber && walletStore.connected) {
console.log("index", index);
console.log("rewardData[index]", rewardData[index]);
const stakeReward = Number(rewardData[index].result[0]);
const sacrificeClanReward = Number(rewardData[index].result[1]);
console.log("stakeReward", stakeReward);
console.log("sacrificeClanReward", sacrificeClanReward);
const clanwin = (Number(coefficent) / 10000 - 1) * sacrificeClanReward;
console.log("clanwin", clanwin);
const sacrificeRewards = Number(sacrificeClanReward) - clanwin;
console.log("sacrificeRewards", sacrificeRewards);
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);
}
console.log("clanwin123", rewards_clan);
const goalInBLD = await memeBurnerStore.getGoalInBLOOD(roundNumber);
this.rounds[roundNumber] = {
goal: Number(goal[0]) / Math.pow(10, tokenStore.memeToken.decimals),
goalInBLD: Number(goalInBLD / BigInt(Math.pow(10, tokenStore.memeToken.decimals))),
expirationTimestamp: Number(goal[1]),
rewards_clan: Number(rewards_clan),
rewards_staking: Number(rewards_staking),
rewards_sacrifice: Number(rewards_sacrifice),
rewards_total: Number(rewards_clan + rewards_sacrifice),
token: tokenArray,
};
console.log("this.rounds[roundNumber]", this.rounds[roundNumber]);
} else {
console.log("round already filled");
}
},
async getFinshedRoundNumbers() {
const array = [];
const rewardLockerStore = useRewardLockerStore();
const lastCompletedRound = await rewardLockerStore.lastCompletedRound();
console.log("lastCompletedRound", lastCompletedRound);
for (let index = 0; index <= Number(lastCompletedRound); index++) {
array.push(index);
}
// for (let index = 1; index <= Number(lastCompletedRound) + 1; index++) {
// array.push(index);
// }
console.log("array", array);
return array;
},
/**
* function to get finished rounds, active round and the next round to check if there is a round the user called unstakeOrdered.
* with Backend it would be much easier because we could store the event data in the database
*/
async getRoundsNumberWithNext() {
const array = [];
const rewardLockerStore = useRewardLockerStore();
const lastCompletedRound = await rewardLockerStore.lastCompletedRound();
for (let index = 1; index <= Number(lastCompletedRound) + 1; index++) {
array.push(index);
}
return array;
},
},
});