From bc3b7eac676ce4ddc3e9d8e69c49420667aa682c Mon Sep 17 00:00:00 2001 From: butterscotch Date: Mon, 23 Oct 2023 01:01:34 +0300 Subject: [PATCH 1/2] simplified uniswap.js --- src/assets/contracts/WETH.json | 5 +++ src/stores/sacrifice.js | 2 +- src/stores/uniswap.js | 74 +++++++--------------------------- 3 files changed, 21 insertions(+), 60 deletions(-) create mode 100644 src/assets/contracts/WETH.json diff --git a/src/assets/contracts/WETH.json b/src/assets/contracts/WETH.json new file mode 100644 index 0000000..6c41117 --- /dev/null +++ b/src/assets/contracts/WETH.json @@ -0,0 +1,5 @@ +{ + "contractName": "WETH", + "contractAddress": "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", + "contractAddressMainnet": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" +} diff --git a/src/stores/sacrifice.js b/src/stores/sacrifice.js index d3d1113..5900a40 100644 --- a/src/stores/sacrifice.js +++ b/src/stores/sacrifice.js @@ -112,7 +112,7 @@ export const useSacrificeStore = defineStore("sacrifice", { const rewardsForResult = await memeburnerStore.getRewardFor(amount); const rewardsForResultSum = rewardsForResult[0] + rewardsForResult[1]; // quote blood amount against ETH base amount - var quoteRewards = await uniswapStore.quoteExactInputSingle(amount, amountETHBase[0]); + var quoteRewards = await uniswapStore.quoteExactInputSingle(amountETHBase[0]); let variant = "sacrifice"; if (quoteRewards.result) { diff --git a/src/stores/uniswap.js b/src/stores/uniswap.js index 9f5a690..6f6169d 100644 --- a/src/stores/uniswap.js +++ b/src/stores/uniswap.js @@ -1,23 +1,17 @@ import { defineStore } from "pinia"; import { useWalletStoreV2 } from "@/stores/walletV2"; import QuoterJson from "@/assets/contracts/Quoter.json"; -import UniswapPoolJson from "@/assets/contracts/UniswapPool.json"; +import WethJson from "@/assets/contracts/WETH.json"; import BloodlineJson from "@/assets/contracts/Bloodline.json"; -import { useMemeburnerStore } from "@/stores/memeburner"; -import { useTokenStore } from "@/stores/tokens"; import { getContract, multicall } from "@wagmi/core"; export const useUniswapStore = defineStore("uniswap", { state: () => ({ quoateV3Contract: {}, - quoateV2Contract: {}, - uniswapPoolContract: {}, - poolData: { - token0: null, - token1: null, - fee: null, - }, + tokenIn: WethJson.contractAddress, + tokenOut: BloodlineJson.contractAddress, + fee: 10000n, }), getters: {}, actions: { @@ -31,38 +25,25 @@ export const useUniswapStore = defineStore("uniswap", { walletClient: walletStore.walletClient || walletStore.publicClient, }); - this.uniswapPoolContract = getContract({ - address: UniswapPoolJson.contractAddress, - abi: UniswapPoolJson.abi, - walletClient: walletStore.walletClient || walletStore.publicClient, - }); - - this.getPoolData(); }, - async quoteExactInputSingle(amount, ethBase) { + async quoteExactInputSingle(ethBase) { try { console.log("quoteExactInputSingle"); - const memeBurnerStore = useMemeburnerStore(); - const tokenStore = useTokenStore(); - - const tokenOut = tokenStore.memeToken.contractAddress; - // tokenIn - WETH - const tokenIn = BigInt(this.poolData.token0) == BigInt(tokenOut) ? this.poolData.token1 : this.poolData.token0; console.log("quoteExactInputSingle-parameter", { - tokenIn: tokenIn, // WETH - tokenOut: tokenOut, // BLOOD - fee: this.poolData.fee, + tokenIn: this.tokenIn, // WETH + tokenOut: this.tokenOut, // BLOOD + fee: this.fee, amountEthBase: ethBase, - pricelimit: 0, //sqrtPriceLimitX96 + pricelimit: 0n, //sqrtPriceLimitX96 }); const quotedAmountOut = await this.quoateV3Contract.read.quoteExactInputSingle([ - tokenIn, //weth - tokenOut, //bloodline - this.poolData.fee, + this.tokenIn, //weth + this.tokenOut, //bloodline + this.fee, ethBase, - 0, //sqrtPriceLimitX96 + 0n, //sqrtPriceLimitX96 ]); console.log("quotedAmountOut", quotedAmountOut); return {result: true, amount: quotedAmountOut}; @@ -70,31 +51,6 @@ export const useUniswapStore = defineStore("uniswap", { return { result: false, amount: null }; } - }, - async getPoolData() { - console.log("getPoolData"); - const contracts = []; - contracts.push( - { - ...this.uniswapPoolContract, - functionName: "token0", - }, - { - ...this.uniswapPoolContract, - functionName: "token1", - }, - { - ...this.uniswapPoolContract, - functionName: "fee", - } - ); - const data = await multicall({ - contracts, - }); - this.poolData.token0 = data[0].result; - this.poolData.token1 = data[1].result; - this.poolData.fee = data[2].result; - console.log("this.poolData", this.poolData); - }, - }, + } + } }); From 74dd5b7e0a3e2ec87eb2db62f8a3853e292459cd Mon Sep 17 00:00:00 2001 From: butterscotch Date: Mon, 23 Oct 2023 20:45:24 +0300 Subject: [PATCH 2/2] sacrifice.js getRewards fixes --- src/stores/sacrifice.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/stores/sacrifice.js b/src/stores/sacrifice.js index 5900a40..844b2b9 100644 --- a/src/stores/sacrifice.js +++ b/src/stores/sacrifice.js @@ -115,6 +115,8 @@ export const useSacrificeStore = defineStore("sacrifice", { var quoteRewards = await uniswapStore.quoteExactInputSingle(amountETHBase[0]); let variant = "sacrifice"; + // here you are setting only variant, let's show by default rewardsForResultSum + output = rewardsForResultSum; if (quoteRewards.result) { // take 99.5% of quoted amount, 0.5% allowed slippage minBLOOD = quoteRewards.amount * 995n / 1000n; @@ -131,13 +133,6 @@ export const useSacrificeStore = defineStore("sacrifice", { rewardsForResultSum, }); } - } else { - /** - * case 4 - * quoter threw an error - */ - // - output = rewardsForResultSum; } console.log("variant", variant); console.log("amount", amount); @@ -151,13 +146,21 @@ export const useSacrificeStore = defineStore("sacrifice", { if (variant === "buy") { output = quoteRewards.amount } - } else if (walletStore.connected && variant === "sacrifice") { + } else if (walletStore.connected && variant === "sacrifice" && token.allowance >= amount) { /** * case 2 * user is logged in and allowance is greater or equal input and variant is sacrifice */ - output = rewardsForResultSum; - } else if (walletStore.connected && variant === "buy") { + // in this case you should use eth_call, so you are showing exact reward in cases of completing cycles + const amountSacrifice = await readContract({ + address: memeburnerStore.contract.address, + abi: memeburnerStore.contract.abi, + functionName: "sacrificeMeme", + args: [token.contractAddress, amount.toString(), minETH.toString()], + account: walletStore.address, + }); + output = amountSacrifice; + } else if (walletStore.connected && variant === "buy" && token.allowance >= amount) { /** * case 3 * user is logged in and allowance is greater or equal input and variant is buy