2022-11-16 08:59:34 -08:00
|
|
|
import { ModalProps } from '../../types/modal'
|
|
|
|
import Modal from '../shared/Modal'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import { notify } from '../../utils/notifications'
|
2022-12-15 19:16:05 -08:00
|
|
|
import Button, { LinkButton } from '../shared/Button'
|
2022-11-16 08:59:34 -08:00
|
|
|
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'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2022-12-15 19:16:05 -08:00
|
|
|
import { abbreviateAddress } from 'utils/formatting'
|
|
|
|
import InlineNotification from '@components/shared/InlineNotification'
|
2023-02-27 23:20:11 -08:00
|
|
|
import { isMangoError } from 'types'
|
2023-11-29 17:37:14 -08:00
|
|
|
import Tooltip from '@components/shared/Tooltip'
|
2022-12-15 19:16:05 -08:00
|
|
|
|
2022-12-15 20:04:26 -08:00
|
|
|
export const DEFAULT_DELEGATE = '11111111111111111111111111111111'
|
2022-11-16 08:59:34 -08:00
|
|
|
|
|
|
|
const DelegateModal = ({ isOpen, onClose }: ModalProps) => {
|
|
|
|
const { t } = useTranslation('common')
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-11-16 08:59:34 -08:00
|
|
|
|
|
|
|
const [delegateAddress, setDelegateAddress] = useState(
|
2022-12-15 19:16:05 -08:00
|
|
|
mangoAccount?.delegate?.toString() !== DEFAULT_DELEGATE
|
|
|
|
? mangoAccount!.delegate.toString()
|
2023-07-21 11:47:53 -07:00
|
|
|
: '',
|
2022-11-16 08:59:34 -08:00
|
|
|
)
|
|
|
|
|
2022-12-15 19:16:05 -08:00
|
|
|
const handleDelegateAccount = async (address: string) => {
|
2022-11-16 08:59:34 -08:00
|
|
|
const client = mangoStore.getState().client
|
|
|
|
const group = mangoStore.getState().group
|
|
|
|
const actions = mangoStore.getState().actions
|
|
|
|
if (!mangoAccount || !group) return
|
|
|
|
|
2022-12-15 19:16:05 -08:00
|
|
|
if (address && address !== '' && !PublicKey.isOnCurve(address)) {
|
2022-11-16 08:59:34 -08:00
|
|
|
notify({
|
|
|
|
type: 'error',
|
2022-12-15 19:16:05 -08:00
|
|
|
title: 'Invalid delegate address',
|
|
|
|
description: 'Check the public key of your delegate wallet is correct',
|
2022-11-16 08:59:34 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-08-12 11:40:09 -07:00
|
|
|
const { signature: tx, slot } = await client.editMangoAccount(
|
2022-11-16 08:59:34 -08:00
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
undefined,
|
2023-07-21 11:47:53 -07:00
|
|
|
delegateAddress ? new PublicKey(address) : undefined,
|
2022-11-16 08:59:34 -08:00
|
|
|
)
|
|
|
|
onClose()
|
|
|
|
notify({
|
2022-12-15 19:16:05 -08:00
|
|
|
title:
|
|
|
|
address !== DEFAULT_DELEGATE
|
2022-12-15 20:04:26 -08:00
|
|
|
? t('delegate-account-info', {
|
2023-07-08 04:41:10 -07:00
|
|
|
delegate: abbreviateAddress(new PublicKey(address)),
|
2022-12-15 20:04:26 -08:00
|
|
|
})
|
2022-12-15 19:16:05 -08:00
|
|
|
: 'Account delegation removed',
|
2022-11-16 08:59:34 -08:00
|
|
|
type: 'success',
|
|
|
|
txid: tx,
|
|
|
|
})
|
2023-08-12 11:40:09 -07:00
|
|
|
await actions.reloadMangoAccount(slot)
|
2023-02-27 23:20:11 -08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
if (!isMangoError(e)) return
|
2022-11-16 08:59:34 -08:00
|
|
|
notify({
|
|
|
|
title: t('account-update-failed'),
|
2022-12-02 15:47:08 -08:00
|
|
|
txid: e?.txid,
|
2022-11-16 08:59:34 -08:00
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} onClose={onClose}>
|
2022-12-15 19:16:05 -08:00
|
|
|
<div className="h-80">
|
2022-11-16 08:59:34 -08:00
|
|
|
<div className="flex h-full flex-col justify-between">
|
|
|
|
<div className="pb-4">
|
|
|
|
<h2 className="mb-1">{t('delegate-account')}</h2>
|
2023-11-29 17:37:14 -08:00
|
|
|
<p className="mb-4">
|
|
|
|
{t('delegate-account-desc')}{' '}
|
|
|
|
<div className="inline-block">
|
|
|
|
<Tooltip content={t('delegate-account-tooltip')}>
|
|
|
|
<span className="tooltip-underline">{t('more-info')}</span>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
</p>
|
2022-12-15 19:16:05 -08:00
|
|
|
{mangoAccount &&
|
|
|
|
mangoAccount.delegate.toString() !== DEFAULT_DELEGATE ? (
|
|
|
|
<div className="mb-4">
|
|
|
|
<InlineNotification
|
|
|
|
type="info"
|
|
|
|
desc={`Account is delegated to ${abbreviateAddress(
|
2023-07-21 11:47:53 -07:00
|
|
|
mangoAccount.delegate,
|
2022-12-15 19:16:05 -08:00
|
|
|
)}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2022-11-16 18:18:50 -08:00
|
|
|
<Label text={t('wallet-address')} />
|
2022-11-16 08:59:34 -08:00
|
|
|
<Input
|
|
|
|
type="text"
|
2022-11-18 01:53:45 -08:00
|
|
|
name="address"
|
|
|
|
id="address"
|
2022-11-16 08:59:34 -08:00
|
|
|
value={delegateAddress}
|
|
|
|
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
setDelegateAddress(e.target.value)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-15 19:16:05 -08:00
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<Button
|
|
|
|
className="w-full"
|
|
|
|
onClick={() => handleDelegateAccount(delegateAddress)}
|
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
{mangoAccount?.delegate.toString() !== DEFAULT_DELEGATE
|
|
|
|
? t('update-delegate')
|
|
|
|
: t('delegate')}
|
|
|
|
</Button>
|
|
|
|
{mangoAccount?.delegate.toString() !== DEFAULT_DELEGATE ? (
|
|
|
|
<LinkButton
|
|
|
|
className="mt-4"
|
|
|
|
onClick={() => handleDelegateAccount(DEFAULT_DELEGATE)}
|
|
|
|
>
|
|
|
|
{t('remove-delegate')}
|
|
|
|
</LinkButton>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2022-11-16 08:59:34 -08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DelegateModal
|