diff --git a/components/markets/TradeResult.tsx b/components/markets/TradeResult.tsx index 5d94b2590..aeb52369b 100644 --- a/components/markets/TradeResult.tsx +++ b/components/markets/TradeResult.tsx @@ -67,7 +67,7 @@ const TradeResult = ({
{amount?.toFixed(2)}
{tokenName} Predictions - For + for
{baseTokenAmount?.toFixed(2)} {baseToken}
diff --git a/components/trade-form/BuyForm.tsx b/components/trade-form/BuyForm.tsx index 82d1aa562..83b3a76b7 100644 --- a/components/trade-form/BuyForm.tsx +++ b/components/trade-form/BuyForm.tsx @@ -31,6 +31,7 @@ import { parseAssetIdString } from "lib/util/parse-asset-id"; import { useState, useEffect, useMemo } from "react"; import { useForm } from "react-hook-form"; import { ISubmittableResult } from "@polkadot/types/types"; +import { assetsAreEqual } from "lib/util/assets-are-equal"; const slippageMultiplier = (100 - DEFAULT_SLIPPAGE_PERCENTAGE) / 100; @@ -159,7 +160,7 @@ const BuyForm = ({ marketId, market?.categories?.length, selectedAsset, - new Decimal(amount).mul(ZTG).toFixed(0), + new Decimal(amount).abs().mul(ZTG).toFixed(0), minAmountOut.toFixed(0), ); }, @@ -173,26 +174,37 @@ const BuyForm = ({ }, ); + const maxSpendableBalance = assetsAreEqual(baseAsset, fee?.assetId) + ? baseAssetBalance?.minus(fee?.amount ?? 0) + : baseAssetBalance; + useEffect(() => { const subscription = watch((value, { name, type }) => { const changedByUser = type != null; - if (!changedByUser || !baseAssetBalance || !maxAmountIn) return; + if (!changedByUser || !maxSpendableBalance || !maxAmountIn) return; if (name === "percentage") { - const max = baseAssetBalance.greaterThan(maxAmountIn) + const max = maxSpendableBalance.greaterThan(maxAmountIn) ? maxAmountIn - : baseAssetBalance; + : maxSpendableBalance; setValue( "amount", - max.mul(value.percentage).div(100).div(ZTG).toNumber(), + Number( + max + .mul(value.percentage) + .abs() + .div(100) + .div(ZTG) + .toFixed(3, Decimal.ROUND_DOWN), + ), ); } else if (name === "amount" && value.amount !== "") { setValue( "percentage", new Decimal(value.amount) .mul(ZTG) - .div(baseAssetBalance) + .div(maxSpendableBalance) .mul(100) .toString(), ); @@ -200,7 +212,7 @@ const BuyForm = ({ trigger("amount"); }); return () => subscription.unsubscribe(); - }, [watch, baseAssetBalance, maxAmountIn]); + }, [watch, maxSpendableBalance, maxAmountIn]); const onSubmit = () => { send(); @@ -212,7 +224,9 @@ const BuyForm = ({ className="flex w-full flex-col items-center gap-y-4" >
-
{amountOut.div(ZTG).toFixed(3)}
+
+ {amountOut.div(ZTG).abs().toFixed(3)} +
{market && selectedAsset && ( { - if (value > (baseAssetBalance?.div(ZTG).toNumber() ?? 0)) { - return `Insufficient balance. Current balance: ${baseAssetBalance + if (value > (maxSpendableBalance?.div(ZTG).toNumber() ?? 0)) { + return `Insufficient balance. Current balance: ${maxSpendableBalance ?.div(ZTG) .toFixed(3)}`; } else if (value <= 0) { @@ -261,7 +275,9 @@ const BuyForm = ({
diff --git a/components/trade-form/SellForm.tsx b/components/trade-form/SellForm.tsx index 3242cad50..a0c860ed9 100644 --- a/components/trade-form/SellForm.tsx +++ b/components/trade-form/SellForm.tsx @@ -81,9 +81,9 @@ const SellForm = ({ ); const formAmount = getValues("amount"); - const amountIn = new Decimal( - formAmount && formAmount !== "" ? formAmount : 0, - ).mul(ZTG); + const amountIn = new Decimal(formAmount && formAmount !== "" ? formAmount : 0) + .mul(ZTG) + .abs(); const assetReserve = pool?.reserves && lookupAssetReserve(pool?.reserves, selectedAsset); @@ -174,7 +174,14 @@ const SellForm = ({ : selectedAssetBalance; setValue( "amount", - max.mul(value.percentage).div(100).div(ZTG).toNumber(), + Number( + max + .mul(value.percentage) + .abs() + .div(100) + .div(ZTG) + .toFixed(3, Decimal.ROUND_DOWN), + ), ); } else if (name === "amount" && value.amount !== "") { setValue( @@ -241,7 +248,7 @@ const SellForm = ({
For
-
{amountOut.div(ZTG).toFixed(5)}
+
{amountOut.div(ZTG).abs().toFixed(3)}
{constants?.tokenSymbol}
= ({ ) : ( <> )} - {marketHasPool === false && ( + {(marketIsLoading === false && marketHasPool === false) && (