mango-ui-v3/components/DepositModal.tsx

172 lines
5.5 KiB
TypeScript
Raw Normal View History

2021-04-09 17:01:00 -07:00
import React, { useMemo, useState } from 'react'
import { nativeToUi } from '@blockworks-foundation/mango-client/lib/utils'
import Modal from './Modal'
2021-04-18 03:34:37 -07:00
import Input from './Input'
2021-04-09 17:01:00 -07:00
import AccountSelect from './AccountSelect'
2021-04-18 03:34:37 -07:00
import { ElementTitle } from './styles'
2021-04-09 17:01:00 -07:00
import useMangoStore from '../stores/useMangoStore'
import useMarketList from '../hooks/useMarketList'
2021-04-20 15:10:10 -07:00
import { getSymbolForTokenMintAddress } from '../utils/index'
2021-04-09 17:01:00 -07:00
import useConnection from '../hooks/useConnection'
import { deposit, initMarginAccountAndDeposit } from '../utils/mango'
import { PublicKey } from '@solana/web3.js'
import Loading from './Loading'
2021-04-10 12:21:02 -07:00
import Button from './Button'
2021-04-12 20:39:08 -07:00
import { notify } from '../utils/notifications'
2021-04-09 17:01:00 -07:00
const DepositModal = ({ isOpen, onClose }) => {
const [inputAmount, setInputAmount] = useState('')
const [submitting, setSubmitting] = useState(false)
2021-04-12 20:39:08 -07:00
const { getTokenIndex, symbols } = useMarketList()
2021-04-09 17:01:00 -07:00
const { connection, programId } = useConnection()
const mintDecimals = useMangoStore((s) => s.selectedMangoGroup.mintDecimals)
const walletAccounts = useMangoStore((s) => s.wallet.balances)
const actions = useMangoStore((s) => s.actions)
const depositAccounts = useMemo(
() =>
walletAccounts.filter((acc) =>
Object.values(symbols).includes(acc.account.mint.toString())
),
[symbols, walletAccounts]
)
const [selectedAccount, setSelectedAccount] = useState(depositAccounts[0])
const handleAccountSelect = (account) => {
setSelectedAccount(account)
setInputAmount('')
}
2021-04-09 17:01:00 -07:00
// TODO: remove duplication in AccountSelect
const getBalanceForAccount = (account) => {
const mintAddress = account?.account.mint.toString()
const balance = nativeToUi(
account?.account?.amount,
mintDecimals[getTokenIndex(mintAddress)]
)
2021-04-20 15:10:10 -07:00
return balance.toString()
2021-04-09 17:01:00 -07:00
}
const setMaxForSelectedAccount = () => {
const max = getBalanceForAccount(selectedAccount)
setInputAmount(max)
}
const handleDeposit = () => {
setSubmitting(true)
const marginAccount = useMangoStore.getState().selectedMarginAccount.current
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
2021-04-14 15:46:36 -07:00
console.log('handleDeposit', wallet, walletAccounts)
2021-04-09 17:01:00 -07:00
if (!marginAccount && mangoGroup) {
initMarginAccountAndDeposit(
connection,
new PublicKey(programId),
mangoGroup,
wallet,
selectedAccount.account.mint,
selectedAccount.publicKey,
Number(inputAmount)
)
.then((_response: Array<any>) => {
2021-04-09 17:01:00 -07:00
actions.fetchWalletBalances()
2021-04-14 15:46:36 -07:00
actions.fetchMangoGroup()
actions.fetchMarginAccounts()
2021-04-09 17:01:00 -07:00
setSubmitting(false)
onClose()
})
.catch((err) => {
setSubmitting(false)
console.error(err)
2021-04-12 20:39:08 -07:00
notify({
message:
'Could not perform init margin account and deposit operation',
type: 'error',
})
2021-04-09 17:01:00 -07:00
onClose()
})
} else {
deposit(
connection,
new PublicKey(programId),
2021-04-09 17:01:00 -07:00
mangoGroup,
marginAccount,
wallet,
selectedAccount.account.mint,
selectedAccount.publicKey,
Number(inputAmount)
)
.then((_response: string) => {
2021-04-09 17:01:00 -07:00
actions.fetchWalletBalances()
2021-04-14 15:46:36 -07:00
actions.fetchMangoGroup()
actions.fetchMarginAccounts()
2021-04-09 17:01:00 -07:00
setSubmitting(false)
onClose()
})
.catch((err) => {
setSubmitting(false)
console.error(err)
2021-04-12 20:39:08 -07:00
notify({
message: 'Could not perform deposit operation',
type: 'error',
})
2021-04-09 17:01:00 -07:00
onClose()
})
}
}
return (
<Modal isOpen={isOpen} onClose={onClose}>
<Modal.Header>
2021-04-18 03:34:37 -07:00
<div className={`text-th-fgd-3 flex-shrink invisible w-5`}>X</div>
<ElementTitle noMarignBottom>Deposit Funds</ElementTitle>
2021-04-09 17:01:00 -07:00
</Modal.Header>
<div className={`pb-6 px-8`}>
2021-04-18 03:34:37 -07:00
<AccountSelect
accounts={depositAccounts}
selectedAccount={selectedAccount}
onSelectAccount={handleAccountSelect}
2021-04-18 03:34:37 -07:00
/>
<div className="flex justify-between pb-2 pt-4">
<div className={`text-th-fgd-1`}>Amount</div>
<div
className="text-th-fgd-1 underline cursor-pointer default-transition hover:text-th-primary hover:no-underline"
onClick={setMaxForSelectedAccount}
>
Max
2021-04-09 17:01:00 -07:00
</div>
</div>
2021-04-18 03:34:37 -07:00
<div className="flex items-center">
<Input
type="number"
min="0"
className={`border border-th-fgd-4 flex-grow pr-11`}
2021-04-18 03:34:37 -07:00
placeholder="0.00"
value={inputAmount}
onChange={(e) => setInputAmount(e.target.value)}
suffix={getSymbolForTokenMintAddress(
selectedAccount?.account?.mint.toString()
)}
2021-04-18 03:34:37 -07:00
/>
</div>
<div className={`mt-5 flex justify-center`}>
<Button onClick={handleDeposit} className="w-full">
<div className={`flex items-center justify-center`}>
2021-04-09 17:01:00 -07:00
{submitting && <Loading />}
{`Deposit ${
inputAmount ? inputAmount : ''
} ${getSymbolForTokenMintAddress(
selectedAccount?.account?.mint.toString()
)}
`}
2021-04-09 17:01:00 -07:00
</div>
</Button>
</div>
</div>
</Modal>
)
}
export default React.memo(DepositModal)