diff --git a/package.json b/package.json index 90b7d09..df7de01 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ghost-dao-interface", "private": true, - "version": "0.5.32", + "version": "0.5.33", "type": "module", "scripts": { "dev": "vite", diff --git a/src/containers/Dex/PoolContainer.jsx b/src/containers/Dex/PoolContainer.jsx index 559140d..fcd0cfc 100644 --- a/src/containers/Dex/PoolContainer.jsx +++ b/src/containers/Dex/PoolContainer.jsx @@ -9,7 +9,7 @@ import { TokenAllowanceGuard } from "../../components/TokenAllowanceGuard/TokenA import { SecondaryButton } from "../../components/Button"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; -import { formatNumber, formatCurrency } from "../../helpers"; +import { formatNumber, formatCurrency, bigIntSqrt } from "../../helpers"; import { useBalance, useTotalSupply } from "../../hooks/tokens"; import { useUniswapV2Pair, useUniswapV2PairReserves, addLiquidity } from "../../hooks/uniswapv2"; @@ -75,34 +75,6 @@ const PoolContainer = ({ const setMaxTop = () => setAmountTop(balanceTop.toString()); const setMaxBottom = () => setAmountBottom(balanceBottom.toString()); - const bigIntSqrt = (n) => { - if (n < 0n) { - throw new Error("Cannot compute the square root of a negative number."); - } - if (n < 2n) { - return n; // The square root of 0 or 1 is the number itself - } - - let low = 0n; - let high = n; - let mid; - - while (low <= high) { - mid = (low + high) / 2n; - const midSquared = mid * mid; - - if (midSquared === n) { - return mid; // Found the exact square root - } else if (midSquared < n) { - low = mid + 1n; // Move to the right half - } else { - high = mid - 1n; // Move to the left half - } - } - - return high; // The integer part of the square root - } - const estimatedAmountOut = useMemo(() => { const pairReserves0 = addressTop.toUpperCase() === tokenAddresses.token0.toUpperCase() ? pairReserves.reserve0 diff --git a/src/containers/Governance/ProposalDetails.jsx b/src/containers/Governance/ProposalDetails.jsx index 578c924..0b80804 100644 --- a/src/containers/Governance/ProposalDetails.jsx +++ b/src/containers/Governance/ProposalDetails.jsx @@ -198,7 +198,7 @@ const ProposalDetails = ({ chainId, address, connect, config }) => { return ( {`Cast $${ghstSymbol} to shape this proposal's outcome`} diff --git a/src/helpers/index.js b/src/helpers/index.js index d5531e7..373b812 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -52,3 +52,16 @@ export const timeConverter = (time, max = 7200, maxText = "long ago") => { return `${mins}m ${secs < 10 ? '0' : ''}${secs}s`; } } + +export const bigIntSqrt = (n) => { + if (n < 0n) return 0n; + if (n < 2n) return n; + + let x = n / 2n + 1n; + let y = (x + n / x) / 2n; + while (y < x) { + x = y; + y = (x + n / x) / 2n; + } + return x; +} diff --git a/src/hooks/bonds/index.js b/src/hooks/bonds/index.js index 825831f..f74732b 100644 --- a/src/hooks/bonds/index.js +++ b/src/hooks/bonds/index.js @@ -13,14 +13,16 @@ import { abi as BondAbi } from "../../abi/GhostBondDepository.json"; import { abi as TreasuryAbi } from "../../abi/GhostTreasury.json"; import { abi as BondingCalculatorAbi } from "../../abi/GhostBondingCalculator.json"; -import { useFtsoPrice } from "../prices"; +import { useReservePrice } from "../prices"; +import { useOrinalCoefficient } from "../treasury"; import { useTokenSymbol, useTokenSymbols } from "../tokens"; import { getTokenAddress, getTokenIcons, getBondNameDisplayName, getTokenPurchaseLink } from "../helpers"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; import { shorten } from "../../helpers"; export const useLiveBonds = (chainId) => { - const baseTokenPerUsd = useFtsoPrice(chainId); + const baseTokenPerUsd = useReservePrice(chainId); + const originalCoefficient = useOrinalCoefficient(chainId); const { data: liveIndexesRaw, refetch } = useReadContract({ abi: BondAbi, @@ -126,7 +128,11 @@ export const useLiveBonds = (chainId) => { const quoteTokenSymbol = quoteTokenSymbols?.at(index).result ? quoteTokenSymbols.at(index).result : ""; const quoteTokenPerBaseToken = new DecimalBigNumber(marketPrice, 9); - const priceInUsd = quoteTokenPerUsd.mul(quoteTokenPerBaseToken).mul(markdown); + let priceInUsd = baseTokenPerUsd.mul(quoteTokenPerBaseToken); + if (Number(markdown?.toString() ?? "1") !== 1) { + priceInUsd = priceInUsd.div(originalCoefficient) + } + const discount = baseTokenPerUsd._value > 0n ? baseTokenPerUsd.sub(priceInUsd).div(baseTokenPerUsd) : new DecimalBigNumber("0", baseTokenPerUsd._decimals); diff --git a/src/hooks/prices/index.js b/src/hooks/prices/index.js index abf3e14..4579970 100644 --- a/src/hooks/prices/index.js +++ b/src/hooks/prices/index.js @@ -116,7 +116,7 @@ export const useReservePrice = (chainId) => { .then(response => { if ('data' in response) { const coinPrice = Number(response?.data?.amount ?? 0.0); - const priceInWei = Math.floor(coinPrice * 1e18 / 0.99); + const priceInWei = Math.floor(coinPrice * 1e18); setReservePrice(new DecimalBigNumber(BigInt(priceInWei), 18)); } else if ('price' in response) { const coinPrice = Number(response?.price ?? 0.0); diff --git a/src/hooks/treasury/index.js b/src/hooks/treasury/index.js index 7708116..2abe014 100644 --- a/src/hooks/treasury/index.js +++ b/src/hooks/treasury/index.js @@ -5,6 +5,7 @@ import { abi as TreasuryAbi } from "../../abi/GhostTreasury.json"; import { useReservePrice } from "../prices/index"; +import { bigIntSqrt } from "../../helpers"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; import { getTokenAddress } from "../helpers"; @@ -40,6 +41,9 @@ export const useTotalReserves = (chainId) => { export const useLpValuation = (chainId, pairAddress, pairSupply) => { const convertedPairAddress = getTokenAddress(chainId, pairAddress); + const originalCoefficient = useOrinalCoefficient(chainId); + const reservePrice = useReservePrice(chainId); + const { data: valuationRaw } = useReadContract({ abi: TreasuryAbi, address: DAO_TREASURY_ADDRESSES[chainId], @@ -52,7 +56,18 @@ export const useLpValuation = (chainId, pairAddress, pairSupply) => { chainId, }); - const valuationPrepared = valuationRaw ? valuationRaw : 0n; + const sqrtReservePrice = reservePrice?._value + ? bigIntSqrt(reservePrice._value) + : 0n; + + const sqrtOriginalCoefficient = originalCoefficient?._value + ? bigIntSqrt(originalCoefficient._value) + : 1n; + + const valuationPrepared = valuationRaw + ? valuationRaw * sqrtReservePrice / sqrtOriginalCoefficient + : 0n; + const valuation = new DecimalBigNumber(valuationPrepared, 9); return valuation;