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])
|
|
|
|
|
2021-04-18 06:45:11 -07:00
|
|
|
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)
|
|
|
|
)
|
2021-04-15 08:57:00 -07:00
|
|
|
.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,
|
2021-04-25 10:23:06 -07:00
|
|
|
new PublicKey(programId),
|
2021-04-09 17:01:00 -07:00
|
|
|
mangoGroup,
|
|
|
|
marginAccount,
|
|
|
|
wallet,
|
|
|
|
selectedAccount.account.mint,
|
|
|
|
selectedAccount.publicKey,
|
|
|
|
Number(inputAmount)
|
|
|
|
)
|
2021-04-15 08:57:00 -07:00
|
|
|
.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>
|
2021-04-18 06:45:11 -07:00
|
|
|
<ElementTitle noMarignBottom>Deposit Funds</ElementTitle>
|
2021-04-09 17:01:00 -07:00
|
|
|
</Modal.Header>
|
2021-04-12 09:49:02 -07:00
|
|
|
<div className={`pb-6 px-8`}>
|
2021-04-18 03:34:37 -07:00
|
|
|
<AccountSelect
|
|
|
|
accounts={depositAccounts}
|
|
|
|
selectedAccount={selectedAccount}
|
2021-04-18 06:45:11 -07:00
|
|
|
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"
|
2021-04-18 06:45:11 -07:00
|
|
|
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)}
|
2021-04-18 06:45:11 -07:00
|
|
|
suffix={getSymbolForTokenMintAddress(
|
|
|
|
selectedAccount?.account?.mint.toString()
|
|
|
|
)}
|
2021-04-18 03:34:37 -07:00
|
|
|
/>
|
|
|
|
</div>
|
2021-04-18 06:45:11 -07:00
|
|
|
<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 />}
|
2021-04-18 06:45:11 -07:00
|
|
|
{`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)
|