Skip to content

Commit

Permalink
Merge pull request #2000 from zeitgeistpm/tr-more-amm2-fixes
Browse files Browse the repository at this point in the history
Amm2 misc
  • Loading branch information
Robiquet authored Nov 27, 2023
2 parents f037efb + b3504c9 commit a7632e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion components/markets/TradeResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const TradeResult = ({
<div className="text-[58px]">{amount?.toFixed(2)}</div>
<div className="text-center">
<span className="font-bold capitalize">{tokenName}</span> Predictions
For
for
<div className="font-bold">
{baseTokenAmount?.toFixed(2)} {baseToken}
</div>
Expand Down
38 changes: 27 additions & 11 deletions components/trade-form/BuyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
);
},
Expand All @@ -173,34 +174,45 @@ 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(),
);
}
trigger("amount");
});
return () => subscription.unsubscribe();
}, [watch, baseAssetBalance, maxAmountIn]);
}, [watch, maxSpendableBalance, maxAmountIn]);

const onSubmit = () => {
send();
Expand All @@ -212,7 +224,9 @@ const BuyForm = ({
className="flex w-full flex-col items-center gap-y-4"
>
<div className="flex w-full items-center justify-center rounded-md p-2">
<div className="mr-4 font-mono">{amountOut.div(ZTG).toFixed(3)}</div>
<div className="mr-4 font-mono">
{amountOut.div(ZTG).abs().toFixed(3)}
</div>
<div>
{market && selectedAsset && (
<MarketContextActionOutcomeSelector
Expand Down Expand Up @@ -240,8 +254,8 @@ const BuyForm = ({
message: "Value is required",
},
validate: (value) => {
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) {
Expand All @@ -261,7 +275,9 @@ const BuyForm = ({
<input
className="mb-[10px] mt-[30px] w-full"
type="range"
disabled={!baseAssetBalance || baseAssetBalance.lessThanOrEqualTo(0)}
disabled={
!maxSpendableBalance || maxSpendableBalance.lessThanOrEqualTo(0)
}
{...register("percentage", { value: "0" })}
/>
<div className="mb-[10px] flex w-full flex-col items-center gap-2 text-xs font-normal text-sky-600 ">
Expand Down
17 changes: 12 additions & 5 deletions components/trade-form/SellForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -241,7 +248,7 @@ const SellForm = ({
</div>
<div className="text-sm">For</div>
<div className="flex w-full items-center justify-center font-mono">
<div className="mr-4">{amountOut.div(ZTG).toFixed(5)}</div>
<div className="mr-4">{amountOut.div(ZTG).abs().toFixed(3)}</div>
<div className="mr-[10px]">{constants?.tokenSymbol}</div>
</div>
<input
Expand Down
2 changes: 1 addition & 1 deletion pages/markets/[marketid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ const Market: NextPage<MarketPageProps> = ({
) : (
<></>
)}
{marketHasPool === false && (
{(marketIsLoading === false && marketHasPool === false) && (
<div className="flex h-ztg-22 items-center bg-vermilion-light text-vermilion p-ztg-20 rounded-ztg-5">
<div className="w-ztg-20 h-ztg-20">
<AlertTriangle size={20} />
Expand Down

0 comments on commit a7632e2

Please sign in to comment.