192 lines
6.5 KiB
JavaScript
192 lines
6.5 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import RewardLockerJson from "@/assets/contracts/RewardLocker.json";
|
|
import { useTokenStore } from "./tokens";
|
|
import { useWalletStoreV2 } from "./walletV2";
|
|
import { useRoundsStore } from "@/stores/rounds";
|
|
import { createPermitObject, getSignatureRSV } from "@/helper/blockchain.js";
|
|
import { multicall, getContract, signTypedData, getAccount, waitForTransaction } from "@wagmi/core";
|
|
|
|
|
|
export const useRewardLockerStore = defineStore("rewardlocker", {
|
|
state: () => ({
|
|
config: RewardLockerJson,
|
|
contract: null,
|
|
ethcontract: null,
|
|
}),
|
|
getters: {},
|
|
actions: {
|
|
async init() {
|
|
console.log("-----------init rewardlocker-------------");
|
|
const walletStore = useWalletStoreV2();
|
|
this.contract = getContract({
|
|
address: this.config.contractAddress,
|
|
abi: this.config.abi,
|
|
walletClient: walletStore.walletClient || walletStore.publicClient,
|
|
});
|
|
},
|
|
/*
|
|
* @param {*} walletAddress
|
|
* @param {*} roundNumber
|
|
* @param {*} tokenAddress
|
|
* @returns Rewards for specific token and specific round
|
|
*/
|
|
async getRewards(walletAddress, roundNumber, tokenAddress) {
|
|
try {
|
|
return await this.contract.read.rewards([walletAddress, roundNumber, tokenAddress]);
|
|
} catch (error) {
|
|
console.error("getRewards", error);
|
|
|
|
}
|
|
},
|
|
async getRewardsForAllToken(roundNumber) {
|
|
console.log("getRewardsForAllToken");
|
|
const tokenStore = useTokenStore();
|
|
const walletStore = useWalletStoreV2();
|
|
const contracts = [];
|
|
for (let index = 0; index < tokenStore.sacrificeToken.length; index++) {
|
|
const token = tokenStore.sacrificeToken[index];
|
|
// calls.push(this.contract.methods.rewards(walletStore.address, roundNumber, token.contractAddress));
|
|
contracts.push({
|
|
...this.contract,
|
|
functionName: "getRewards",
|
|
args: [walletStore.address, roundNumber, token.contractAddress],
|
|
});
|
|
}
|
|
const data = await multicall({
|
|
contracts,
|
|
});
|
|
// const multicallStore = useMultiCallStore();
|
|
return data;
|
|
},
|
|
async getSacrificedTokenCoefficient(roundNumber, sacrificedToken) {
|
|
try {
|
|
return await this.contract.read.getSacrificedTokenCoefficient([roundNumber, sacrificedToken]);
|
|
} catch (error) {
|
|
console.error("getSacrificedTokenCoefficient", error);
|
|
}
|
|
},
|
|
async lastCompletedRound() {
|
|
try {
|
|
return await this.contract.read.lastCompletedRound();
|
|
} catch (error) {
|
|
console.error("lastCompletedRound", error);
|
|
}
|
|
},
|
|
async claim(round) {
|
|
try {
|
|
console.log("claim");
|
|
return await this.contract.write.claim([round]);
|
|
} catch (error) {
|
|
console.error("claim", error);
|
|
|
|
}
|
|
},
|
|
async claimAll() {
|
|
console.log("claimAll");
|
|
const roundsStore = useRoundsStore();
|
|
const account = getAccount();
|
|
console.log("acocunt", account);
|
|
|
|
const rounds = await roundsStore.getFinshedRoundNumbers();
|
|
return await this.contract.write.claim([rounds]);
|
|
},
|
|
async getTotalRewardsOf(address, rounds) {
|
|
try {
|
|
return await this.contract.read.getTotalRewardsOf([address, rounds]);
|
|
} catch (error) {
|
|
console.error("getTotalRewardsOf", error);
|
|
|
|
}
|
|
},
|
|
async getTotalRewards(address) {
|
|
try {
|
|
console.log("this.contract", this.contract);
|
|
const roundsStore = useRoundsStore();
|
|
const rounds = await roundsStore.getFinshedRoundNumbers();
|
|
return await this.contract.read.getTotalRewardsOf([address, rounds]);
|
|
} catch (error) {
|
|
console.error("getTotalRewards", error);
|
|
|
|
}
|
|
},
|
|
async getStaked(address) {
|
|
try {
|
|
return await this.contract.read.staked([address]);
|
|
} catch (error) {
|
|
console.error("getStaked", error);
|
|
}
|
|
},
|
|
async orderUnstake() {
|
|
try {
|
|
return await this.contract.write.orderUnstake();
|
|
} catch (error) {
|
|
console.error("orderUnstake", error);
|
|
}
|
|
},
|
|
async unstake(unstakeRound) {
|
|
try {
|
|
return await this.contract.write.unstake([unstakeRound]);
|
|
} catch (error) {
|
|
console.error("unstake", error);
|
|
}
|
|
},
|
|
async stakeBloodline(tokenNumber) {
|
|
console.log("tokenNumber", tokenNumber);
|
|
const walletStore = useWalletStoreV2();
|
|
const tokenStore = useTokenStore();
|
|
|
|
const fromAddress = walletStore.address;
|
|
const spender = this.config.contractAddress;
|
|
const deadline = Date.now() + 20 * 60;
|
|
const decimals = tokenStore.getToken(walletStore.memeToken.contractAddress).decimals;
|
|
const value = (tokenNumber * Math.pow(10, decimals)).toString();
|
|
const nonce = await tokenStore.memeToken.contract.read.nonces([fromAddress]);
|
|
console.log("nonce", nonce);
|
|
const { types, message, domain, primaryType } = createPermitObject(
|
|
fromAddress,
|
|
spender,
|
|
nonce,
|
|
deadline,
|
|
value
|
|
);
|
|
|
|
const signature = await signTypedData({
|
|
domain: domain,
|
|
message: message,
|
|
primaryType: primaryType,
|
|
types: types,
|
|
});
|
|
|
|
const { r, s, v } = getSignatureRSV(signature);
|
|
console.log("value, deadline, v, r, s", value, deadline, v, r, s);
|
|
const hash = await this.contract.write.stakeToBeSecured([value, deadline, v, r, s]);
|
|
return hash;
|
|
|
|
},
|
|
async stakeAndClaimRound(stakeCoefficient, round) {
|
|
// stakeCoefficient 25% = 2500, 50% = 5000
|
|
const tokenStore = useTokenStore();
|
|
const addresses = tokenStore.sacrificeToken.map((obj) => obj.contractAddress);
|
|
const hash = await this.contract.write.stakeAndClaim([stakeCoefficient, [round], addresses]);
|
|
const data = await waitForTransaction({
|
|
hash: hash,
|
|
});
|
|
const walletStore = useWalletStoreV2();
|
|
await walletStore.loadWalletData();
|
|
// todo reload roundData and wallet data
|
|
},
|
|
async stakeAndClaimAllRounds(stakeCoefficient) {
|
|
console.log("stakeAndClaimAllRounds");
|
|
const tokenStore = useTokenStore();
|
|
const roundsStore = useRoundsStore();
|
|
const rounds = await roundsStore.getFinshedRoundNumbers();
|
|
const addresses = tokenStore.sacrificeToken.map((obj) => obj.contractAddress);
|
|
console.log({ stakeCoefficient, rounds, addresses });
|
|
console.log("stakeCoefficient", stakeCoefficient);
|
|
var stakeCoefficientInt = Math.round(stakeCoefficient);
|
|
const hash = await this.contract.write.stakeAndClaim([stakeCoefficientInt, rounds, addresses]);
|
|
return hash;
|
|
},
|
|
},
|
|
});
|