|
| 1 | +import React, { useContext, useEffect, useState } from 'react' |
| 2 | +import * as yup from 'yup' |
| 3 | +import useWalletStore from 'stores/useWalletStore' |
| 4 | +import { |
| 5 | + Governance, |
| 6 | + ProgramAccount, |
| 7 | + serializeInstructionToBase64, |
| 8 | +} from '@solana/spl-governance' |
| 9 | +import { PublicKey } from '@solana/web3.js' |
| 10 | +import { validateInstruction } from '@utils/instructionTools' |
| 11 | +import { |
| 12 | + DomainNameTransferForm, |
| 13 | + UiInstruction, |
| 14 | +} from '@utils/uiTypes/proposalCreationTypes' |
| 15 | +import { transferInstruction, NAME_PROGRAM_ID } from '@bonfida/spl-name-service' |
| 16 | +import { NewProposalContext } from '../../new' |
| 17 | +import GovernedAccountSelect from '../GovernedAccountSelect' |
| 18 | + |
| 19 | +import { LoadingDots } from '@components/Loading' |
| 20 | +import useGovernanceAssets from '@hooks/useGovernanceAssets' |
| 21 | +import Select from '@components/inputs/Select' |
| 22 | +import Input from '@components/inputs/Input' |
| 23 | +import { useDomainsForAccount } from '@hooks/useDomainNames' |
| 24 | +import { isPublicKey } from '@tools/core/pubkey' |
| 25 | + |
| 26 | +const TransferDomainName = ({ |
| 27 | + index, |
| 28 | + governance, |
| 29 | +}: { |
| 30 | + index: number |
| 31 | + governance: ProgramAccount<Governance> | null |
| 32 | +}) => { |
| 33 | + const connection = useWalletStore((s) => s.connection.current) |
| 34 | + const shouldBeGoverned = index !== 0 && governance |
| 35 | + const { handleSetInstructions } = useContext(NewProposalContext) |
| 36 | + |
| 37 | + const { assetAccounts } = useGovernanceAssets() |
| 38 | + const governedAccount = assetAccounts.filter((acc) => acc.isSol)[0] |
| 39 | + const { accountDomains, isLoading } = useDomainsForAccount( |
| 40 | + connection, |
| 41 | + governedAccount |
| 42 | + ) |
| 43 | + |
| 44 | + const [formErrors, setFormErrors] = useState({}) |
| 45 | + const [form, setForm] = useState<DomainNameTransferForm>({ |
| 46 | + destinationAccount: '', |
| 47 | + governedAccount: undefined, |
| 48 | + domainAddress: undefined, |
| 49 | + }) |
| 50 | + |
| 51 | + const handleSetForm = ({ propertyName, value }) => { |
| 52 | + setFormErrors({}) |
| 53 | + setForm({ ...form, [propertyName]: value }) |
| 54 | + } |
| 55 | + |
| 56 | + async function getInstruction(): Promise<UiInstruction> { |
| 57 | + const isValid = await validateInstruction({ |
| 58 | + schema, |
| 59 | + form, |
| 60 | + setFormErrors, |
| 61 | + }) |
| 62 | + |
| 63 | + const obj: UiInstruction = { |
| 64 | + serializedInstruction: '', |
| 65 | + isValid, |
| 66 | + governance: governedAccount?.governance, |
| 67 | + } |
| 68 | + |
| 69 | + if ( |
| 70 | + isValid && |
| 71 | + form.destinationAccount && |
| 72 | + form.domainAddress && |
| 73 | + form.governedAccount |
| 74 | + ) { |
| 75 | + const nameProgramId = new PublicKey(NAME_PROGRAM_ID) |
| 76 | + const nameAccountKey = new PublicKey(form.domainAddress) |
| 77 | + const newOwnerKey = new PublicKey(form.destinationAccount) |
| 78 | + const nameOwner = governedAccount.pubkey |
| 79 | + |
| 80 | + const transferIx = transferInstruction( |
| 81 | + nameProgramId, |
| 82 | + nameAccountKey, |
| 83 | + newOwnerKey, |
| 84 | + nameOwner |
| 85 | + ) |
| 86 | + |
| 87 | + obj.serializedInstruction = serializeInstructionToBase64(transferIx) |
| 88 | + } |
| 89 | + return obj |
| 90 | + } |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + handleSetInstructions( |
| 94 | + { governedAccount: governedAccount?.governance, getInstruction }, |
| 95 | + index |
| 96 | + ) |
| 97 | + }, [form]) |
| 98 | + |
| 99 | + const schema = yup.object().shape({ |
| 100 | + governedAccount: yup |
| 101 | + .object() |
| 102 | + .nullable() |
| 103 | + .required('Governed account is required'), |
| 104 | + destinationAccount: yup |
| 105 | + .string() |
| 106 | + .required('Please provide a valid destination account') |
| 107 | + .test({ |
| 108 | + name: 'is-valid-account', |
| 109 | + test(val, ctx) { |
| 110 | + if (!val || !isPublicKey(val)) { |
| 111 | + return ctx.createError({ |
| 112 | + message: 'Please verify the account address', |
| 113 | + }) |
| 114 | + } |
| 115 | + return true |
| 116 | + }, |
| 117 | + }), |
| 118 | + domainAddress: yup.string().required('Please select a domain name'), |
| 119 | + }) |
| 120 | + |
| 121 | + return ( |
| 122 | + <> |
| 123 | + <GovernedAccountSelect |
| 124 | + label="Governance" |
| 125 | + governedAccounts={assetAccounts.filter((acc) => acc.isSol)} |
| 126 | + onChange={(value) => { |
| 127 | + handleSetForm({ value, propertyName: 'governedAccount' }) |
| 128 | + }} |
| 129 | + value={governedAccount} |
| 130 | + error={formErrors['governedAccount']} |
| 131 | + shouldBeGoverned={shouldBeGoverned} |
| 132 | + governance={governance} |
| 133 | + /> |
| 134 | + <Input |
| 135 | + label="Destination Account" |
| 136 | + value={form.destinationAccount} |
| 137 | + type="text" |
| 138 | + onChange={(element) => |
| 139 | + handleSetForm({ |
| 140 | + propertyName: 'destinationAccount', |
| 141 | + value: element.target.value, |
| 142 | + }) |
| 143 | + } |
| 144 | + error={formErrors['destinationAccount']} |
| 145 | + /> |
| 146 | + {isLoading ? ( |
| 147 | + <div className="mt-5"> |
| 148 | + <div>Looking up accountDomains...</div> |
| 149 | + <LoadingDots /> |
| 150 | + </div> |
| 151 | + ) : ( |
| 152 | + <Select |
| 153 | + className="" |
| 154 | + label="Domain" |
| 155 | + value={ |
| 156 | + form.domainAddress |
| 157 | + ? accountDomains.find( |
| 158 | + (d) => d.domainAddress === form.domainAddress |
| 159 | + )?.domainName + '.sol' |
| 160 | + : '' |
| 161 | + } |
| 162 | + placeholder="Please select..." |
| 163 | + error={formErrors['domainAddress']} |
| 164 | + onChange={(value) => { |
| 165 | + handleSetForm({ |
| 166 | + value: accountDomains.find((d) => d.domainName === value) |
| 167 | + ?.domainAddress, |
| 168 | + propertyName: 'domainAddress', |
| 169 | + }) |
| 170 | + }} |
| 171 | + > |
| 172 | + {accountDomains?.map( |
| 173 | + (domain, index) => |
| 174 | + domain.domainAddress && ( |
| 175 | + <Select.Option |
| 176 | + key={domain.domainName! + index} |
| 177 | + value={domain.domainName} |
| 178 | + > |
| 179 | + <div className="text-fgd-1 mb-2">{domain.domainName}.sol</div> |
| 180 | + <div className="">{domain.domainAddress?.toString()}</div> |
| 181 | + </Select.Option> |
| 182 | + ) |
| 183 | + )} |
| 184 | + </Select> |
| 185 | + )} |
| 186 | + </> |
| 187 | + ) |
| 188 | +} |
| 189 | + |
| 190 | +export default TransferDomainName |
0 commit comments