From d9c5080c1bf929d451c1b57180b718fa168c3001 Mon Sep 17 00:00:00 2001 From: WRadoslaw Date: Wed, 27 Mar 2024 19:58:42 +0100 Subject: [PATCH] Fix sell token logic to consider amm balance --- .../AmmModalFormTemplate.tsx | 71 ++++++++++--------- .../_crt/SellTokenModal/SellTokenModal.tsx | 12 ++-- .../_inputs/TokenInput/TokenInput.tsx | 2 +- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/packages/atlas/src/components/_crt/AmmModalTemplates/AmmModalFormTemplate.tsx b/packages/atlas/src/components/_crt/AmmModalTemplates/AmmModalFormTemplate.tsx index 45ae6d349f..9932e7046e 100644 --- a/packages/atlas/src/components/_crt/AmmModalTemplates/AmmModalFormTemplate.tsx +++ b/packages/atlas/src/components/_crt/AmmModalTemplates/AmmModalFormTemplate.tsx @@ -44,41 +44,42 @@ export const AmmModalFormTemplate = ({ ...(validation ? { valid: validation } : {}), }, }} - render={({ field }) => ( - - - field.onChange(Math.round(value ?? 0))} - placeholder="0" - maxValue={maxValue} - nodeEnd={ - pricePerUnit ? ( - - ${convertTokensToUSD((field.value || 0) * pricePerUnit)?.toFixed(2)} - - ) : null - } - /> - - {showTresholdButtons && typeof maxValue === 'number' ? ( - - - - - - - ) : null} - - )} + render={({ field }) => { + return ( + + + field.onChange(value ? Math.round(value) : value)} + placeholder="0" + nodeEnd={ + pricePerUnit ? ( + + ${convertTokensToUSD((field.value || 0) * pricePerUnit)?.toFixed(2)} + + ) : null + } + /> + + {showTresholdButtons && typeof maxValue === 'number' ? ( + + + + + + + ) : null} + + ) + }} /> diff --git a/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx b/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx index 06ef3aafcc..5392991b14 100644 --- a/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx +++ b/packages/atlas/src/components/_crt/SellTokenModal/SellTokenModal.tsx @@ -45,7 +45,8 @@ export const SellTokenModal = ({ tokenId, onClose: _onClose, show }: SellTokenMo }) const client = useApolloClient() - const currentAmm = data?.creatorTokenById?.ammCurves.find((amm) => !amm.finalized) + const currentAmm = data?.creatorTokenById?.currentAmmSale + const ammBalance = currentAmm ? +currentAmm.mintedByAmm - +currentAmm.burnedByAmm : 0 const title = data?.creatorTokenById?.symbol ?? 'N/A' const { tokenBalance: userTokenBalance } = useGetTokenBalance(tokenId) @@ -59,13 +60,13 @@ export const SellTokenModal = ({ tokenId, onClose: _onClose, show }: SellTokenMo (amount: number) => { const currentAmm = data?.creatorTokenById?.ammCurves.find((amm) => !amm.finalized) return calcSellMarketPricePerToken( - currentAmm ? +currentAmm.mintedByAmm - +currentAmm.burnedByAmm : undefined, + currentAmm ? ammBalance : undefined, currentAmm?.ammSlopeParameter, currentAmm?.ammInitPrice, amount ) }, - [data?.creatorTokenById] + [ammBalance, data?.creatorTokenById?.ammCurves] ) const priceForAllToken = useMemo(() => { @@ -257,15 +258,14 @@ export const SellTokenModal = ({ tokenId, onClose: _onClose, show }: SellTokenMo control={control} error={formState.errors.tokenAmount?.message} pricePerUnit={pricePerUnit} - maxValue={Math.min(+(currentAmm?.mintedByAmm ?? 0), userTokenBalance)} + maxValue={Math.min(ammBalance, userTokenBalance)} details={formDetails} showTresholdButtons validation={(value) => { if (!value || value < 1) { return 'You need to sell at least one token' } - if (value > +(currentAmm?.mintedByAmm ?? 0)) - return 'You cannot sell more tokens than available in the market' + if (value > ammBalance) return 'You cannot sell more tokens than available in the market' if (value > userTokenBalance) return 'Amount exceeds your account balance' return true }} diff --git a/packages/atlas/src/components/_inputs/TokenInput/TokenInput.tsx b/packages/atlas/src/components/_inputs/TokenInput/TokenInput.tsx index be59f4ea90..7ba0152814 100644 --- a/packages/atlas/src/components/_inputs/TokenInput/TokenInput.tsx +++ b/packages/atlas/src/components/_inputs/TokenInput/TokenInput.tsx @@ -51,7 +51,7 @@ const _TokenInput: ForwardRefRenderFunction = const valueStr = event.target.value const valueNum = event.target.valueAsNumber - if (valueStr.length < MAX_LENGTH && valueNum < (maxValue || Number.MAX_SAFE_INTEGER)) { + if (valueStr.length < MAX_LENGTH && (valueNum || 0) <= (maxValue || Number.MAX_SAFE_INTEGER)) { setInternalValue(valueStr) onChange(valueNum) }