101 lines
3.7 KiB
JavaScript
101 lines
3.7 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import MemeBurnerJson from "@/assets/contracts/MemeBurner.json";
|
|
import { useTokenStore } from "./tokens";
|
|
import { useWalletStoreV2 } from "./walletV2";
|
|
import { useRoundsStore } from "@/stores/rounds";
|
|
|
|
import { multicall, getContract, fetchToken, waitForTransaction, getWalletClient } from "@wagmi/core";
|
|
|
|
|
|
export const useMemeburnerStore = defineStore("memeburner", {
|
|
state: () => ({
|
|
config: MemeBurnerJson,
|
|
contract: null,
|
|
loading: false,
|
|
}),
|
|
getters: {},
|
|
actions: {
|
|
async init() {
|
|
console.log("-----------init memeburner-------------");
|
|
const walletStore = useWalletStoreV2();
|
|
// const roundsStore = useRoundsStore();
|
|
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();
|
|
// var rewards = await this.contract.methods
|
|
// .getRewardFor(tokenStore.activeToken.contractAddress, amount)
|
|
// .call();
|
|
const rewards = await this.contract.read.getRewardFor([tokenStore.activeToken.contractAddress, amount]);
|
|
console.log("rewards", rewards);
|
|
return rewards;
|
|
},
|
|
async getCurrentRound() {
|
|
return await this.contract.read.currentRound();
|
|
},
|
|
async getGoal(roundnumber) {
|
|
return await this.contract.read.rounds([roundnumber]);
|
|
},
|
|
//returns participated, amount achieved
|
|
async getSacrificedMeme(roundnumber, tokenAddress) {
|
|
return await this.contract.read.getSacrificedMeme([roundnumber, tokenAddress]);
|
|
},
|
|
async getAmountToReachGoal(tokenAddress) {
|
|
return await this.contract.read.getAmountToReachGoal([tokenAddress]);
|
|
},
|
|
async getSacrificedRound(roundnumber) {
|
|
console.log("getSacrificedRound", roundnumber);
|
|
const tokenStore = useTokenStore();
|
|
const contracts = [];
|
|
for (let index = 0; index < tokenStore.sacrificeToken.length; index++) {
|
|
const token = tokenStore.sacrificeToken[index];
|
|
console.log("token.contractAddress", token.contractAddress);
|
|
console.log("roundnumber", roundnumber);
|
|
// contracts.push(this.contract.methods.getSacrificedMeme(roundnumber, token.contractAddress));
|
|
contracts.push({ ...this.contract, functionName: "getSacrificedMeme", args: [roundnumber, 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) {
|
|
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));
|
|
console.log("this.contract", this.contract);
|
|
console.log("amountDecimal", amountDecimal);
|
|
var hash = await this.contract.write
|
|
.sacrificeMeme([tokenStore.activeToken.contractAddress, amountDecimal])
|
|
console.log("hash", hash);
|
|
const data = await waitForTransaction({
|
|
hash: hash,
|
|
});
|
|
tokenStore.activeToken.allowance = await tokenStore.allowance();
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
async getGoalInBLD(roundNumber) {
|
|
return await this.contract.read.getGoalInBLD([roundNumber]);
|
|
},
|
|
},
|
|
});
|