2021-10-18 13:38:03 -07:00
|
|
|
import { useState } from 'react'
|
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
|
|
|
import Button, { LinkButton } from './Button'
|
|
|
|
import { notify } from '../utils/notifications'
|
|
|
|
import Loading from './Loading'
|
|
|
|
import Modal from './Modal'
|
|
|
|
import { msrmMints } from '@blockworks-foundation/mango-client'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-03-23 03:42:04 -07:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
2021-10-18 13:38:03 -07:00
|
|
|
|
|
|
|
const WithdrawMsrmModal = ({ onClose, isOpen }) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-10-18 13:38:03 -07:00
|
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const actions = useMangoStore((s) => s.actions)
|
|
|
|
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
|
|
|
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
|
|
|
const walletTokens = useMangoStore((s) => s.wallet.tokens)
|
2022-03-23 03:42:04 -07:00
|
|
|
const { wallet } = useWallet()
|
2021-10-18 13:38:03 -07:00
|
|
|
const cluster = useMangoStore.getState().connection.cluster
|
|
|
|
|
|
|
|
const handleMsrmWithdraw = async () => {
|
2022-03-28 08:13:54 -07:00
|
|
|
if (!mangoGroup || !mangoAccount || !wallet) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-18 13:38:03 -07:00
|
|
|
setSubmitting(true)
|
2022-03-13 16:26:30 -07:00
|
|
|
const mangoClient = useMangoStore.getState().connection.client
|
2021-10-18 13:38:03 -07:00
|
|
|
const ownerMsrmAccount = walletTokens.find((t) =>
|
|
|
|
t.account.mint.equals(msrmMints[cluster])
|
|
|
|
)
|
|
|
|
try {
|
|
|
|
const txid = await mangoClient.withdrawMsrm(
|
|
|
|
mangoGroup,
|
|
|
|
mangoAccount,
|
2022-03-23 03:42:04 -07:00
|
|
|
wallet?.adapter,
|
2021-10-18 13:38:03 -07:00
|
|
|
ownerMsrmAccount.account.publicKey,
|
|
|
|
1
|
|
|
|
)
|
|
|
|
notify({
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('msrm-withdrawal'),
|
2021-10-18 13:38:03 -07:00
|
|
|
txid,
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.log('error:', e)
|
|
|
|
notify({
|
|
|
|
type: 'error',
|
2021-10-20 05:42:40 -07:00
|
|
|
title: t('msrm-withdraw-error'),
|
2021-10-18 13:38:03 -07:00
|
|
|
description: e.message,
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
setSubmitting(false)
|
|
|
|
actions.fetchMangoGroup()
|
|
|
|
actions.reloadMangoAccount()
|
|
|
|
onClose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal onClose={onClose} isOpen={isOpen}>
|
2022-03-09 12:53:11 -08:00
|
|
|
<div className="flex justify-center text-lg text-th-fgd-1">
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('withdraw')}
|
|
|
|
</div>
|
2022-03-09 12:53:11 -08:00
|
|
|
<div className="mt-4 border border-th-bkg-3 bg-th-bkg-1 p-6 text-center text-lg text-th-fgd-1">
|
2021-10-18 13:38:03 -07:00
|
|
|
1 MSRM
|
|
|
|
</div>
|
2022-03-09 12:53:11 -08:00
|
|
|
<div className="mt-6 flex items-center justify-center">
|
2021-10-18 13:38:03 -07:00
|
|
|
<Button onClick={handleMsrmWithdraw}>
|
2021-10-20 05:42:40 -07:00
|
|
|
{submitting ? <Loading /> : <span>{t('confirm')}</span>}
|
2021-10-18 13:38:03 -07:00
|
|
|
</Button>
|
|
|
|
<LinkButton className="ml-4 text-th-fgd-1" onClick={onClose}>
|
2021-10-20 05:42:40 -07:00
|
|
|
{t('cancel')}
|
2021-10-18 13:38:03 -07:00
|
|
|
</LinkButton>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WithdrawMsrmModal
|