Skip to content

Commit

Permalink
Fix sell token logic to consider amm balance
Browse files Browse the repository at this point in the history
  • Loading branch information
WRadoslaw committed Mar 27, 2024
1 parent 4ab02dd commit d9c5080
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,42 @@ export const AmmModalFormTemplate = ({
...(validation ? { valid: validation } : {}),
},
}}
render={({ field }) => (
<FlexBox gap={2} flow="column" width="100%">
<FormField error={error}>
<TokenInput
value={field.value}
onChange={(value) => field.onChange(Math.round(value ?? 0))}
placeholder="0"
maxValue={maxValue}
nodeEnd={
pricePerUnit ? (
<Text variant="t300" as="p" color="colorTextMuted">
${convertTokensToUSD((field.value || 0) * pricePerUnit)?.toFixed(2)}
</Text>
) : null
}
/>
</FormField>
{showTresholdButtons && typeof maxValue === 'number' ? (
<FlexBox gap={2} width="100%" equalChildren>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.25))}>
25%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.5))}>
50%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.75))}>
75%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(maxValue)}>
100%
</Button>
</FlexBox>
) : null}
</FlexBox>
)}
render={({ field }) => {
return (
<FlexBox gap={2} flow="column" width="100%">
<FormField error={error}>
<TokenInput
value={field.value}
onChange={(value) => field.onChange(value ? Math.round(value) : value)}
placeholder="0"
nodeEnd={
pricePerUnit ? (
<Text variant="t300" as="p" color="colorTextMuted">
${convertTokensToUSD((field.value || 0) * pricePerUnit)?.toFixed(2)}
</Text>
) : null
}
/>
</FormField>
{showTresholdButtons && typeof maxValue === 'number' ? (
<FlexBox gap={2} width="100%" equalChildren>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.25))}>
25%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.5))}>
50%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(Math.round(maxValue * 0.75))}>
75%
</Button>
<Button size="small" variant="secondary" onClick={() => field.onChange(maxValue)}>
100%
</Button>
</FlexBox>
) : null}
</FlexBox>
)
}}
/>

<FlexBox flow="column" gap={2}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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(() => {
Expand Down Expand Up @@ -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
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const _TokenInput: ForwardRefRenderFunction<HTMLInputElement, TokenInputProps> =
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)
}
Expand Down

0 comments on commit d9c5080

Please sign in to comment.