|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import * as z from 'zod'; |
| 4 | +import { |
| 5 | + BytesValue, |
| 6 | + TypedValue, |
| 7 | + ContractCallPayloadBuilder, |
| 8 | + ContractFunction, |
| 9 | + BigUIntValue, |
| 10 | + AddressValue, |
| 11 | + Address, |
| 12 | +} from '@multiversx/sdk-core'; |
| 13 | +import { useForm } from 'react-hook-form'; |
| 14 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 15 | +import { Form } from '@/components/ui/form'; |
| 16 | +import { OperationsInputField } from '@/components/operations/operations-input-field'; |
| 17 | +import { OperationsSubmitButton } from '@/components/operations/operations-submit-button'; |
| 18 | +import { CommonOpertationContentProps } from '@/components/operations/operations-common-types'; |
| 19 | +import { OperationsRadioGroup } from '@/components/operations/operations-radio-group'; |
| 20 | +import BigNumber from 'bignumber.js'; |
| 21 | +import { useConfig, useTransaction } from '@useelven/core'; |
| 22 | +import axios from 'axios'; |
| 23 | +import { |
| 24 | + builtInSC, |
| 25 | + commonOpertationsGasLimit, |
| 26 | +} from '@/components/operations/constants'; |
| 27 | +import { useTxStatus } from '@/hooks/use-tx-status'; |
| 28 | +import { OperationInfoBox } from '@/components/operation-info-box'; |
| 29 | + |
| 30 | +const formSchema = z.object({ |
| 31 | + tokenId: z.string().min(1, 'The field is required'), |
| 32 | + type: z.enum(['freeze', 'unfreeze'], { |
| 33 | + required_error: 'Please choose the type of the operation (freeze/unfreeze)', |
| 34 | + }), |
| 35 | + accountAddressToFreeze: z.string().min(1, 'The field is required'), |
| 36 | +}); |
| 37 | + |
| 38 | +export const FreezeUnfreezeSingle = ({ |
| 39 | + tokenType, |
| 40 | +}: { |
| 41 | + tokenType: CommonOpertationContentProps['tokenType']; |
| 42 | +}) => { |
| 43 | + const { triggerTx, error, txResult, transaction, pending } = useTransaction(); |
| 44 | + const { apiAddress } = useConfig(); |
| 45 | + |
| 46 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 47 | + resolver: zodResolver(formSchema), |
| 48 | + defaultValues: { |
| 49 | + tokenId: '', |
| 50 | + type: 'freeze', |
| 51 | + accountAddressToFreeze: '', |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + useTxStatus({ |
| 56 | + successHash: txResult?.hash, |
| 57 | + pendingHash: transaction?.getHash()?.toString(), |
| 58 | + error, |
| 59 | + pending, |
| 60 | + }); |
| 61 | + |
| 62 | + const onSubmit = async ({ |
| 63 | + tokenId, |
| 64 | + type, |
| 65 | + accountAddressToFreeze, |
| 66 | + }: z.infer<typeof formSchema>) => { |
| 67 | + try { |
| 68 | + // TODO: replace with useElven useApiCall when ready to handle such cases |
| 69 | + const tokenOnNetwork = await axios.get<{ |
| 70 | + nonce: number; |
| 71 | + collection: string; |
| 72 | + }>(`${apiAddress}/nfts/${tokenId.trim()}`, { |
| 73 | + headers: { |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + Accept: 'application/json', |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const nonce = tokenOnNetwork?.data?.nonce; |
| 80 | + const collectionId = tokenOnNetwork?.data?.collection; |
| 81 | + |
| 82 | + // TODO: show the error in the transaction status modal |
| 83 | + if (!nonce || !collectionId) { |
| 84 | + console.error( |
| 85 | + "Can't read the nonce or/and collection id of the token, using MultiversX API!" |
| 86 | + ); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const args: TypedValue[] = [ |
| 91 | + BytesValue.fromUTF8(collectionId.trim()), |
| 92 | + new BigUIntValue(new BigNumber(nonce)), |
| 93 | + new AddressValue(new Address(accountAddressToFreeze.trim())), |
| 94 | + ]; |
| 95 | + |
| 96 | + // TODO: replace ContractCallPayloadBuilder |
| 97 | + const data = new ContractCallPayloadBuilder() |
| 98 | + .setFunction( |
| 99 | + new ContractFunction( |
| 100 | + type === 'freeze' ? 'freezeSingleNFT' : 'unFreezeSingleNFT' |
| 101 | + ) |
| 102 | + ) |
| 103 | + .setArgs(args) |
| 104 | + .build(); |
| 105 | + |
| 106 | + triggerTx?.({ |
| 107 | + address: builtInSC, |
| 108 | + gasLimit: commonOpertationsGasLimit, |
| 109 | + data, |
| 110 | + value: 0, |
| 111 | + }); |
| 112 | + |
| 113 | + form.reset(); |
| 114 | + } catch (e) { |
| 115 | + console.error( |
| 116 | + "Can't read the nonce or/and collection id of the token, using MultiversX API!", |
| 117 | + e |
| 118 | + ); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + return ( |
| 123 | + <> |
| 124 | + <OperationInfoBox error={error} txHash={txResult?.hash} /> |
| 125 | + <Form {...form}> |
| 126 | + <form |
| 127 | + id="freeze-unfreeze-single-form" |
| 128 | + onSubmit={form.handleSubmit(onSubmit)} |
| 129 | + className="space-y-8" |
| 130 | + > |
| 131 | + <div className="flex-1 overflow-auto p-1"> |
| 132 | + <OperationsRadioGroup |
| 133 | + items={[ |
| 134 | + { name: 'freeze', description: 'Freeze an address' }, |
| 135 | + { name: 'unfreeze', description: 'Unfreeze an address' }, |
| 136 | + ]} |
| 137 | + name="type" |
| 138 | + label="Operation type" |
| 139 | + description="Please choose the type of the operation. Freeze or Unfreeze." |
| 140 | + /> |
| 141 | + <OperationsInputField |
| 142 | + name="tokenId" |
| 143 | + label="Token id" |
| 144 | + placeholder="Example: MyToken-23432-01" |
| 145 | + description="Please provide your token id" |
| 146 | + /> |
| 147 | + <OperationsInputField |
| 148 | + name="accountAddressToFreeze" |
| 149 | + label="Account" |
| 150 | + placeholder="Example: erd1..." |
| 151 | + description={`Please provide the account that holds the ${tokenType} ESDT to freeze/unfreeze.`} |
| 152 | + /> |
| 153 | + </div> |
| 154 | + <OperationsSubmitButton formId="freeze-unfreeze-single-form" /> |
| 155 | + </form> |
| 156 | + </Form> |
| 157 | + </> |
| 158 | + ); |
| 159 | +}; |
0 commit comments