144 lines
6.2 KiB
JavaScript
144 lines
6.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;
|
|
const winnerCoin = this.rounds[this.activeRound]?.token.find((token) => token.amount === goal)
|
|
if (winnerCoin) {
|
|
return winnerCoin;
|
|
} else {
|
|
return false;
|
|
}
|
|
},
|
|
getCurrentRoundData() {
|
|
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,
|
|
});
|
|
console.log("data123abc", data);
|
|
// 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] = {};
|
|
}
|
|
this.rounds[index].round = index + 1
|
|
this.rounds[index].rewards_total = element.result
|
|
}
|
|
},
|
|
async updateCurrentRound(sacrificedToken) {
|
|
const tokenStore = useTokenStore();
|
|
const memeBurnerStore = useMemeburnerStore();
|
|
var data = await memeBurnerStore.getSacrificedRound(this.currentRound);
|
|
console.log("data123", data);
|
|
var tokenIndex = this.rounds[this.currentRound].token.findIndex((tokenData) => tokenData.contractAddress === sacrificedToken);
|
|
this.rounds[this.currentRound].token[tokenIndex].amount = data[tokenIndex].result[1];
|
|
|
|
},
|
|
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) {
|
|
rewardData = await rewardLockerStore.getRewardsForAllToken(roundNumber);
|
|
}
|
|
console.log("rewardData123", rewardData);
|
|
|
|
var goal = await memeBurnerStore.getGoal(roundNumber);
|
|
console.log("goal", goal);
|
|
var tokenArray = []
|
|
var rewards_clan = 0
|
|
var rewards_sacrifice = 0
|
|
for (let index = 0; index < data.length; index++) {
|
|
var tokenData = {};
|
|
const token = tokenStore.token[index];
|
|
var testcoef = await rewardLockerStore.getSacrificedTokenCoefficient(roundNumber, token.contractAddress);
|
|
|
|
|
|
|
|
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.getAmountToReachGoal= await memeBurnerStore.getAmountToReachGoal(token.contractAddress);
|
|
if (this.currentRound !== roundNumber && walletStore.connected) {
|
|
const clanwin = (Number(testcoef) / 10000 - 1) * Number(rewardData[index].result);
|
|
const sacrificeRewards = Number(rewardData[index].result) - clanwin;
|
|
|
|
tokenData.rewards_ges = BigInt(rewardData[index].result);
|
|
tokenData.rewards_clan = BigInt(clanwin);
|
|
tokenData.rewards_sacrifice = BigInt(sacrificeRewards);
|
|
|
|
rewards_clan = rewards_clan + (clanwin / Math.pow(10, token.decimals));
|
|
rewards_sacrifice =
|
|
rewards_sacrifice + sacrificeRewards / Math.pow(10, token.decimals);
|
|
}
|
|
tokenArray.push(tokenData);
|
|
}
|
|
const goalInBLD = await memeBurnerStore.getGoalInBLD(roundNumber);
|
|
this.rounds[roundNumber] = {
|
|
goal: goal[0],
|
|
goalInBLD: goalInBLD / BigInt(Math.pow(10, tokenStore.memeToken.decimals)),
|
|
expirationTimestamp: goal[1],
|
|
rewards_clan: BigInt(rewards_clan),
|
|
rewards_sacrifice: BigInt(rewards_sacrifice),
|
|
rewards_total: BigInt(rewards_clan + rewards_sacrifice),
|
|
token: tokenArray,
|
|
};
|
|
console.log("this.rounds[roundNumber]", this.rounds[roundNumber]);
|
|
} else {
|
|
console.log("round already filled");
|
|
}
|
|
console.log("this.rounds123", this.rounds);
|
|
},
|
|
},
|
|
});
|