mango-ui-v3/components/DepositMsrmModal.tsx

74 lines
2.3 KiB
TypeScript
Raw Normal View History

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'
import { useTranslation } from 'next-i18next'
2021-10-18 13:38:03 -07:00
const DepositMsrmModal = ({ onClose, isOpen }) => {
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 wallet = useMangoStore((s) => s.wallet.current)
const walletTokens = useMangoStore((s) => s.wallet.tokens)
const cluster = useMangoStore.getState().connection.cluster
const handleMsrmDeposit = async () => {
setSubmitting(true)
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.depositMsrm(
mangoGroup,
mangoAccount,
wallet,
2021-10-18 14:21:37 -07:00
ownerMsrmAccount?.account?.publicKey,
2021-10-18 13:38:03 -07:00
1
)
notify({
title: t('msrm-deposited'),
2021-10-18 13:38:03 -07:00
txid,
})
} catch (e) {
console.log('error:', e)
notify({
type: 'error',
title: t('msrm-deposit-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}>
<div className="flex justify-center text-lg text-th-fgd-1">
{t('deposit')}
</div>
<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>
<div className="mt-6 flex items-center justify-center">
2021-10-18 13:38:03 -07:00
<Button onClick={handleMsrmDeposit}>
{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}>
{t('cancel')}
2021-10-18 13:38:03 -07:00
</LinkButton>
</div>
</Modal>
)
}
export default DepositMsrmModal