mango-ui-v2/components/MarginBalances.tsx

198 lines
6.6 KiB
TypeScript
Raw Normal View History

2021-04-09 17:01:00 -07:00
import { useCallback, useState } from 'react'
import { InformationCircleIcon } from '@heroicons/react/outline'
2021-04-09 07:27:49 -07:00
import FloatingElement from './FloatingElement'
2021-04-10 12:21:02 -07:00
import { ElementTitle } from './styles'
2021-04-09 07:27:49 -07:00
import useMangoStore from '../stores/useMangoStore'
import useMarketList from '../hooks/useMarketList'
import { tokenPrecision } from '../utils/index'
2021-04-09 17:01:00 -07:00
import DepositModal from './DepositModal'
2021-04-10 14:12:15 -07:00
import WithdrawModal from './WithdrawModal'
2021-04-10 12:21:02 -07:00
import Button from './Button'
2021-04-09 07:27:49 -07:00
2021-04-14 23:16:36 -07:00
export default function MarginBalances() {
2021-04-09 07:27:49 -07:00
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const selectedMarginAccount = useMangoStore(
(s) => s.selectedMarginAccount.current
)
2021-04-10 12:21:02 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
2021-04-09 07:27:49 -07:00
const { symbols } = useMarketList()
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
2021-04-09 17:01:00 -07:00
const handleCloseDeposit = useCallback(() => {
setShowDepositModal(false)
}, [])
2021-04-09 07:27:49 -07:00
2021-04-09 17:01:00 -07:00
const handleCloseWithdraw = useCallback(() => {
setShowWithdrawModal(false)
}, [])
2021-04-09 07:27:49 -07:00
return (
<>
<FloatingElement>
<ElementTitle>
Margin Account
{/* <Popover
2021-04-09 07:27:49 -07:00
content={
<AddressTooltip
owner={selectedMarginAccount?.owner.toString()}
marginAccount={selectedMarginAccount?.publicKey.toString()}
/>
}
placement="topLeft"
trigger="hover"
> */}
<div>
<InformationCircleIcon
2021-04-16 04:50:56 -07:00
className={`h-5 w-5 ml-2 text-th-primary cursor-help`}
/>
</div>
{/* </Popover> */}
2021-04-09 07:27:49 -07:00
</ElementTitle>
{selectedMangoGroup ? (
<table className={`min-w-full`}>
2021-04-09 07:27:49 -07:00
<thead>
<tr className={`text-center text-th-fgd-4 mb-2`}>
2021-04-13 07:10:57 -07:00
<th scope="col" className={`flex-auto font-normal`}>
2021-04-09 07:27:49 -07:00
Assets
</th>
2021-04-13 07:10:57 -07:00
<th scope="col" className={`flex-auto font-normal`}>
2021-04-09 07:27:49 -07:00
Deposits
</th>
2021-04-13 07:10:57 -07:00
<th scope="col" className={`flex-auto font-normal`}>
2021-04-09 07:27:49 -07:00
Borrows
</th>
2021-04-13 07:10:57 -07:00
<th scope="col" className={`flex-auto font-normal`}>
2021-04-10 14:12:15 -07:00
Interest (APY)
2021-04-09 07:27:49 -07:00
</th>
</tr>
</thead>
<tbody>
{Object.entries(symbols).map(([name], i) => (
2021-04-16 04:50:56 -07:00
<tr key={name} className={`text-th-fgd-1`}>
<td className={`flex items-center py-2`}>
2021-04-09 07:27:49 -07:00
<img
alt=""
width="20"
height="20"
src={`/assets/icons/${name.toLowerCase()}.svg`}
2021-04-16 04:50:56 -07:00
className={`mr-2.5`}
2021-04-09 07:27:49 -07:00
/>
<span>{name}</span>
</td>
<td className={`text-center`}>
2021-04-09 07:27:49 -07:00
{selectedMarginAccount
? selectedMarginAccount
.getUiDeposit(selectedMangoGroup, i)
.toFixed(tokenPrecision[name])
: (0).toFixed(tokenPrecision[name])}
</td>
<td className={`text-center`}>
2021-04-09 07:27:49 -07:00
{selectedMarginAccount
? selectedMarginAccount
2021-04-14 11:40:28 -07:00
.getUiBorrow(selectedMangoGroup, i)
2021-04-09 07:27:49 -07:00
.toFixed(tokenPrecision[name])
: (0).toFixed(tokenPrecision[name])}
</td>
<td className={`text-center`}>
<span className={`text-th-green`}>
2021-04-09 07:27:49 -07:00
+{(selectedMangoGroup.getDepositRate(i) * 100).toFixed(2)}
%
</span>
2021-04-13 07:10:57 -07:00
<span className={`text-th-fgd-4`}>{' / '}</span>
<span className={`text-th-red`}>
2021-04-09 07:27:49 -07:00
-{(selectedMangoGroup.getBorrowRate(i) * 100).toFixed(2)}%
</span>
</td>
</tr>
))}
</tbody>
</table>
) : null}
2021-04-13 07:10:57 -07:00
<div className={`flex justify-center items-center mt-4`}>
<Button
onClick={() => setShowDepositModal(true)}
className="w-1/2"
disabled={!connected}
>
<span>Deposit</span>
</Button>
<Button
onClick={() => setShowWithdrawModal(true)}
className="ml-4 w-1/2"
2021-04-13 16:41:04 -07:00
disabled={!connected || !selectedMarginAccount}
2021-04-13 07:10:57 -07:00
>
<span>Withdraw</span>
</Button>
2021-04-09 07:27:49 -07:00
</div>
2021-04-16 04:50:56 -07:00
<div className={`text-center mt-5 text-th-fgd-4 text-sm`}>
2021-04-09 07:27:49 -07:00
Settle funds in the Balances tab
</div>
</FloatingElement>
{showDepositModal && (
2021-04-09 17:01:00 -07:00
<DepositModal isOpen={showDepositModal} onClose={handleCloseDeposit} />
2021-04-09 07:27:49 -07:00
)}
{showWithdrawModal && (
2021-04-10 14:12:15 -07:00
<WithdrawModal
2021-04-09 07:27:49 -07:00
isOpen={showWithdrawModal}
2021-04-09 17:01:00 -07:00
onClose={handleCloseWithdraw}
2021-04-09 07:27:49 -07:00
/>
)}
</>
)
}
// const AddressTooltip = ({
// owner,
// marginAccount,
// }: {
// owner?: string
// marginAccount?: string
// }) => {
// return (
// <>
// {owner && marginAccount ? (
// <>
// <div className={`flex`}>
// Margin Account:
// <a
// href={'https://explorer.solana.com/address/' + marginAccount}
// target="_blank"
// rel="noopener noreferrer"
// >
// <div className={`ml-4 flex`}>
// <ExternalLinkIcon
// className={`h-5 w-5 mr-1 text-mango-yellow`}
// />
// <span className={`text-mango-yellow hover:opacity-50`}>
// {marginAccount}
// </span>
// </div>
// </a>
// </div>
// <div className={`flex mt-2`}>
// Account Owner:
// <a
// href={'https://explorer.solana.com/address/' + owner}
// target="_blank"
// rel="noopener noreferrer"
// >
// <div className={`ml-4 flex`}>
// <ExternalLinkIcon
// className={`h-5 w-5 mr-1 text-mango-yellow`}
// />
// <span className={`text-mango-yellow hover:opacity-50`}>
// {owner}
// </span>
// </div>
// </a>
// </div>
// </>
// ) : (
// 'Connect a wallet and deposit funds to start trading'
// )}
// </>
// )
// }