This commit is contained in:
Adrian Brzeziński 2024-02-20 22:41:06 +01:00
parent 6ae77f5077
commit 1345ea5185
5 changed files with 270 additions and 299 deletions

View File

@ -1,11 +1,7 @@
import { import { ArrowPathIcon, ExclamationCircleIcon } from '@heroicons/react/20/solid'
ArrowPathIcon,
ChevronDownIcon,
ExclamationCircleIcon,
} from '@heroicons/react/20/solid'
import { useWallet } from '@solana/wallet-adapter-react' import { useWallet } from '@solana/wallet-adapter-react'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import React, { useCallback, useEffect, useMemo, useState } from 'react' import React, { useCallback, useMemo, useState } from 'react'
import NumberFormat, { NumberFormatValues } from 'react-number-format' import NumberFormat, { NumberFormatValues } from 'react-number-format'
import mangoStore from '@store/mangoStore' import mangoStore from '@store/mangoStore'
import { notify } from '../utils/notifications' import { notify } from '../utils/notifications'
@ -24,17 +20,11 @@ import SecondaryConnectButton from './shared/SecondaryConnectButton'
import useMangoAccountAccounts from 'hooks/useMangoAccountAccounts' import useMangoAccountAccounts from 'hooks/useMangoAccountAccounts'
import InlineNotification from './shared/InlineNotification' import InlineNotification from './shared/InlineNotification'
import Link from 'next/link' import Link from 'next/link'
import LeverageSlider from './shared/LeverageSlider'
import useMangoGroup from 'hooks/useMangoGroup' import useMangoGroup from 'hooks/useMangoGroup'
import FormatNumericValue from './shared/FormatNumericValue' import { depositAndCreate } from 'utils/transactions'
import { depositAndCreate, stakeAndCreate } from 'utils/transactions'
// import { MangoAccount } from '@blockworks-foundation/mango-v4' // import { MangoAccount } from '@blockworks-foundation/mango-v4'
import { AnchorProvider } from '@project-serum/anchor' import { AnchorProvider } from '@project-serum/anchor'
import useBankRates from 'hooks/useBankRates'
import { Disclosure } from '@headlessui/react'
import SheenLoader from './shared/SheenLoader' import SheenLoader from './shared/SheenLoader'
import useLeverageMax from 'hooks/useLeverageMax'
import { STAKEABLE_TOKENS_DATA } from 'utils/constants'
import { sleep } from 'utils' import { sleep } from 'utils'
import ButtonGroup from './forms/ButtonGroup' import ButtonGroup from './forms/ButtonGroup'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
@ -63,7 +53,6 @@ export const walletBalanceForToken = (
: null : null
} }
return { return {
maxAmount: walletToken ? walletToken.uiAmount : 0, maxAmount: walletToken ? walletToken.uiAmount : 0,
maxDecimals: bank?.mintDecimals || 6, maxDecimals: bank?.mintDecimals || 6,
@ -199,7 +188,6 @@ function DespositForm({ token: selectedToken }: StakeFormProps) {
[tokenMax, depositBank], [tokenMax, depositBank],
) )
return ( return (
<> <>
<div className="flex flex-col justify-between"> <div className="flex flex-col justify-between">
@ -271,8 +259,7 @@ function DespositForm({ token: selectedToken }: StakeFormProps) {
</div> </div>
</div> </div>
{depositBank ? ( {depositBank ? (
<div className="pt-8"> <div className="pt-8"></div>
</div>
) : !groupLoaded ? ( ) : !groupLoaded ? (
<div className="pt-8"> <div className="pt-8">
<SheenLoader className="flex flex-1 rounded-xl"> <SheenLoader className="flex flex-1 rounded-xl">

View File

@ -6,7 +6,6 @@ import TransactionHistory from './TransactionHistory'
import mangoStore, { ActiveTab } from '@store/mangoStore' import mangoStore, { ActiveTab } from '@store/mangoStore'
import { useCallback, useEffect } from 'react' import { useCallback, useEffect } from 'react'
import { BOOST_ACCOUNT_PREFIX } from 'utils/constants' import { BOOST_ACCOUNT_PREFIX } from 'utils/constants'
import useMangoAccount from 'hooks/useMangoAccount'
const set = mangoStore.getState().set const set = mangoStore.getState().set
@ -34,8 +33,6 @@ const HomePage = () => {
}) })
}, [selectedToken]) }, [selectedToken])
return ( return (
<> <>
<div className="mb-6 grid grid-cols-3"> <div className="mb-6 grid grid-cols-3">

View File

@ -43,7 +43,8 @@ const Positions = ({
}: { }: {
setActiveTab: (tab: ActiveTab) => void setActiveTab: (tab: ActiveTab) => void
}) => { }) => {
const [showInactivePositions, setShowInactivePositions] = useLocalStorageState(SHOW_INACTIVE_POSITIONS_KEY, true) const [showInactivePositions, setShowInactivePositions] =
useLocalStorageState(SHOW_INACTIVE_POSITIONS_KEY, true)
const { borrowBank, positions } = usePositions(showInactivePositions) const { borrowBank, positions } = usePositions(showInactivePositions)
console.log(showInactivePositions) console.log(showInactivePositions)
@ -55,7 +56,8 @@ const Positions = ({
return ( return (
<> <>
<div className="mb-2 flex items-center justify-between rounded-lg border-2 border-th-fgd-1 bg-th-bkg-1 px-6 py-3.5"> <div className="mb-2 flex items-center justify-between rounded-lg border-2 border-th-fgd-1 bg-th-bkg-1 px-6 py-3.5">
<p className="font-medium">{`You have ${numberOfPositions} active position${numberOfPositions !== 1 ? 's' : '' <p className="font-medium">{`You have ${numberOfPositions} active position${
numberOfPositions !== 1 ? 's' : ''
}`}</p> }`}</p>
<Switch <Switch
checked={showInactivePositions} checked={showInactivePositions}
@ -133,21 +135,13 @@ const PositionItem = ({
const liqPriceChangePercentage = const liqPriceChangePercentage =
((parseFloat(liqRatio) - currentPriceRatio) / currentPriceRatio) * 100 ((parseFloat(liqRatio) - currentPriceRatio) / currentPriceRatio) * 100
return [liqRatio, liqPriceChangePercentage.toFixed(2)] return [liqRatio, liqPriceChangePercentage.toFixed(2)]
}, [bank, borrowBalance, borrowBank, stakeBalance]) }, [bank, borrowBalance, borrowBank, stakeBalance])
const { estimatedNetAPY, borrowBankBorrowRate } = useBankRates(
const liquidationPrice = useMemo(() => { bank.name,
const borrowMaintLiabWeight = borrowBank?.maintLiabWeight leverage,
const stakeMaintAssetWeight = position.bank?.maintAssetWeight )
const price = Number(position.bank?.uiPrice) * (Number(borrowMaintLiabWeight) / Number(stakeMaintAssetWeight)) * (1 - (1 / leverage))
return price
}, [position.bank, borrowBank, leverage])
const { estimatedNetAPY, borrowBankBorrowRate } = useBankRates(bank.name, leverage)
const uiRate = bank.name == 'USDC' ? borrowBankBorrowRate : estimatedNetAPY const uiRate = bank.name == 'USDC' ? borrowBankBorrowRate : estimatedNetAPY
return ( return (
@ -190,10 +184,9 @@ const PositionItem = ({
<FormatNumericValue value={uiRate} decimals={2} />% <FormatNumericValue value={uiRate} decimals={2} />%
</span> </span>
</div> </div>
{position.bank.name == 'USDC' ? {position.bank.name == 'USDC' ? (
<> <></>
</> ) : (
:
<> <>
<div> <div>
<p className="mb-1 text-th-fgd-4">Leverage</p> <p className="mb-1 text-th-fgd-4">Leverage</p>
@ -225,8 +218,7 @@ const PositionItem = ({
</div> </div>
</div> </div>
</> </>
} )}
</div> </div>
</div> </div>
) )

View File

@ -9,7 +9,6 @@ import { formatTokenSymbol } from 'utils/tokens'
import { useViewport } from 'hooks/useViewport' import { useViewport } from 'hooks/useViewport'
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/20/solid' import { ArrowTopRightOnSquareIcon } from '@heroicons/react/20/solid'
import DespositForm from './DepositForm' import DespositForm from './DepositForm'
import WithdrawForm from './WithdrawForm'
const set = mangoStore.getState().set const set = mangoStore.getState().set
@ -50,24 +49,23 @@ const Stake = () => {
onChange={(v) => setActiveFormTab(v)} onChange={(v) => setActiveFormTab(v)}
/> />
</div> </div>
{selectedToken == 'USDC' ? {selectedToken == 'USDC' ? (
<> <>
{activeFormTab === 'Add' ? ( {activeFormTab === 'Add' ? <DespositForm token="USDC" /> : null}
<DespositForm token='USDC' />
) : null}
{activeFormTab === 'Remove' ? ( {activeFormTab === 'Remove' ? (
<UnstakeForm token='USDC' /> <UnstakeForm token="USDC" />
) : null} ) : null}
</> </>
: ) : (
<> <>
{activeFormTab === 'Add' ? ( {activeFormTab === 'Add' ? (
<StakeForm token={selectedToken} /> <StakeForm token={selectedToken} />
) : null} ) : null}
{activeFormTab === 'Remove' ? ( {activeFormTab === 'Remove' ? (
<UnstakeForm token={selectedToken} /> <UnstakeForm token={selectedToken} />
) : null}</> ) : null}
} </>
)}
</div> </div>
</div> </div>
</div> </div>

View File

@ -13,14 +13,12 @@ const TokenButton = ({
handleTokenSelect: (v: string) => void handleTokenSelect: (v: string) => void
}) => { }) => {
const leverage = useLeverageMax(tokenName) const leverage = useLeverageMax(tokenName)
const { const { borrowBankBorrowRate, borrowBankStakeRate } = useBankRates(
stakeBankDepositRate, tokenName,
borrowBankBorrowRate, leverage,
borrowBankStakeRate, )
leveragedAPY, const UiRate =
estimatedNetAPY, tokenName == 'USDC' ? borrowBankBorrowRate : borrowBankStakeRate
} = useBankRates(tokenName, leverage)
const UiRate = tokenName == 'USDC' ? borrowBankBorrowRate : borrowBankStakeRate
return ( return (
<button <button
@ -68,7 +66,6 @@ const TokenButton = ({
// ) : // ) :
`${UiRate.toFixed(2)}%` `${UiRate.toFixed(2)}%`
} }
</span> </span>
</div> </div>
</button> </button>