import { ModalProps } from '../../types/modal' import Modal from '../shared/Modal' import mangoStore from '@store/mangoStore' import { notify } from '../../utils/notifications' import Button, { LinkButton } from '../shared/Button' import { useTranslation } from 'next-i18next' import { ChangeEvent, useState } from 'react' import Input from '../forms/Input' import Label from '../forms/Label' import { PublicKey } from '@solana/web3.js' import useMangoAccount from 'hooks/useMangoAccount' import { abbreviateAddress } from 'utils/formatting' import InlineNotification from '@components/shared/InlineNotification' const DEFAULT_DELEGATE = '11111111111111111111111111111111' const DelegateModal = ({ isOpen, onClose }: ModalProps) => { const { t } = useTranslation('common') const { mangoAccount } = useMangoAccount() const [delegateAddress, setDelegateAddress] = useState( mangoAccount?.delegate?.toString() !== DEFAULT_DELEGATE ? mangoAccount!.delegate.toString() : '' ) const handleDelegateAccount = async (address: string) => { const client = mangoStore.getState().client const group = mangoStore.getState().group const actions = mangoStore.getState().actions if (!mangoAccount || !group) return if (address && address !== '' && !PublicKey.isOnCurve(address)) { notify({ type: 'error', title: 'Invalid delegate address', description: 'Check the public key of your delegate wallet is correct', }) } try { const tx = await client.editMangoAccount( group, mangoAccount, undefined, delegateAddress ? new PublicKey(address) : undefined ) onClose() notify({ title: address !== DEFAULT_DELEGATE ? `Account delegated to ${abbreviateAddress( new PublicKey(address) )}` : 'Account delegation removed', type: 'success', txid: tx, }) await actions.reloadMangoAccount() } catch (e: any) { notify({ title: t('account-update-failed'), txid: e?.txid, type: 'error', }) console.error(e) } } return (

{t('delegate-account')}

{t('delegate-desc')}

{mangoAccount && mangoAccount.delegate.toString() !== DEFAULT_DELEGATE ? (
) : null}
{mangoAccount?.delegate.toString() !== DEFAULT_DELEGATE ? ( handleDelegateAccount(DEFAULT_DELEGATE)} > {t('remove-delegate')} ) : null}
) } export default DelegateModal