use original coeff for tokenValue correction
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
This commit is contained in:
parent
83761d54ca
commit
98a6a5b5d2
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "ghost-dao-interface",
|
"name": "ghost-dao-interface",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.5.32",
|
"version": "0.5.33",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import { TokenAllowanceGuard } from "../../components/TokenAllowanceGuard/TokenA
|
|||||||
import { SecondaryButton } from "../../components/Button";
|
import { SecondaryButton } from "../../components/Button";
|
||||||
|
|
||||||
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
||||||
import { formatNumber, formatCurrency } from "../../helpers";
|
import { formatNumber, formatCurrency, bigIntSqrt } from "../../helpers";
|
||||||
|
|
||||||
import { useBalance, useTotalSupply } from "../../hooks/tokens";
|
import { useBalance, useTotalSupply } from "../../hooks/tokens";
|
||||||
import { useUniswapV2Pair, useUniswapV2PairReserves, addLiquidity } from "../../hooks/uniswapv2";
|
import { useUniswapV2Pair, useUniswapV2PairReserves, addLiquidity } from "../../hooks/uniswapv2";
|
||||||
@ -75,34 +75,6 @@ const PoolContainer = ({
|
|||||||
const setMaxTop = () => setAmountTop(balanceTop.toString());
|
const setMaxTop = () => setAmountTop(balanceTop.toString());
|
||||||
const setMaxBottom = () => setAmountBottom(balanceBottom.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 estimatedAmountOut = useMemo(() => {
|
||||||
const pairReserves0 = addressTop.toUpperCase() === tokenAddresses.token0.toUpperCase()
|
const pairReserves0 = addressTop.toUpperCase() === tokenAddresses.token0.toUpperCase()
|
||||||
? pairReserves.reserve0
|
? pairReserves.reserve0
|
||||||
|
|||||||
@ -198,7 +198,7 @@ const ProposalDetails = ({ chainId, address, connect, config }) => {
|
|||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<PageTitle
|
<PageTitle
|
||||||
name={`GBP-${id.slice(-5)}`}
|
name={`GDP-${id.slice(-5)}`}
|
||||||
subtitle={
|
subtitle={
|
||||||
<Typography component="span">
|
<Typography component="span">
|
||||||
{`Cast $${ghstSymbol} to shape this proposal's outcome`}
|
{`Cast $${ghstSymbol} to shape this proposal's outcome`}
|
||||||
|
|||||||
@ -52,3 +52,16 @@ export const timeConverter = (time, max = 7200, maxText = "long ago") => {
|
|||||||
return `${mins}m ${secs < 10 ? '0' : ''}${secs}s`;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -13,14 +13,16 @@ import { abi as BondAbi } from "../../abi/GhostBondDepository.json";
|
|||||||
import { abi as TreasuryAbi } from "../../abi/GhostTreasury.json";
|
import { abi as TreasuryAbi } from "../../abi/GhostTreasury.json";
|
||||||
import { abi as BondingCalculatorAbi } from "../../abi/GhostBondingCalculator.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 { useTokenSymbol, useTokenSymbols } from "../tokens";
|
||||||
import { getTokenAddress, getTokenIcons, getBondNameDisplayName, getTokenPurchaseLink } from "../helpers";
|
import { getTokenAddress, getTokenIcons, getBondNameDisplayName, getTokenPurchaseLink } from "../helpers";
|
||||||
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
||||||
import { shorten } from "../../helpers";
|
import { shorten } from "../../helpers";
|
||||||
|
|
||||||
export const useLiveBonds = (chainId) => {
|
export const useLiveBonds = (chainId) => {
|
||||||
const baseTokenPerUsd = useFtsoPrice(chainId);
|
const baseTokenPerUsd = useReservePrice(chainId);
|
||||||
|
const originalCoefficient = useOrinalCoefficient(chainId);
|
||||||
|
|
||||||
const { data: liveIndexesRaw, refetch } = useReadContract({
|
const { data: liveIndexesRaw, refetch } = useReadContract({
|
||||||
abi: BondAbi,
|
abi: BondAbi,
|
||||||
@ -126,7 +128,11 @@ export const useLiveBonds = (chainId) => {
|
|||||||
const quoteTokenSymbol = quoteTokenSymbols?.at(index).result ? quoteTokenSymbols.at(index).result : "";
|
const quoteTokenSymbol = quoteTokenSymbols?.at(index).result ? quoteTokenSymbols.at(index).result : "";
|
||||||
|
|
||||||
const quoteTokenPerBaseToken = new DecimalBigNumber(marketPrice, 9);
|
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
|
const discount = baseTokenPerUsd._value > 0n
|
||||||
? baseTokenPerUsd.sub(priceInUsd).div(baseTokenPerUsd)
|
? baseTokenPerUsd.sub(priceInUsd).div(baseTokenPerUsd)
|
||||||
: new DecimalBigNumber("0", baseTokenPerUsd._decimals);
|
: new DecimalBigNumber("0", baseTokenPerUsd._decimals);
|
||||||
|
|||||||
@ -116,7 +116,7 @@ export const useReservePrice = (chainId) => {
|
|||||||
.then(response => {
|
.then(response => {
|
||||||
if ('data' in response) {
|
if ('data' in response) {
|
||||||
const coinPrice = Number(response?.data?.amount ?? 0.0);
|
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));
|
setReservePrice(new DecimalBigNumber(BigInt(priceInWei), 18));
|
||||||
} else if ('price' in response) {
|
} else if ('price' in response) {
|
||||||
const coinPrice = Number(response?.price ?? 0.0);
|
const coinPrice = Number(response?.price ?? 0.0);
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { abi as TreasuryAbi } from "../../abi/GhostTreasury.json";
|
|||||||
|
|
||||||
import { useReservePrice } from "../prices/index";
|
import { useReservePrice } from "../prices/index";
|
||||||
|
|
||||||
|
import { bigIntSqrt } from "../../helpers";
|
||||||
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
import { DecimalBigNumber } from "../../helpers/DecimalBigNumber";
|
||||||
import { getTokenAddress } from "../helpers";
|
import { getTokenAddress } from "../helpers";
|
||||||
|
|
||||||
@ -40,6 +41,9 @@ export const useTotalReserves = (chainId) => {
|
|||||||
|
|
||||||
export const useLpValuation = (chainId, pairAddress, pairSupply) => {
|
export const useLpValuation = (chainId, pairAddress, pairSupply) => {
|
||||||
const convertedPairAddress = getTokenAddress(chainId, pairAddress);
|
const convertedPairAddress = getTokenAddress(chainId, pairAddress);
|
||||||
|
const originalCoefficient = useOrinalCoefficient(chainId);
|
||||||
|
const reservePrice = useReservePrice(chainId);
|
||||||
|
|
||||||
const { data: valuationRaw } = useReadContract({
|
const { data: valuationRaw } = useReadContract({
|
||||||
abi: TreasuryAbi,
|
abi: TreasuryAbi,
|
||||||
address: DAO_TREASURY_ADDRESSES[chainId],
|
address: DAO_TREASURY_ADDRESSES[chainId],
|
||||||
@ -52,7 +56,18 @@ export const useLpValuation = (chainId, pairAddress, pairSupply) => {
|
|||||||
chainId,
|
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);
|
const valuation = new DecimalBigNumber(valuationPrepared, 9);
|
||||||
|
|
||||||
return valuation;
|
return valuation;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user