sacrifice: refactoring.
This commit is contained in:
+41
-33
@@ -13,7 +13,7 @@ import { formatNumber } from "@/helper/helper.js";
|
||||
|
||||
export const useSacrificeStore = defineStore("sacrifice", {
|
||||
state: () => ({
|
||||
sacrificeNumber: null,
|
||||
sacrificeNumber: 0n,
|
||||
sacrificeState: 0,
|
||||
}),
|
||||
getters: {},
|
||||
@@ -33,11 +33,11 @@ export const useSacrificeStore = defineStore("sacrifice", {
|
||||
let hash = null;
|
||||
if (getRewards.variant === "sacrifice") {
|
||||
console.log("sacrifice");
|
||||
hash = await memeburnerStore.sacrificeMeme(amount);
|
||||
hash = await memeburnerStore.sacrificeMeme(amount, getRewards.minETH);
|
||||
console.log("hash", hash);
|
||||
} else {
|
||||
console.log("buy");
|
||||
hash = await memeburnerStore.sacrificeMemeAndBuyBLOOD(amount);
|
||||
hash = await memeburnerStore.sacrificeMemeAndBuyBLOOD(amount, getRewards.minBLOOD);
|
||||
console.log("hash", hash);
|
||||
}
|
||||
|
||||
@@ -89,34 +89,41 @@ export const useSacrificeStore = defineStore("sacrifice", {
|
||||
const uniswapStore = useUniswapStore();
|
||||
const walletStore = useWalletStoreV2();
|
||||
const rewardLockerStore = useRewardLockerStore();
|
||||
|
||||
|
||||
// what is value here? i thought amount and value are same
|
||||
const amountDecimal = BigInt(value) * 10n ** BigInt(token.decimals == undefined ? 18n : token.decimals);
|
||||
let output = 0;
|
||||
const amountETHBase = await memeburnerStore.getAmountETHBase(amountDecimal);
|
||||
// take 99.5% of amount ETH got for selling meme, 0.5% allowed slippage
|
||||
const minETH = amountETHBase[1] * 995n / 1000n;
|
||||
|
||||
let minBLOOD;
|
||||
|
||||
try {
|
||||
if (!value || isNaN(value)) {
|
||||
this.sacrificeState = 0;
|
||||
return output;
|
||||
}
|
||||
const rewardsForResult = await memeburnerStore.getRewardFor(
|
||||
BigInt(value * Math.pow(10, token.decimals))
|
||||
);
|
||||
const rewardsForResultNumber = rewardsForResult.reduce((x, y) => {
|
||||
return x + Number(y) / Math.pow(10, token.decimals);
|
||||
}, 0);
|
||||
var quoteRewards = await uniswapStore.quoteExactInputSingle(value * Math.pow(10, token.decimals));
|
||||
|
||||
const rewardsForResult = await memeburnerStore.getRewardFor(amountDecimal);
|
||||
const rewardsForResultSum = rewardsForResult[0] + rewardsForResult[1];
|
||||
// quote blood amount against ETH base amount
|
||||
var quoteRewards = await uniswapStore.quoteExactInputSingle(amountDecimal, amountETHBase[0]);
|
||||
|
||||
let variant = "sacrifice";
|
||||
if (quoteRewards.result) {
|
||||
const quoteRewardsNumber = Number(quoteRewards.amount) / Math.pow(10, token.decimals);
|
||||
if (quoteRewardsNumber * 0.995 > rewardsForResultNumber) {
|
||||
// take 99.5% of quoted amount, 0.5% allowed slippage
|
||||
minBLOOD = quoteRewards.amount * 995n / 1000n;
|
||||
|
||||
if (quoteRewards.amount * 95n / 100n > rewardsForResultSum) {
|
||||
variant = "buy";
|
||||
console.log("use Buy-function", {
|
||||
quoteRewardsNumber: quoteRewardsNumber,
|
||||
rewardsForResultNumber,
|
||||
quoteRewardsNumber: quoteRewards.amount,
|
||||
rewardsForResultSum,
|
||||
});
|
||||
} else {
|
||||
console.log("use sacrifice-function", {
|
||||
quoteRewardsNumber: quoteRewardsNumber,
|
||||
rewardsForResultNumber,
|
||||
quoteRewardsNumber: quoteRewards.amount,
|
||||
rewardsForResultSum,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -125,24 +132,27 @@ export const useSacrificeStore = defineStore("sacrifice", {
|
||||
* quoter threw an error
|
||||
*/
|
||||
//
|
||||
output = rewardsForResultNumber;
|
||||
output = rewardsForResultSum;
|
||||
}
|
||||
console.log("variant", variant);
|
||||
const amountDecimal = BigInt(value * Math.pow(10, token.decimals));
|
||||
console.log("amountDecimal", amountDecimal);
|
||||
// TODO: when token.allowance become BigInt with decimals, value must be amountDecimal
|
||||
if (!walletStore.connected || (walletStore.connected && token.allowance < value)) {
|
||||
/**
|
||||
* case 1
|
||||
* user is not logged in or Allowance is less than Input, then I show getRewardsForSacrifice Value
|
||||
* variante not relevant
|
||||
*/
|
||||
output = rewardsForResultNumber;
|
||||
output = rewardsForResultSum;
|
||||
if (variant === "buy") {
|
||||
output = quoteRewards.amount
|
||||
}
|
||||
} else if (walletStore.connected && variant === "sacrifice") {
|
||||
/**
|
||||
* case 2
|
||||
* user is logged in and allowance is greater or equal input and variant is sacrifice
|
||||
*/
|
||||
output = rewardsForResultNumber;
|
||||
output = rewardsForResultSum;
|
||||
} else if (walletStore.connected && variant === "buy") {
|
||||
/**
|
||||
* case 3
|
||||
@@ -153,7 +163,7 @@ export const useSacrificeStore = defineStore("sacrifice", {
|
||||
address: memeburnerStore.contract.address,
|
||||
abi: memeburnerStore.contract.abi,
|
||||
functionName: "sacrificeMeme",
|
||||
args: [token.contractAddress, amountDecimal.toString(), 0],
|
||||
args: [token.contractAddress, amountDecimal.toString(), minETH.toString()],
|
||||
account: walletStore.address,
|
||||
});
|
||||
|
||||
@@ -161,32 +171,30 @@ export const useSacrificeStore = defineStore("sacrifice", {
|
||||
address: memeburnerStore.contract.address,
|
||||
abi: memeburnerStore.contract.abi,
|
||||
functionName: "sacrificeMemeAndBuyBLOOD",
|
||||
args: [token.contractAddress, amountDecimal.toString(), 0],
|
||||
args: [token.contractAddress, amountDecimal.toString(), minBLOOD.toString()],
|
||||
account: walletStore.address,
|
||||
});
|
||||
const amountBuyNumber = Number(amountBuy) / Math.pow(10, token.decimals);
|
||||
const amountSacrificeNumber = Number(amountSacrifice) / Math.pow(10, token.decimals);
|
||||
|
||||
if (amountBuyNumber * 0.995 > amountSacrificeNumber) {
|
||||
if (amountBuy * 95n / 100n > amountSacrifice) {
|
||||
variant = "buy";
|
||||
output = amountBuyNumber;
|
||||
output = amountBuy;
|
||||
console.log("use buy after comparison sacrificeMemeAndBuyBLOOD and sacrificeMeme");
|
||||
} else {
|
||||
variant = "sacrifice";
|
||||
output = amountSacrificeNumber;
|
||||
output = amountSacrifice;
|
||||
console.log("use sacrifice after comparison sacrificeMemeAndBuyBLOOD and sacrificeMeme");
|
||||
}
|
||||
}
|
||||
//potential staking rewards
|
||||
let stakingRewardsNumber = 0;
|
||||
if (output > 0) {
|
||||
if (output > 0n && walletStore.connected) {
|
||||
const stakingReward = await rewardLockerStore.getStakeRewardForSacrificeReward(
|
||||
token.contractAddress,
|
||||
output * Math.pow(10, token.decimals)
|
||||
output
|
||||
);
|
||||
stakingRewardsNumber = (Number(stakingReward) / Math.pow(10, token.decimals)).toFixed(2);
|
||||
stakingRewardsNumber = (Number(stakingReward / 10n ** BigInt(token.decimals))).toFixed(2);
|
||||
}
|
||||
return { output: output, variant: variant, staking: stakingRewardsNumber };
|
||||
return { output: output, variant: variant, minETH: minETH, minBLOOD: minBLOOD, staking: stakingRewardsNumber };
|
||||
} catch (error) {
|
||||
console.error("getRewardsFor", error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user