From 501510eb4342453e7e35b961b6f89a9e81defd77 Mon Sep 17 00:00:00 2001 From: tjs Date: Sat, 20 Aug 2022 20:09:36 -0400 Subject: [PATCH] fix max numbers shown in modals and swap form --- components/account/ActionTokenItem.tsx | 10 +-- components/modals/AccountNameModal.tsx | 2 +- components/modals/BorrowModal.tsx | 80 ++++++++++++++---------- components/modals/CreateAccountModal.tsx | 2 +- components/modals/DepositModal.tsx | 13 ++-- components/modals/UserSetupModal.tsx | 2 +- components/modals/WithdrawModal.tsx | 18 +++--- components/swap/JupiterRouteInfo.tsx | 52 ++++++++------- components/swap/SwapForm.tsx | 28 ++++++--- components/swap/SwapSettings.tsx | 2 - components/swap/SwapTokenChart.tsx | 2 +- public/locales/en/common.json | 1 + public/locales/es/common.json | 1 + public/locales/zh/common.json | 1 + public/locales/zh_tw/common.json | 1 + store/mangoStore.ts | 4 +- 16 files changed, 127 insertions(+), 92 deletions(-) diff --git a/components/account/ActionTokenItem.tsx b/components/account/ActionTokenItem.tsx index 37740662..af2e2354 100644 --- a/components/account/ActionTokenItem.tsx +++ b/components/account/ActionTokenItem.tsx @@ -32,11 +32,11 @@ const ActionTokenItem = ({ return ( diff --git a/components/modals/AccountNameModal.tsx b/components/modals/AccountNameModal.tsx index 210b73aa..377c8794 100644 --- a/components/modals/AccountNameModal.tsx +++ b/components/modals/AccountNameModal.tsx @@ -40,7 +40,7 @@ const AccountNameModal = ({ isOpen, onClose }: ModalProps) => { txid: e?.signature, type: 'error', }) - console.log(e) + console.error(e) } } diff --git a/components/modals/BorrowModal.tsx b/components/modals/BorrowModal.tsx index 91bb0672..71ce6477 100644 --- a/components/modals/BorrowModal.tsx +++ b/components/modals/BorrowModal.tsx @@ -1,4 +1,9 @@ -import { toUiDecimals } from '@blockworks-foundation/mango-v4' +import { + Bank, + Group, + MangoAccount, + toUiDecimals, +} from '@blockworks-foundation/mango-v4' import { ChevronDownIcon } from '@heroicons/react/solid' import { useTranslation } from 'next-i18next' import Image from 'next/image' @@ -7,7 +12,7 @@ import mangoStore from '../../store/mangoStore' import { ModalProps } from '../../types/modal' import { INPUT_TOKEN_DEFAULT } from '../../utils/constants' import { notify } from '../../utils/notifications' -import { formatFixedDecimals } from '../../utils/numbers' +import { floorToDecimal, formatFixedDecimals } from '../../utils/numbers' import ActionTokenList from '../account/ActionTokenList' import ButtonGroup from '../forms/ButtonGroup' import Input from '../forms/Input' @@ -17,7 +22,30 @@ import HealthImpact from '../shared/HealthImpact' import Loading from '../shared/Loading' import Modal from '../shared/Modal' import { EnterBottomExitBottom, FadeInFadeOut } from '../shared/Transitions' -import { BorrowLeverageSlider } from '../swap/LeverageSlider' +// import { BorrowLeverageSlider } from '../swap/LeverageSlider' + +const getMaxBorrowForToken = ( + group: Group, + mangoAccount: MangoAccount, + bank: Bank +) => { + const vaultBalance = floorToDecimal( + bank.uiDeposits() - bank.uiBorrows(), + bank.mintDecimals + ) + const currentBalance = mangoAccount?.getTokenBalanceUi(bank) || 0 + const maxBorrow = toUiDecimals( + mangoAccount?.getMaxWithdrawWithBorrowForToken(group, bank.mint).toNumber(), + bank.mintDecimals + ) + const maxBorrowWithBalance = + currentBalance > 0 ? maxBorrow + currentBalance : maxBorrow + + return Math.min( + vaultBalance, + floorToDecimal(maxBorrowWithBalance, bank.mintDecimals) + ) +} interface BorrowModalProps { token?: string @@ -56,12 +84,9 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { const tokenMax = useMemo(() => { const group = mangoStore.getState().group - if (!group || !bank) return 0 - const amount = mangoAccount - ?.getMaxWithdrawWithBorrowForToken(group, bank.mint) - .toNumber() - return amount && amount > 0 ? toUiDecimals(amount, bank.mintDecimals) : 0 - }, [mangoAccount, bank, selectedToken]) + if (!group || !bank || !mangoAccount) return 0 + return getMaxBorrowForToken(group, mangoAccount, bank) + }, [mangoAccount, bank]) const setMax = useCallback(() => { setInputAmount(tokenMax.toString()) @@ -69,11 +94,12 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { const handleSizePercentage = useCallback( (percentage: string) => { + if (!bank) return setSizePercentage(percentage) const amount = (Number(percentage) / 100) * (tokenMax || 0) - setInputAmount(amount.toString()) + setInputAmount(floorToDecimal(amount, bank.mintDecimals).toString()) }, - [tokenMax] + [tokenMax, bank] ) const handleSelectToken = (token: string) => { @@ -103,8 +129,7 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { }) actions.reloadAccount() } catch (e: any) { - console.log(e) - + console.error(e) notify({ title: 'Transaction failed', description: e.message, @@ -122,17 +147,8 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { return group?.banksMapByName ? Array.from(group?.banksMapByName, ([key, value]) => { const bank = value[0] - const amount = mangoAccount - ?.getMaxWithdrawWithBorrowForToken(group, bank.mint) - .toNumber() - return { - key, - value, - maxAmount: - amount && amount > 0 - ? toUiDecimals(amount, bank.mintDecimals) - : 0, - } + const maxAmount = getMaxBorrowForToken(group, mangoAccount, bank) + return { key, value, maxAmount } }) : [] } @@ -146,14 +162,14 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { show={showTokenList} >

{t('select-token')}

-
-
+
+

{t('token')}

-
+

{t('borrow-rate')}

-
+

{t('max-borrow')}

@@ -177,9 +193,7 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { {t('max')}: - - {formatFixedDecimals(tokenMax)} - + {tokenMax}
@@ -222,7 +236,7 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { unit="%" />
-
+ {/*

{t('leverage')}

0.00x

@@ -232,7 +246,7 @@ function BorrowModal({ isOpen, onClose, token }: ModalCombinedProps) { tokenMax={tokenMax} onChange={(x) => setInputAmount(x)} /> -
+
*/}
{ const group = mangoStore.getState().group - console.log('1: ', selectedToken) - return group?.banksMapByName.get(selectedToken)![0] }, [selectedToken]) @@ -145,7 +143,7 @@ function DepositModal({ isOpen, onClose, token }: ModalCombinedProps) { } const banks = useMemo(() => { - return group?.banksMapByName + const banks = group?.banksMapByName ? Array.from(group?.banksMapByName, ([key, value]) => { const walletBalance = walletBalanceForToken(walletTokens, key) return { @@ -158,6 +156,7 @@ function DepositModal({ isOpen, onClose, token }: ModalCombinedProps) { } }) : [] + return banks.filter((b) => b.walletBalance > 0) }, [group?.banksMapByName, walletTokens]) return ( @@ -168,14 +167,14 @@ function DepositModal({ isOpen, onClose, token }: ModalCombinedProps) { show={showTokenList} >

{t('select-token')}

-
-
+
+

{t('token')}

-
+

{t('deposit-rate')}

-
+

{t('wallet-balance')}

diff --git a/components/modals/UserSetupModal.tsx b/components/modals/UserSetupModal.tsx index afc1b429..caab0d38 100644 --- a/components/modals/UserSetupModal.tsx +++ b/components/modals/UserSetupModal.tsx @@ -91,7 +91,7 @@ const UserSetupModal = ({ isOpen, onClose }: ModalProps) => { txid: e?.signature, type: 'error', }) - console.log(e) + console.error(e) } }, [accountName, wallet, t]) diff --git a/components/modals/WithdrawModal.tsx b/components/modals/WithdrawModal.tsx index 04b3efa0..9832e19e 100644 --- a/components/modals/WithdrawModal.tsx +++ b/components/modals/WithdrawModal.tsx @@ -91,8 +91,7 @@ function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { }) actions.reloadAccount() } catch (e: any) { - console.log(e) - + console.error(e) notify({ title: 'Transaction failed', description: e.message, @@ -110,9 +109,9 @@ function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { setShowTokenList(false) } - const banks = useMemo(() => { + const withdrawBank = useMemo(() => { if (mangoAccount) { - return group?.banksMapByName + const banks = group?.banksMapByName ? Array.from(group?.banksMapByName, ([key, value]) => { const accountBalance = mangoAccount?.getTokenBalanceUi(value[0]) return { @@ -122,6 +121,7 @@ function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { } }) : [] + return banks.filter((b) => b.accountBalance > 0) } return [] }, [mangoAccount, group?.banksMapByName]) @@ -134,16 +134,16 @@ function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { show={showTokenList} >

{t('select-token')}

-
-
+
+

{t('token')}

-
+

Account Balance

@@ -162,7 +162,7 @@ function WithdrawModal({ isOpen, onClose, token }: ModalCombinedProps) { onClick={() => handleSizePercentage('100')} > - {t('available-balance')} + {t('account-balance')} {tokenMax} diff --git a/components/swap/JupiterRouteInfo.tsx b/components/swap/JupiterRouteInfo.tsx index 3cd2e6ac..aa71dae8 100644 --- a/components/swap/JupiterRouteInfo.tsx +++ b/components/swap/JupiterRouteInfo.tsx @@ -6,7 +6,11 @@ import React, { useState, } from 'react' import { TransactionInstruction, PublicKey } from '@solana/web3.js' -import { toNativeDecimals, toUiDecimals } from '@blockworks-foundation/mango-v4' +import { + HealthType, + toNativeDecimals, + toUiDecimals, +} from '@blockworks-foundation/mango-v4' import { Jupiter, RouteInfo, TransactionFeeInfo } from '@jup-ag/core' import JSBI from 'jsbi' import Decimal from 'decimal.js' @@ -117,27 +121,29 @@ const JupiterRouteInfo = ({ return 'Unknown' const simulatedHealthRatio = mangoAccount - .simHealthRatioWithTokenPositionChanges(group, [ - { - mintPk: new PublicKey(inputTokenInfo.address), - tokenAmount: - toNativeDecimals( - amountIn.toNumber(), - inputTokenInfo.decimals - ).toNumber() * -1, - }, - { - mintPk: new PublicKey(outputTokenInfo.address), - tokenAmount: toNativeDecimals( - amountOut, - outputTokenInfo.decimals - ).toNumber(), - }, - ]) + .simHealthRatioWithTokenPositionChanges( + group, + [ + { + mintPk: new PublicKey(inputTokenInfo.address), + tokenAmount: + toNativeDecimals( + amountIn.toNumber(), + inputTokenInfo.decimals + ).toNumber() * -1, + }, + { + mintPk: new PublicKey(outputTokenInfo.address), + tokenAmount: toNativeDecimals( + amountOut, + outputTokenInfo.decimals + ).toNumber(), + }, + ], + HealthType.maint + ) .toNumber() - - // console.log('simulatedHealthRatio', simulatedHealthRatio) - return simulatedHealthRatio > 100 ? 100 : simulatedHealthRatio + return simulatedHealthRatio > 100 ? 100 : simulatedHealthRatio.toFixed(2) }, [mangoAccount, inputTokenInfo, outputTokenInfo, amountIn, amountOut]) const onSwap = async () => { @@ -177,7 +183,7 @@ const JupiterRouteInfo = ({ }) await actions.reloadAccount() } catch (e: any) { - console.log('Error swapping:', e) + console.error('onSwap error: ', e) notify({ title: 'Transaction failed', description: e.message, @@ -218,7 +224,7 @@ const JupiterRouteInfo = ({ />
-

{`${amountIn} ${ +

{`${amountIn} ${ inputTokenInfo!.name } for ${amountOut} ${outputTokenInfo.symbol}`}

diff --git a/components/swap/SwapForm.tsx b/components/swap/SwapForm.tsx index 358b47cf..2488ad9e 100644 --- a/components/swap/SwapForm.tsx +++ b/components/swap/SwapForm.tsx @@ -308,8 +308,15 @@ export const useTokenMax = () => { if (!group || !inputBank || !mangoAccount || !outputBank) return { amount: 0.0, decimals: 6, amountWithBorrow: 0.0 } - const amount = mangoAccount.getTokenBalanceUi(inputBank) - const amountWithBorrow = mangoAccount + const inputBankLiquidity = floorToDecimal( + inputBank.uiDeposits() - inputBank.uiBorrows(), + inputBank.mintDecimals + ) + const maxWithdrawableAmount = Math.min( + mangoAccount.getTokenBalanceUi(inputBank), + inputBankLiquidity + ) + const maxNativeAmountWithBorrow = mangoAccount ?.getMaxSourceForTokenSwap( group, inputBank.mint, @@ -317,14 +324,21 @@ export const useTokenMax = () => { 0.98 - slippage / 10 ) .toNumber() + const maxUiAmountWithBorrow = toUiDecimals( + maxNativeAmountWithBorrow, + inputBank.mintDecimals + ) return { - amount: amount > 0 ? floorToDecimal(amount, inputBank.mintDecimals) : 0, + amount: + maxWithdrawableAmount > 0 + ? floorToDecimal(maxWithdrawableAmount, inputBank.mintDecimals) + : 0, amountWithBorrow: - amountWithBorrow > 0 - ? floorToDecimal( - toUiDecimals(amountWithBorrow, inputBank.mintDecimals), - inputBank.mintDecimals + maxUiAmountWithBorrow > 0 + ? Math.min( + floorToDecimal(maxUiAmountWithBorrow, inputBank.mintDecimals), + inputBankLiquidity ) : 0, decimals: inputBank.mintDecimals, diff --git a/components/swap/SwapSettings.tsx b/components/swap/SwapSettings.tsx index 39d29f99..803a16aa 100644 --- a/components/swap/SwapSettings.tsx +++ b/components/swap/SwapSettings.tsx @@ -30,8 +30,6 @@ const SwapSettings = ({ onClose }: { onClose: () => void }) => { }) } - console.log('slippage', slippage) - return ( <>

{t('settings')}

diff --git a/components/swap/SwapTokenChart.tsx b/components/swap/SwapTokenChart.tsx index b92759e2..c22c9c7e 100644 --- a/components/swap/SwapTokenChart.tsx +++ b/components/swap/SwapTokenChart.tsx @@ -129,7 +129,7 @@ const SwapTokenChart: FunctionComponent = ({ setChartData(chartData) setLoadChartData(false) } catch (e) { - console.log('Unable to load chart data') + console.warn('Unable to load chart data') } }, [baseTokenId, quoteTokenId, daysToShow]) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index b45cc4ae..114b7823 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -1,5 +1,6 @@ { "account-closed": "Account Closed 👋", + "account-balance": "Account Balance", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", "account-update-failed": "Failed to update account", diff --git a/public/locales/es/common.json b/public/locales/es/common.json index 723b2ea1..d717aa0e 100644 --- a/public/locales/es/common.json +++ b/public/locales/es/common.json @@ -1,4 +1,5 @@ { + "account-balance": "Account Balance", "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", diff --git a/public/locales/zh/common.json b/public/locales/zh/common.json index 079fc111..25c94a75 100644 --- a/public/locales/zh/common.json +++ b/public/locales/zh/common.json @@ -1,4 +1,5 @@ { + "account-balance": "Account Balance", "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", diff --git a/public/locales/zh_tw/common.json b/public/locales/zh_tw/common.json index 52710fd4..e176b94a 100644 --- a/public/locales/zh_tw/common.json +++ b/public/locales/zh_tw/common.json @@ -1,4 +1,5 @@ { + "account-balance": "Account Balance", "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", diff --git a/store/mangoStore.ts b/store/mangoStore.ts index b7b32e3d..8cdbcd77 100644 --- a/store/mangoStore.ts +++ b/store/mangoStore.ts @@ -305,7 +305,7 @@ const mangoStore = create( state.coingeckoPrices.loading = false }) } catch (e) { - console.log('ERORR: Unable to load Coingecko prices') + console.warn('Unable to load Coingecko prices') set((state) => { state.coingeckoPrices.loading = false }) @@ -583,7 +583,7 @@ const mangoStore = create( state.wallet.loadProfilePic = false }) } catch (e) { - console.log('Could not get profile picture', e) + console.error('Could not get profile picture', e) set((state) => { state.wallet.loadProfilePic = false })