init functions for the storage
This commit is contained in:
+76
-11
@@ -2,35 +2,100 @@ import { defineStore } from "pinia";
|
||||
import MemeBurnerJson from "@/assets/contracts/MemeBurner.json";
|
||||
import { useTokenStore } from "./tokens";
|
||||
import { useMultiCallStore } from "./multicall";
|
||||
import { useWalletStore } from "./wallet";
|
||||
import { useRoundsStore } from "@/stores/rounds";
|
||||
|
||||
export const useMemeburnerStore = defineStore("memeburner", {
|
||||
state: () => ({
|
||||
config: MemeBurnerJson,
|
||||
contract: null,
|
||||
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
async init() {
|
||||
console.log("-----------init memeburner-------------");
|
||||
const walletStore = useWalletStore();
|
||||
const roundsStore = useRoundsStore();
|
||||
|
||||
this.contract = new walletStore.web3.eth.Contract(this.config.abi, this.config.contractAddress);
|
||||
|
||||
|
||||
const websocketMemeBurner = new walletStore.web3socket.eth.Contract(
|
||||
this.config.abi,
|
||||
this.config.contractAddress
|
||||
);
|
||||
websocketMemeBurner.events
|
||||
.MemeSacrificed()
|
||||
.on("data", async (event) => {
|
||||
console.log("sacrificedAmount", event.returnValues.sacrificedAmount);
|
||||
console.log("sacrificedToken", event.returnValues.sacrificedToken);
|
||||
await roundsStore.updateCurrentRound(
|
||||
event.returnValues.sacrificedToken,
|
||||
event.returnValues.sacrificedAmount
|
||||
);
|
||||
})
|
||||
.on("error", (error) => console.log("MemeSacrificederror", error));
|
||||
},
|
||||
async getRewardsFor(amount) {
|
||||
const tokenStore = useTokenStore();
|
||||
var rewards = await this.contract.methods
|
||||
.getRewardFor(tokenStore.activeToken.contractAddress, amount)
|
||||
.call();
|
||||
return rewards;
|
||||
},
|
||||
async getCurrentRound() {
|
||||
return await this.contract.methods.currentRound().call();
|
||||
},
|
||||
async getGoal (roundnumber) {
|
||||
async getGoal(roundnumber) {
|
||||
return await this.contract.methods.rounds(roundnumber).call();
|
||||
},
|
||||
//returns participated, amount achieved
|
||||
async getSacrificedMeme(roundnumber, tokenAddress) {
|
||||
return await this.contract.methods.getSacrificedMeme(roundnumber, tokenAddress).call();
|
||||
},
|
||||
async getSacrificedRound(roundnumber) {
|
||||
console.log("getSacrificedRound", roundnumber);
|
||||
const tokenStore = useTokenStore();
|
||||
const calls = [];
|
||||
for (let index = 0; index < tokenStore.token.length; index++) {
|
||||
const token = tokenStore.token[index];
|
||||
},
|
||||
async getAmountToReachGoal(tokenAddress) {
|
||||
return await this.contract.methods.getAmountToReachGoal(tokenAddress).call();
|
||||
},
|
||||
async getSacrificedRound(roundnumber) {
|
||||
console.log("getSacrificedRound", roundnumber);
|
||||
const tokenStore = useTokenStore();
|
||||
const calls = [];
|
||||
// const calls1 = [];
|
||||
for (let index = 0; index < tokenStore.token.length; index++) {
|
||||
const token = tokenStore.token[index];
|
||||
console.log("token.contractAddress", token.contractAddress);
|
||||
console.log("roundnumber", roundnumber);
|
||||
var test = await this.getSacrificedMeme(roundnumber, token.contractAddress);
|
||||
console.log("test", test);
|
||||
calls.push(this.contract.methods.getSacrificedMeme(roundnumber, token.contractAddress));
|
||||
}
|
||||
const multicallStore = useMultiCallStore();
|
||||
// calls1.push(this.contract.methods.getAmountToReachGoal(token.contractAddress));
|
||||
}
|
||||
console.log("this.contract", this.contract);
|
||||
const multicallStore = useMultiCallStore();
|
||||
return await multicallStore.aggregate(calls);
|
||||
|
||||
},
|
||||
async sacrificeMeme(amount) {
|
||||
const walletStore = useWalletStore();
|
||||
const tokenStore = useTokenStore();
|
||||
try {
|
||||
if (!amount || amount == 0) {
|
||||
//todo error
|
||||
console.error("amount is 0 or null or undefined");
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
const amountDecimal = BigInt(amount * Math.pow(10, tokenStore.activeToken.decimals));
|
||||
var reward = await this.contract.methods
|
||||
.sacrificeMeme(tokenStore.activeToken.contractAddress, amountDecimal)
|
||||
.send({ from: walletStore.address });
|
||||
console.log("reward", reward);
|
||||
tokenStore.activeToken.allowance = await this.allowance();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -10,6 +10,15 @@ export const useMultiCallStore = defineStore("multicall", {
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
async init() {
|
||||
console.log("-----------init multicall-------------");
|
||||
const walletStore = useWalletStore();
|
||||
|
||||
this.contract = new walletStore.web3.eth.Contract(
|
||||
this.config.abi,
|
||||
this.config.contractAddress
|
||||
);
|
||||
},
|
||||
async aggregate(calls, parameters = {}) {
|
||||
const callRequests = calls.map((call) => {
|
||||
const callData = call.encodeABI();
|
||||
|
||||
@@ -3,14 +3,21 @@ import RewardLockerJson from "@/assets/contracts/RewardLocker.json";
|
||||
import { useTokenStore } from "./tokens";
|
||||
import { useMultiCallStore } from "./multicall";
|
||||
import { useWalletStore } from "./wallet";
|
||||
import { ethers } from "ethers";
|
||||
|
||||
export const useRewardLockerStore = defineStore("rewardlocker", {
|
||||
state: () => ({
|
||||
config: RewardLockerJson,
|
||||
contract: null,
|
||||
ethcontract: null,
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
async init() {
|
||||
console.log("-----------init rewardlocker-------------");
|
||||
const walletStore = useWalletStore();
|
||||
this.contract = new walletStore.web3.eth.Contract(this.config.abi, this.config.contractAddress);
|
||||
},
|
||||
/*
|
||||
* @param {*} walletAddress
|
||||
* @param {*} roundNumber
|
||||
@@ -20,23 +27,31 @@ export const useRewardLockerStore = defineStore("rewardlocker", {
|
||||
async getRewards(walletAddress, roundNumber, tokenAddress) {
|
||||
return await this.contract.methods.rewards(walletAddress, roundNumber, tokenAddress).call();
|
||||
},
|
||||
async getRewardsForAllToken(roundNumber) {
|
||||
console.log("getRewardsForAllToken");
|
||||
const tokenStore = useTokenStore();
|
||||
const walletStore = useWalletStore();
|
||||
async getRewardsForAllToken(roundNumber) {
|
||||
console.log("getRewardsForAllToken");
|
||||
const tokenStore = useTokenStore();
|
||||
const walletStore = useWalletStore();
|
||||
const calls = [];
|
||||
for (let index = 0; index < tokenStore.token.length; index++) {
|
||||
const token = tokenStore.token[index];
|
||||
const token = tokenStore.token[index];
|
||||
var test = this.contract.methods.rewards(walletStore.address, roundNumber, token.contractAddress);
|
||||
console.log("test123", test);
|
||||
calls.push(this.contract.methods.rewards(walletStore.address, roundNumber, token.contractAddress));
|
||||
}
|
||||
const multicallStore = useMultiCallStore();
|
||||
return await multicallStore.aggregate(calls);
|
||||
return await multicallStore.aggregate(calls);
|
||||
},
|
||||
async getSacrificedTokenCoefficient(roundNumber, sacrificedToken) {
|
||||
return await this.contract.methods.getSacrificedTokenCoefficient(roundNumber, sacrificedToken).call();
|
||||
},
|
||||
async lastCompletedRound() {
|
||||
return await this.contract.methods.lastCompletedRound().call();
|
||||
},
|
||||
async claim(round) {
|
||||
return await this.contract.methods.claim(round).call();
|
||||
async claim(round) {
|
||||
const walletStore = useWalletStore();
|
||||
// return await this.contract.methods.claim(round).call();
|
||||
console.log(this.contract.methods.claim(round));
|
||||
return await this.contract.methods.claim(round).send({from: walletStore.address, to: this.contractAddress});
|
||||
},
|
||||
async getTotalRewardsOf(address, rounds) {
|
||||
return await this.contract.methods.getTotalRewardsOf(address, rounds).call();
|
||||
|
||||
+46
-8
@@ -17,12 +17,17 @@ export const useRoundsStore = defineStore("rounds", {
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async init() {
|
||||
//fill initial round
|
||||
},
|
||||
async updateCurrentRound(sacrificedToken, sacrificedAmount) {
|
||||
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][1];
|
||||
|
||||
},
|
||||
async fillRound() {
|
||||
console.log("fillRound");
|
||||
@@ -34,27 +39,60 @@ export const useRoundsStore = defineStore("rounds", {
|
||||
// console.log("activeRound is not completed");
|
||||
// } else
|
||||
if (!this.rounds[this.activeRound]) {
|
||||
console.log("round not filled --> fill round");
|
||||
var data = await memeBurnerStore.getSacrificedRound(this.activeRound)
|
||||
console.log("round not filled --> fill round");
|
||||
console.log("vor error");
|
||||
var data = await memeBurnerStore.getSacrificedRound(this.activeRound)
|
||||
var rewardData = await rewardLockerStore.getRewardsForAllToken(this.activeRound);
|
||||
var goal = await memeBurnerStore.getGoal(this.activeRound);
|
||||
console.log("data", data);
|
||||
var goal = await memeBurnerStore.getGoal(this.activeRound);
|
||||
console.log("goal", goal);
|
||||
console.log("this.activeRound", this.activeRound);
|
||||
console.log("dataabc", data);
|
||||
console.log("rewardData", rewardData);
|
||||
console.log("pepecoin-rewards", rewardData[0]);
|
||||
|
||||
var tokenArray = []
|
||||
var rewards_clan = BigInt(0);
|
||||
var rewards_sacrifice = BigInt(0);
|
||||
for (let index = 0; index < data.length; index++) {
|
||||
var tokenData = {};
|
||||
const token = tokenStore.token[index];
|
||||
console.log("testabcv", token);
|
||||
console.log("123abc", token.contractAddress);
|
||||
var testcoef = await rewardLockerStore.getSacrificedTokenCoefficient(this.activeRound, token.contractAddress);
|
||||
console.log("testcoef",);
|
||||
console.log(`rewards ${token.name}`, BigInt(rewardData[index]));
|
||||
console.log(`clanwin multiplier ${token.name}`, (testcoef / 10000));
|
||||
const clanwin = BigInt((testcoef / 10000 -1) * rewardData[index]);
|
||||
console.log(`clanwin ${token.name}`, clanwin);
|
||||
const sacrificeRewards = BigInt(rewardData[index]) - clanwin;
|
||||
console.log(`sacrifice rewards`, sacrificeRewards);
|
||||
console.log("token", token);
|
||||
tokenData.name = token.name;
|
||||
tokenData.contractAddress = token.contractAddress;
|
||||
tokenData.vamplogo = token.vamp.logo;
|
||||
tokenData.vampname = token.vamp.name;
|
||||
tokenData.amount = data[index][1];
|
||||
tokenData.rewards = rewardData[index]
|
||||
tokenData.getAmountToReachGoal= await memeBurnerStore.getAmountToReachGoal(token.contractAddress);
|
||||
tokenData.rewards_ges = rewardData[index];
|
||||
if (this.currentRound !== this.activeRound) {
|
||||
tokenData.rewards_clan = clanwin;
|
||||
console.log("clanwin", clanwin);
|
||||
tokenData.rewards_sacrifice = sacrificeRewards;
|
||||
rewards_clan = rewards_clan + BigInt(clanwin / BigInt(Math.pow(10, token.decimals)));
|
||||
rewards_sacrifice =
|
||||
rewards_sacrifice + BigInt(sacrificeRewards / BigInt(Math.pow(10, token.decimals)));;
|
||||
}
|
||||
tokenArray.push(tokenData);
|
||||
}
|
||||
this.rounds[this.activeRound] = {
|
||||
goal: goal, token: tokenArray
|
||||
}
|
||||
|
||||
console.log("Math.pow(10, tokenStore.memeToken.decimals)", Math.pow(10, tokenStore.memeToken.decimals));
|
||||
console.log("goal", goal);
|
||||
this.rounds[this.activeRound] = {
|
||||
goal: goal,
|
||||
rewards_clan: rewards_clan,
|
||||
rewards_sacrifice: rewards_sacrifice,
|
||||
token: tokenArray,
|
||||
};
|
||||
} else {
|
||||
console.log("round already filled");
|
||||
}
|
||||
|
||||
+33
-48
@@ -1,7 +1,10 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { markRaw } from "vue";
|
||||
import Web3 from "web3";
|
||||
import { useWalletStore } from "./wallet";
|
||||
import { useContractStore } from "./contracts";
|
||||
import { ethers } from "ethers";
|
||||
import { useMemeburnerStore } from "./memeburner";
|
||||
import Meme1Json from "@/assets/contracts/Meme1.json";
|
||||
import Meme2Json from "@/assets/contracts/Meme2.json";
|
||||
import MemeToken from "@/assets/contracts/MemeToken.json";
|
||||
@@ -21,6 +24,7 @@ export const useTokenStore = defineStore("token", {
|
||||
backgroundColor: "#4b8501",
|
||||
abi: Meme1Json.abi,
|
||||
contractAddress: Meme1Json.contractAddress,
|
||||
ethcontract: null,
|
||||
},
|
||||
{
|
||||
name: "$SHIB",
|
||||
@@ -53,19 +57,31 @@ export const useTokenStore = defineStore("token", {
|
||||
allowance: null,
|
||||
},
|
||||
loading: false,
|
||||
memeToken: {
|
||||
config: {
|
||||
abi: MemeToken.abi,
|
||||
contractAddress: MemeToken.contractAddress
|
||||
}
|
||||
},
|
||||
memeBurner: {
|
||||
config: MemeBurnerJson,
|
||||
contract: null,
|
||||
memeToken: {
|
||||
config: {
|
||||
abi: MemeToken.abi,
|
||||
contractAddress: MemeToken.contractAddress,
|
||||
},
|
||||
},
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
async init() {
|
||||
console.log("-----------init token-------------");
|
||||
const walletStore = useWalletStore();
|
||||
for (let index = 0; index < this.token.length; index++) {
|
||||
const token = this.token[index];
|
||||
token.contract = new walletStore.web3.eth.Contract(token.abi, token.contractAddress);
|
||||
token.decimals = parseInt(await token.contract.methods.decimals().call());
|
||||
}
|
||||
|
||||
//MemeToken init
|
||||
this.memeToken.contract = new walletStore.web3.eth.Contract(
|
||||
this.memeToken.config.abi,
|
||||
this.memeToken.config.contractAddress
|
||||
);
|
||||
this.memeToken.decimals = parseInt(await this.memeToken.contract.methods.decimals().call());
|
||||
},
|
||||
async changeActiveToken(token) {
|
||||
this.activeToken = token;
|
||||
const web3 = new Web3(window.ethereum);
|
||||
@@ -77,28 +93,29 @@ export const useTokenStore = defineStore("token", {
|
||||
}
|
||||
},
|
||||
async allowance() {
|
||||
console.log("allowance");
|
||||
const walletStore = useWalletStore();
|
||||
console.log("walletStore.address", walletStore.address);
|
||||
const allowance = await this.contract.methods
|
||||
.allowance(walletStore.address, MemeBurnerJson.contractAddress)
|
||||
const memeBurnerStore = useMemeburnerStore();
|
||||
var allowance = await this.contract.methods
|
||||
.allowance(walletStore.address, memeBurnerStore.config.contractAddress)
|
||||
.call();
|
||||
console.log("allowance", allowance);
|
||||
var decimals = await this.contract.methods.decimals().call();
|
||||
console.log("decimals", decimals);
|
||||
console.log("Math.pow(10, decimals)", Math.pow(10, decimals));
|
||||
allowance = BigInt(allowance / Math.pow(10, decimals));
|
||||
return allowance;
|
||||
},
|
||||
async approveToken(amount) {
|
||||
try {
|
||||
console.log("amount", amount);
|
||||
this.loading = true;
|
||||
const walletStore = useWalletStore();
|
||||
const memeBurnerStore = useMemeburnerStore();
|
||||
var approve = await this.contract.methods
|
||||
.approve(this.memeBurner.config.contractAddress, amount)
|
||||
.approve(memeBurnerStore.config.contractAddress, amount)
|
||||
.send({ from: walletStore.address });
|
||||
//todo parameter value übergeben in eth web3.utils.toHex(web3.utils.toWei(amount, 'ether'))
|
||||
|
||||
// .send({ from: walletStore.address, value: 0.5 });
|
||||
console.log("approve", approve);
|
||||
this.activeToken.allowance = await this.allowance();
|
||||
return approve;
|
||||
} catch (error) {
|
||||
@@ -107,42 +124,10 @@ export const useTokenStore = defineStore("token", {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
async sacrificeToken(amount) {
|
||||
try {
|
||||
console.log("amount", amount);
|
||||
if (!amount || amount == 0) {
|
||||
//todo error
|
||||
console.error("amount is 0 or null or undefined");
|
||||
return;
|
||||
}
|
||||
this.loading = true;
|
||||
console.log("this.activeToken.", this.activeToken.contractAddress);
|
||||
const walletStore = useWalletStore();
|
||||
console.log("walletStore.address", walletStore.address);
|
||||
var reward = await this.memeBurner.contract.methods
|
||||
.sacrificeMeme(this.activeToken.contractAddress, amount)
|
||||
.send({ from: walletStore.address });
|
||||
console.log("reward", reward);
|
||||
this.activeToken.allowance = await this.allowance();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
async getRewardsFor(amount) {
|
||||
const contractsStore = useContractStore();
|
||||
var rewards = await contractsStore.list.memeBurnerPublic.methods
|
||||
.getRewardFor(this.activeToken.contractAddress, amount)
|
||||
.call();
|
||||
return rewards;
|
||||
},
|
||||
getTokenContract(token) {
|
||||
console.log("token", token);
|
||||
const contractAddress = token.contractAddress;
|
||||
console.log("contractAddress", contractAddress);
|
||||
const activeToken = this.token.find((obj) => obj.contractAddress === contractAddress);
|
||||
console.log("activeToken", activeToken);
|
||||
const web3 = new Web3(window.ethereum);
|
||||
const contract = new web3.eth.Contract(activeToken.abi, contractAddress);
|
||||
return contract;
|
||||
|
||||
+38
-27
@@ -1,4 +1,5 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { markRaw } from "vue";
|
||||
import { useTokenStore } from "./tokens";
|
||||
import Web3 from "web3";
|
||||
export const useWalletStore = defineStore("wallet", {
|
||||
@@ -7,32 +8,41 @@ export const useWalletStore = defineStore("wallet", {
|
||||
alchemy: null,
|
||||
connected: false,
|
||||
address: null,
|
||||
web3: null
|
||||
web3: null,
|
||||
provider: null
|
||||
}),
|
||||
getters: {
|
||||
},
|
||||
actions: {
|
||||
actions: {
|
||||
async init() {
|
||||
const goerliKey = import.meta.env.VITE_GOERLI_KEY;
|
||||
if (this.connected) {
|
||||
const web3 = new Web3(window.ethereum);
|
||||
this.web3 = markRaw(web3);
|
||||
// provider = new ethers.BrowserProvider(window.ethereum);
|
||||
} else {
|
||||
const url = `https://eth-goerli.g.alchemy.com/v2/${goerliKey}`;
|
||||
this.web3 = new Web3(url);
|
||||
// provider = new ethers.AlchemyProvider("goerli", goerliKey);
|
||||
}
|
||||
|
||||
//Websocket
|
||||
this.web3socket = new Web3(
|
||||
new Web3.providers.WebsocketProvider(`wss://eth-goerli.g.alchemy.com/v2/${goerliKey}`)
|
||||
);
|
||||
},
|
||||
increment() {
|
||||
this.count++;
|
||||
},
|
||||
connectWallet() {
|
||||
console.log("window.ethereum", window.ethereum);
|
||||
const tokenStore = useTokenStore();
|
||||
// if (window.ethereum) {
|
||||
window.ethereum
|
||||
.request({ method: "eth_requestAccounts" })
|
||||
.then((data) => {
|
||||
console.log("data", data);
|
||||
this.isConnected();
|
||||
// this.address = data[0];
|
||||
// this.connected = true;
|
||||
// const tokenStore = useTokenStore();
|
||||
// if (tokenStore.activeToken.address) {
|
||||
// //Token selected but not refreshed
|
||||
// this.getTokenBalance(tokenStore.activeToken.address).then(
|
||||
// (data) => (tokenStore.activeToken.balance = data)
|
||||
// );
|
||||
// }
|
||||
// console.log("permissions", permissions);
|
||||
this.address = data[0];
|
||||
this.connected = true;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.code === 4001) {
|
||||
@@ -58,19 +68,19 @@ export const useWalletStore = defineStore("wallet", {
|
||||
this.connected = true;
|
||||
this.address = accounts[0];
|
||||
console.log(`You're connected to: ${accounts[0]}`);
|
||||
const web3 = new Web3(window.ethereum);
|
||||
const tokenStore = useTokenStore();
|
||||
console.log("tokenStore.activeToken", tokenStore.activeToken);
|
||||
if (tokenStore.activeToken.contractAddress) {
|
||||
tokenStore.activeToken.balance = await this.getTokenBalance(
|
||||
tokenStore.activeToken
|
||||
);
|
||||
tokenStore.activeToken.allowance = await tokenStore.allowance();
|
||||
}
|
||||
tokenStore.memeBurner.contract = new web3.eth.Contract(
|
||||
tokenStore.memeBurner.config.abi,
|
||||
tokenStore.memeBurner.config.contractAddress
|
||||
);
|
||||
// const web3 = new Web3(window.ethereum);
|
||||
// const tokenStore = useTokenStore();
|
||||
// console.log("tokenStore.activeToken", tokenStore.activeToken);
|
||||
// if (tokenStore.activeToken.contractAddress) {
|
||||
// tokenStore.activeToken.balance = await this.getTokenBalance(
|
||||
// tokenStore.activeToken
|
||||
// );
|
||||
// tokenStore.activeToken.allowance = await tokenStore.allowance();
|
||||
// }
|
||||
// tokenStore.memeBurner.contract = new web3.eth.Contract(
|
||||
// tokenStore.memeBurner.config.abi,
|
||||
// tokenStore.memeBurner.config.contractAddress
|
||||
// );
|
||||
} else {
|
||||
console.log("Metamask is not connected");
|
||||
this.connected = false;
|
||||
@@ -79,6 +89,7 @@ export const useWalletStore = defineStore("wallet", {
|
||||
console.log("error", error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
return this.connected
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user