fix: vault calculation of assets (#486)

This commit is contained in:
Linkie Link 2023-09-19 15:39:28 +02:00 committed by GitHub
parent 50fd39e926
commit 9ced4ab6c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 17 deletions

View File

@ -28,13 +28,11 @@ interface Props {
onChangeDeposits: (deposits: BNCoin[]) => void onChangeDeposits: (deposits: BNCoin[]) => void
onChangeIsCustomRatio: (isCustomRatio: boolean) => void onChangeIsCustomRatio: (isCustomRatio: boolean) => void
toggleOpen: (index: number) => void toggleOpen: (index: number) => void
displayCurrency: string
depositCapReachedCoins: BNCoin[] depositCapReachedCoins: BNCoin[]
} }
export default function VaultDeposit(props: Props) { export default function VaultDeposit(props: Props) {
const { deposits, primaryAsset, secondaryAsset, account, onChangeDeposits, displayCurrency } = const { deposits, primaryAsset, secondaryAsset, account, onChangeDeposits } = props
props
const [availablePrimaryAmount, availableSecondaryAmount] = useMemo( const [availablePrimaryAmount, availableSecondaryAmount] = useMemo(
() => [ () => [
accumulateAmounts(primaryAsset.denom, [...account.deposits, ...account.lends]), accumulateAmounts(primaryAsset.denom, [...account.deposits, ...account.lends]),
@ -80,26 +78,55 @@ export default function VaultDeposit(props: Props) {
() => () =>
BN( BN(
Math.min( Math.min(
availablePrimaryAmount.multipliedBy(primaryPrice).toNumber(), availablePrimaryAmount
availableSecondaryAmount.multipliedBy(secondaryPrice).toNumber(), .shiftedBy(-primaryAsset.decimals)
.multipliedBy(primaryPrice)
.toNumber(),
availableSecondaryAmount
.shiftedBy(-secondaryAsset.decimals)
.multipliedBy(secondaryPrice)
.toNumber(),
), ),
), ),
[availablePrimaryAmount, primaryPrice, availableSecondaryAmount, secondaryPrice], [
availablePrimaryAmount,
availableSecondaryAmount,
primaryAsset,
primaryPrice,
secondaryAsset,
secondaryPrice,
],
) )
const primaryMax = useMemo( const primaryMax = useMemo(
() => () =>
props.isCustomRatio props.isCustomRatio
? availablePrimaryAmount ? availablePrimaryAmount
: maxAssetValueNonCustom.dividedBy(primaryPrice).integerValue(), : maxAssetValueNonCustom
[props.isCustomRatio, availablePrimaryAmount, primaryPrice, maxAssetValueNonCustom], .dividedBy(primaryPrice)
) .shiftedBy(primaryAsset.decimals)
const secondaryMax = useMemo( .integerValue(),
() => [
props.isCustomRatio props.isCustomRatio,
? availableSecondaryAmount availablePrimaryAmount,
: maxAssetValueNonCustom.dividedBy(secondaryPrice).decimalPlaces(0), primaryPrice,
[props.isCustomRatio, availableSecondaryAmount, secondaryPrice, maxAssetValueNonCustom], primaryAsset,
maxAssetValueNonCustom,
],
) )
const secondaryMax = useMemo(() => {
return props.isCustomRatio
? availableSecondaryAmount
: maxAssetValueNonCustom
.dividedBy(secondaryPrice)
.shiftedBy(secondaryAsset.decimals)
.integerValue()
}, [
props.isCustomRatio,
availableSecondaryAmount,
secondaryPrice,
secondaryAsset,
maxAssetValueNonCustom,
])
const [percentage, setPercentage] = useState( const [percentage, setPercentage] = useState(
primaryValue.dividedBy(maxAssetValueNonCustom).multipliedBy(100).decimalPlaces(0).toNumber() || primaryValue.dividedBy(maxAssetValueNonCustom).multipliedBy(100).decimalPlaces(0).toNumber() ||

View File

@ -148,7 +148,6 @@ export default function VaultModalContent(props: Props) {
toggleOpen={toggleOpen} toggleOpen={toggleOpen}
isCustomRatio={isCustomRatio} isCustomRatio={isCustomRatio}
onChangeIsCustomRatio={onChangeIsCustomRatio} onChangeIsCustomRatio={onChangeIsCustomRatio}
displayCurrency={displayCurrency}
depositCapReachedCoins={depositCapReachedCoins} depositCapReachedCoins={depositCapReachedCoins}
/> />
), ),

View File

@ -201,7 +201,7 @@ export default function SwapForm(props: Props) {
BNCoin.fromDenomAndBigNumber(sellAsset.denom, BN_ZERO), BNCoin.fromDenomAndBigNumber(sellAsset.denom, BN_ZERO),
isAutoLendEnabled ? 'lend' : 'deposit', isAutoLendEnabled ? 'lend' : 'deposit',
) )
}, [buyAsset.denom, sellAsset.denom, simulateTrade, isAutoLendEnabled]) }, [buyAsset.denom, sellAsset.denom, simulateTrade, isAutoLendEnabled, setMarginChecked])
useEffect(() => { useEffect(() => {
const removeDepositAmount = sellAssetAmount.isGreaterThanOrEqualTo(sellSideMarginThreshold) const removeDepositAmount = sellAssetAmount.isGreaterThanOrEqualTo(sellSideMarginThreshold)