Skip to content

Commit

Permalink
Merge pull request #2187 from zeitgeistpm/tr-cpmm-clean-up
Browse files Browse the repository at this point in the history
Full amm2 support + price setting
  • Loading branch information
yornaath authored Jan 24, 2024
2 parents f4ee5da + 6d2fe17 commit a7dfc9a
Show file tree
Hide file tree
Showing 24 changed files with 278 additions and 827 deletions.
63 changes: 16 additions & 47 deletions components/create/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
reportingPeriodOptions,
} from "lib/state/market-creation/constants/deadline-options";
import { useMarketDraftEditor } from "lib/state/market-creation/editor";
import * as MarketDraft from "lib/state/market-creation/types/draft";
import { persistentAtom } from "lib/state/util/persistent-atom";
import dynamic from "next/dynamic";
import { useMemo, useRef } from "react";
import { AiOutlineInfoCircle } from "react-icons/ai";
Expand All @@ -36,30 +34,18 @@ import { getMetadataForCurrency } from "lib/constants/supported-currencies";
import Input from "components/ui/Input";
import TimezoneSelect from "./inputs/TimezoneSelect";
import { Loader } from "components/ui/Loader";
import { LiquidityInputAmm2 } from "./inputs/LiquidityAMM2";
import FeeSelect from "./inputs/FeeSelect";
import { useWallet } from "lib/state/wallet";
import {
isAMM2Form,
marketFormDataToExtrinsicParams,
} from "lib/state/market-creation/types/form";
import { marketFormDataToExtrinsicParams } from "lib/state/market-creation/types/form";
import { KeyringPairOrExtSigner } from "@zeitgeistpm/rpc";
import { CreateMarketParams, RpcContext } from "@zeitgeistpm/sdk";

const QuillEditor = dynamic(() => import("components/ui/QuillEditor"), {
ssr: false,
});

const createMarketStateAtom = persistentAtom<MarketDraft.MarketDraftState>({
key: "market-creation-form",
defaultValue: MarketDraft.empty(),
migrations: [() => MarketDraft.empty(), () => MarketDraft.empty()],
});

export const MarketEditor = () => {
const wallet = useWallet();
const [state, setState] = useAtom(createMarketStateAtom);
const editor = useMarketDraftEditor({ draft: state, update: setState });
const editor = useMarketDraftEditor();

const headerRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -111,28 +97,21 @@ export const MarketEditor = () => {
fieldsState.liquidity.isTouched && form.liquidity?.deploy && isWizard;

const isLoaded = Boolean(chainTime && isFetched);
const isAMM2Market = isAMM2Form(form);

const creationParams = useMemo<
CreateMarketParams<RpcContext> | undefined
>(() => {
let signer = wallet.getSigner();

if (!editor.isValid || !chainTime || !signer) return;

const proxy = wallet.getProxyFor(wallet.activeAccount?.address);

if (proxy && proxy.enabled) {
return marketFormDataToExtrinsicParams(
editor.form,
{ address: wallet.realAddress } as KeyringPairOrExtSigner,
chainTime,
signer,
);
}

return marketFormDataToExtrinsicParams(editor.form, signer, chainTime);
}, [editor.form, chainTime, wallet.activeAccount]);
const signer = wallet.getSigner();
const proxy = wallet.getProxyFor(wallet.activeAccount?.address);

const creationParams =
editor.isValid && chainTime && signer
? proxy && proxy.enabled
? marketFormDataToExtrinsicParams(
editor.form,
{ address: wallet.realAddress } as KeyringPairOrExtSigner,
chainTime,
signer,
)
: marketFormDataToExtrinsicParams(editor.form, signer, chainTime)
: undefined;

return (
<>
Expand Down Expand Up @@ -633,16 +612,6 @@ export const MarketEditor = () => {
Answers must be filled out correctly before adding
liquidity.
</div>
) : isAMM2Market ? (
<LiquidityInputAmm2
{...input("liquidity", { mode: "all" })}
currency={form.currency}
errorMessage={
!fieldsState.answers.isValid
? "Answers must be filled out correctly before adding liquidity."
: ""
}
/>
) : (
<LiquidityInput
{...input("liquidity", { mode: "all" })}
Expand Down
23 changes: 9 additions & 14 deletions components/create/editor/Publishing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
baseCurrency?.assetId,
);

const baseAssetLiquidityRow = editor.form?.liquidity?.rows?.find(
(row) => row.asset === editor.form.currency,
);

const bondCost =
editor.form.moderation === "Permissionless"
? constants?.markets.validityBond
Expand All @@ -108,7 +104,7 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
editor.form.moderation === "Permissionless" &&
editor.form.liquidity?.deploy &&
editor.form.currency === "ZTG"
? new Decimal(baseAssetLiquidityRow?.value ?? 0).mul(2).toNumber()
? new Decimal(editor.form.liquidity.amount ?? 0).toNumber()
: 0,
)
.plus(ztgTransactionFee ?? 0);
Expand All @@ -125,7 +121,7 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {

const foreignCurrencyCost =
editor.form.liquidity?.deploy && editor.form.currency !== "ZTG"
? new Decimal(baseAssetLiquidityRow?.value ?? 0)
? new Decimal(editor.form.liquidity.amount ?? 0)
.mul(2)
.plus(baseAssetTransactionFee ?? 0)
: null;
Expand Down Expand Up @@ -305,8 +301,7 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
</div>

{editor.form.moderation === "Permissionless" &&
editor.form.liquidity?.deploy &&
baseAssetLiquidityRow && (
editor.form.liquidity?.deploy && (
<div className="mb-4 mt-4 flex">
<div className="flex-1">
<h3 className="text-base font-normal text-black">
Expand All @@ -318,10 +313,10 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
fees but subject to impermanent loss.
</h4>
<div className="">
{new Decimal(baseAssetLiquidityRow.value)
.mul(2)
.toFixed(1)}{" "}
{baseAssetLiquidityRow?.asset}
{new Decimal(
editor.form.liquidity.amount ?? 0,
).toFixed(1)}{" "}
{editor.form.currency}
</div>
</div>
</div>
Expand Down Expand Up @@ -367,7 +362,7 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
className={`text-${baseCurrencyMetadata?.twColor}`}
>
{foreignCurrencyCost.toNumber()}{" "}
{baseAssetLiquidityRow?.asset}
{editor.form.currency}
</div>
</>
)}
Expand Down Expand Up @@ -399,7 +394,7 @@ export const Publishing = ({ editor, creationParams }: PublishingProps) => {
className={`text-${baseCurrencyMetadata?.twColor}`}
>
{foreignAssetBalanceDelta.toNumber()}{" "}
{baseAssetLiquidityRow?.asset}
{editor.form.currency}
</div>
</>
)}
Expand Down
60 changes: 12 additions & 48 deletions components/create/editor/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ export const MarketSummary = ({

const { data: baseAssetPrice } = useAssetUsdPrice(currencyMetadata?.assetId);

const baseAssetLiquidityRow = form?.liquidity?.rows?.find(
(row) => row.asset === form.currency,
);
const amm2Liquidity = editor.form?.liquidity?.amount;
const baseAmount = form.liquidity?.amount;

return (
<div className="flex-1 text-center">
Expand Down Expand Up @@ -111,43 +108,23 @@ export const MarketSummary = ({
</div>
</div>
<div>
{baseAssetLiquidityRow &&
form?.liquidity?.deploy &&
{form?.liquidity?.deploy &&
form?.moderation === "Permissionless" ? (
<>
<div className="mb-4 flex justify-center gap-4">
<div className="flex items-center justify-center gap-2">
<Label>Amount</Label>{" "}
<div>
{amm2Liquidity ?? baseAssetLiquidityRow?.amount ?? "--"}
</div>
<Label>Amount</Label> <div>{baseAmount ?? "--"}</div>
</div>
{!amm2Liquidity && (
<div className="flex items-center justify-center gap-2">
<Label>Weight</Label>{" "}
<div>{baseAssetLiquidityRow?.weight ?? "--"}</div>
</div>
)}
<div className="flex items-center justify-center gap-2">
<Label>Swap Fee</Label>{" "}
{form.liquidity?.swapFee?.value ?? "--"}%
</div>
</div>

<div>
<Label>Total Base Liquidity</Label>{" "}
{amm2Liquidity
? amm2Liquidity
: new Decimal(baseAssetLiquidityRow?.value)
.mul(2)
.toFixed(1)}{" "}
{baseAssetLiquidityRow?.asset}{" "}
<span className="text-gray-400"></span>{" "}
{baseAssetPrice
?.mul(Number(amm2Liquidity) ?? baseAssetLiquidityRow?.value)
.mul(2)
.toFixed(2)}{" "}
USD
<Label>Total Base Liquidity</Label> {baseAmount}{" "}
{form.currency} <span className="text-gray-400"></span>{" "}
{baseAssetPrice?.mul(baseAmount || 0).toFixed(2)} USD
</div>
</>
) : !form?.liquidity?.deploy &&
Expand Down Expand Up @@ -328,26 +305,10 @@ const AnswersDisplay = ({
</div>
<div className="table-cell text-left">
<div>
{liquidity?.amount ?? answerLiquidity?.amount ?? "--"}
{Number(answerLiquidity?.amount).toFixed(1) ?? "--"}
</div>
</div>
</div>

{!liquidity.amount && (
<div className="mb-1 table-row">
<div className="table-cell pr-4 text-left">
<Label className="text-xs">Weight</Label>{" "}
</div>
<div className="table-cell text-left">
<div>
{answerLiquidity?.weight
? new Decimal(answerLiquidity.weight).toFixed(2)
: "--"}
</div>
</div>
</div>
)}

<div className="mb-1 table-row">
<div className="table-cell pr-4 text-left">
<Label className="text-xs">Value</Label>{" "}
Expand All @@ -356,10 +317,13 @@ const AnswersDisplay = ({
<div className="mb-1">
{answerLiquidity ? (
<>
{new Decimal(answerLiquidity?.value).toFixed(1)}{" "}
{new Decimal(answerLiquidity?.amount || 0)
.mul(answerLiquidity?.price.price ?? 0)
.toFixed(1)}{" "}
<span className="text-gray-400"></span>{" "}
{baseAssetPrice
?.mul(answerLiquidity?.value)
?.mul(answerLiquidity?.amount || 0)
.mul(answerLiquidity?.price.price ?? 0)
.toFixed(2)}{" "}
USD
</>
Expand Down
9 changes: 6 additions & 3 deletions components/create/editor/inputs/Liquidity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { FieldState } from "lib/state/market-creation/types/fieldstate";
import { CurrencyTag, Liquidity } from "lib/state/market-creation/types/form";
import { ReactNode } from "react";
import { FormEvent } from "../types";
import Input from "components/ui/Input";
import PoolSettingsAmm2 from "components/liquidity/PoolSettingsAMM2";
import FeeSelect, { Fee } from "./FeeSelect";
import { useMarketDraftEditor } from "lib/state/market-creation/editor";

export type LiquidityInputProps = {
name: string;
Expand All @@ -30,17 +29,19 @@ export const LiquidityInput = ({
currency,
fieldState,
}: LiquidityInputProps) => {
const editor = useMarketDraftEditor();
const currencyMetadata = getMetadataForCurrency(currency);
const { data: baseAssetPrice } = useAssetUsdPrice(currencyMetadata?.assetId);

const handleRowsChange = (data: PoolAssetRowData[]) => {
const handleRowsChange = (data: PoolAssetRowData[], amount: string) => {
onChange({
type: "change",
target: {
name,
value: {
...value!,
rows: transformRows(data),
amount: amount,
},
},
});
Expand All @@ -66,6 +67,8 @@ export const LiquidityInput = ({
<div className="mb-4 ">
<PoolSettings
baseAssetPrice={baseAssetPrice ?? undefined}
baseAssetSymbol={currencyMetadata?.name ?? ""}
baseAssetAmount={value?.amount ?? ""}
data={transformRows(value?.rows ?? [])}
onChange={handleRowsChange}
noDataMessage={errorMessage}
Expand Down
Loading

0 comments on commit a7dfc9a

Please sign in to comment.