diff --git a/components/StakeForm.tsx b/components/StakeForm.tsx index fde79d6..95bd995 100644 --- a/components/StakeForm.tsx +++ b/components/StakeForm.tsx @@ -35,6 +35,8 @@ import { Disclosure } from '@headlessui/react' import SheenLoader from './shared/SheenLoader' import useLeverageMax from 'hooks/useLeverageMax' import { sleep } from 'utils' +import ButtonGroup from './forms/ButtonGroup' +import Decimal from 'decimal.js' const set = mangoStore.getState().set @@ -82,6 +84,7 @@ export const walletBalanceForToken = ( function StakeForm({ token: selectedToken }: StakeFormProps) { const { t } = useTranslation(['common', 'account']) const [inputAmount, setInputAmount] = useState('') + const [sizePercentage, setSizePercentage] = useState('') const submitting = mangoStore((s) => s.submittingBoost) const [leverage, setLeverage] = useState(1) const [refreshingWalletTokens, setRefreshingWalletTokens] = useState(false) @@ -93,7 +96,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { borrowBankBorrowRate, leveragedAPY, estimatedNetAPY, - collateralFeeAPY + collateralFeeAPY, } = useBankRates(selectedToken, leverage) const leverageMax = useLeverageMax(selectedToken) * 0.9 // Multiplied by 0.975 becuase you cant actually get to the end of the inifinite geometric series? @@ -116,7 +119,6 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { return price }, [stakeBank, borrowBank, leverage]) - const tokenPositionsFull = useMemo(() => { if (!stakeBank || !usedTokens.length || !totalTokens.length) return false const hasTokenPosition = usedTokens.find( @@ -137,6 +139,19 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { setInputAmount(max.toFixed()) }, [tokenMax]) + const handleSizePercentage = useCallback( + (percentage: string) => { + if (!stakeBank) return + setSizePercentage(percentage) + const amount = floorToDecimal( + new Decimal(percentage).div(100).mul(tokenMax.maxAmount), + stakeBank.mintDecimals, + ) + setInputAmount(amount.toFixed()) + }, + [tokenMax, stakeBank], + ) + const amountToBorrow = useMemo(() => { const borrowPrice = borrowBank?.uiPrice const stakePrice = stakeBank?.uiPrice @@ -146,7 +161,6 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { return borrowAmount }, [leverage, borrowBank, stakeBank, inputAmount]) - const handleRefreshWalletBalances = useCallback(async () => { if (!publicKey) return const actions = mangoStore.getState().actions @@ -159,7 +173,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { const client = mangoStore.getState().client const group = mangoStore.getState().group const actions = mangoStore.getState().actions - const mangoAccountsLength = (mangoStore.getState().mangoAccounts).length + const mangoAccountsLength = mangoStore.getState().mangoAccounts.length const mangoAccount = mangoStore.getState().mangoAccount.current if (!group || !stakeBank || !publicKey) return @@ -290,6 +304,14 @@ function StakeForm({ token: selectedToken }: StakeFormProps) { +
+ handleSizePercentage(p)} + values={['10', '25', '50', '75', '100']} + unit="%" + /> +
diff --git a/components/UnstakeForm.tsx b/components/UnstakeForm.tsx index f0bedaf..1c12cac 100644 --- a/components/UnstakeForm.tsx +++ b/components/UnstakeForm.tsx @@ -148,7 +148,8 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) { const actions = mangoStore.getState().actions let mangoAccount = mangoStore.getState().mangoAccount.current - if (!group || !stakeBank || !borrowBank || !publicKey || !mangoAccount) return + if (!group || !stakeBank || !borrowBank || !publicKey || !mangoAccount) + return setSubmitting(true) try { @@ -157,7 +158,10 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) { title: 'Sending transaction 1 of 2', type: 'info', }) - console.log('unstake and swap', mangoAccount.getTokenBalanceUi(borrowBank)) + console.log( + 'unstake and swap', + mangoAccount.getTokenBalanceUi(borrowBank), + ) const { signature: tx } = await unstakeAndSwap( client, @@ -208,7 +212,7 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) { type: 'error', }) } - }, [stakeBank, publicKey, inputAmount]) + }, [borrowBank, stakeBank, publicKey, inputAmount]) const showInsufficientBalance = tokenMax.maxAmount < Number(inputAmount) || @@ -296,29 +300,32 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) { {({ open }) => ( <> - {stakeBank.name == 'USDC' - ? + {stakeBank.name == 'USDC' ? ( <> - : - <> - -
-

Staked Amount

-
- - - - + +
+

Staked Amount

+
+ + + + + /> +
-
- +

Staked Amount

@@ -333,10 +340,11 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) {

USDC borrowed

{borrowBank ? ( 0.001 - ? 'text-th-fgd-1' - : 'text-th-bkg-4' - }`} + className={`font-bold ${ + borrowed > 0.001 + ? 'text-th-fgd-1' + : 'text-th-bkg-4' + }`} > ) : null}
-
- } + + + )} )}