import { defineStore } from "pinia"; import MemeAltarJson from "@/assets/contracts/MemeAltar.json"; import { useTokenStore } from "./tokens"; import { useWalletStoreV2 } from "./walletV2"; import { useCycleStore } from "@/stores/cycles"; import { multicall, getContract, fetchToken, waitForTransaction, getWalletClient } from "@wagmi/core"; export const useMemeburnerStore = defineStore("memeburner", { state: () => ({ config: MemeAltarJson, contract: null, loading: false, }), getters: {}, actions: { async init() { console.log("-----------init memeburner-------------"); const walletStore = useWalletStoreV2(); // const cycleStore = useCycleStore(); console.log("walletStore.walletClient", walletStore.walletClient); this.contract = getContract({ address: this.config.contractAddress, abi: this.config.abi, walletClient: walletStore.walletClient || walletStore.publicClient, }); console.log("this.contract", this.contract); }, async getRewardFor(amount) { const tokenStore = useTokenStore(); const rewards = await this.contract.read.getRewardForSacrifice([ tokenStore.activeToken.contractAddress, amount, ]); console.log("amount", amount); console.log("rewards", rewards); return rewards; }, async getCurrentCycle() { return await this.contract.read.currentCycle(); }, async getGoal(cycleNumber) { return await this.contract.read.cycles([cycleNumber]); }, //returns participated, amount achieved async getSacrificedMeme(cycleNumber, tokenAddress) { return await this.contract.read.getSacrificedMeme([cycleNumber, tokenAddress]); }, async getAmountToReachGoal(tokenAddress) { return await this.contract.read.getAmountToReachGoal([tokenAddress]); }, async getSacrificedCycle(cycleNumber) { console.log("getSacrificedCycle", cycleNumber); const tokenStore = useTokenStore(); const contracts = []; for (let index = 0; index < tokenStore.sacrificeToken.length; index++) { const token = tokenStore.sacrificeToken[index]; contracts.push({ ...this.contract, functionName: "getSacrificedMeme", args: [cycleNumber, token.contractAddress], }); } // console.log("this.contract", this.contract); // const multicallStore = useMultiCallStore(); // return await multicallStore.aggregate(calls); const data = await multicall({ contracts, }); return data; }, async sacrificeMeme(amount, minETH) { const tokenStore = useTokenStore(); try { if (!amount || amount == 0) { //todo error console.error("amount is 0 or null or undefined"); return; } this.loading = true; // TODO: store all integer values as one type: BigInt const amountDecimal = BigInt(amount) * 10n ** BigInt(tokenStore.activeToken.decimals); var hash = await this.contract.write.sacrificeMeme([ tokenStore.activeToken.contractAddress, amountDecimal.toString(), minETH.toString() ]); return hash; } catch (error) { console.error(error); throw new Error(error); } finally { this.loading = false; } }, async sacrificeMemeAndBuyBLOOD(amount, minBLOOD) { const tokenStore = useTokenStore(); const amountDecimal = BigInt(amount) * 10n ** BigInt(tokenStore.activeToken.decimals); var hash = await this.contract.write.sacrificeMemeAndBuyBLOOD([ tokenStore.activeToken.contractAddress, amountDecimal.toString(), minBLOOD.toString() ]); return hash; }, async getGoalInBLOOD(cycleNumber) { return await this.contract.read.getGoalInBLOOD([cycleNumber]); }, async getAmountETHBase(amount) { console.log("getAmountETHBase"); const tokenStore = useTokenStore(); return await this.contract.read.getAmountETHBase([tokenStore.activeToken.contract.address, amount, true]); } }, });