Skip to content

Commit a59fa1e

Browse files
committed
move semi-fungible operations
1 parent 4fffa88 commit a59fa1e

44 files changed

Lines changed: 1071 additions & 338 deletions

File tree

Some content is hidden

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

app/(operations)/common/freeze-unfreeze-single.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ export const FreezeUnfreezeSingle = ({
109109
data,
110110
value: 0,
111111
});
112-
113-
form.reset();
114112
} catch (e) {
115113
console.error(
116114
"Can't read the nonce or/and collection id of the token, using MultiversX API!",

app/(operations)/common/issue-nft-sft.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ export const IssueNftSft = ({
107107
data,
108108
value: payment,
109109
});
110-
111-
form.reset();
112110
};
113111

114112
return (

app/(operations)/common/stop-creation.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export const StopCreation = ({
6161
data,
6262
value: 0,
6363
});
64-
65-
form.reset();
6664
};
6765

6866
return (

app/(operations)/common/transfer-creation-role.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ export const TransferCreationRole = ({
7676
data,
7777
value: 0,
7878
});
79-
80-
form.reset();
8179
};
8280

8381
return (

app/(operations)/common/transfer-ownership.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ export const TransferOwnership = ({
7070
data,
7171
value: 0,
7272
});
73-
74-
form.reset();
7573
};
7674

7775
return (

app/(operations)/common/wipe-single.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ export const WipeSingle = ({
9696
data,
9797
value: 0,
9898
});
99-
100-
form.reset();
10199
} catch (e) {
102100
console.error(
103101
"Can't read the nonce or/and collection id of the token, using MultiversX API!",

app/(operations)/fungible-tokens/change-properties/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const ChangePropertiesPage: NextPage = () => {
1616
<br />
1717
<br />
1818
<strong>
19-
Due to API caching, changes may not take effect immediately.
19+
Due to API caching, changes may not be visible immediately.
2020
</strong>
2121
</p>
2222
</div>

app/(operations)/fungible-tokens/freeze-toggle/components/freeze-unfreeze.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ export const FreezeUnfreeze = () => {
7171
data,
7272
value: 0,
7373
});
74-
75-
form.reset();
7674
};
7775

7876
return (

app/(operations)/fungible-tokens/issue/components/issue.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ export const Issue = () => {
116116
data,
117117
value: payment,
118118
});
119-
120-
form.reset();
121119
};
122120

123121
return (

0 commit comments

Comments
 (0)