anymal-protocol 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anymal-protocol",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
package/src/index.ts CHANGED
@@ -1 +1,6 @@
1
1
  export * from "./utils/useVerifyAccount";
2
+ export * from "./utils/anymals/useMintAnymalNFT";
3
+ export * from "./utils/anymals/useAddAnymalToDatabase";
4
+ export * from "./utils/anymals/useDeleteAnymalFromDatabase";
5
+ export * from "./utils/anymals/useSaveAnymalMetadata";
6
+ export * from "./types/Anymal";
@@ -0,0 +1,8 @@
1
+ export interface CreateAnymalInputData {}
2
+
3
+ export interface AnymalNftMetadataInputData {
4
+ name: string;
5
+ description: string;
6
+ image: string;
7
+ nft_id: string;
8
+ }
@@ -0,0 +1,82 @@
1
+ import { useCallback } from "react";
2
+ import { CreateAnymalInputData } from "../../types/Anymal";
3
+
4
+ export function useAddAnymalToDatabase() {
5
+ return useCallback(
6
+ async (
7
+ dbAuthToken: string,
8
+ endpoint: string,
9
+ anymalData: CreateAnymalInputData
10
+ ): Promise<{
11
+ _docID: string | null;
12
+ success: boolean;
13
+ message: string;
14
+ }> => {
15
+ if (!dbAuthToken) {
16
+ return {
17
+ success: false,
18
+ message: "Authentication token is missing.",
19
+ _docID: null,
20
+ };
21
+ }
22
+
23
+ let anymalDocID: string | null = null;
24
+
25
+ try {
26
+ const mutation = `
27
+ mutation Create_Anymal($input: [AnymalMutationInputArg!]) {
28
+ create_Anymal(input: $input) {
29
+ _docID
30
+ name
31
+ passportID
32
+ }
33
+ }
34
+ `;
35
+
36
+ const response = await fetch(endpoint, {
37
+ method: "POST",
38
+ headers: {
39
+ "Content-Type": "application/json",
40
+ Authorization: `Bearer ${dbAuthToken}`,
41
+ },
42
+ body: JSON.stringify({
43
+ query: mutation,
44
+ variables: { input: [anymalData] },
45
+ }),
46
+ });
47
+
48
+ if (!response.ok) {
49
+ throw new Error(
50
+ `Failed to create pet. HTTP status ${response.status}`
51
+ );
52
+ }
53
+
54
+ const { data, errors } = await response.json();
55
+
56
+ if (errors) {
57
+ const errorMessage = JSON.stringify(errors);
58
+ if (errorMessage.includes("given ID already exists")) {
59
+ console.warn("Duplicate document ID detected.");
60
+ anymalDocID = data?.create_Anymal[0]?._docID || null; // Capture the petDocID if possible
61
+ throw new Error("Document with the given ID already exists.");
62
+ }
63
+ throw new Error(`GraphQL error: ${errorMessage}`);
64
+ }
65
+
66
+ anymalDocID = data.create_Anymal[0]._docID;
67
+ return {
68
+ success: true,
69
+ _docID: anymalDocID,
70
+ message: "Anymal added to account",
71
+ };
72
+ } catch (error) {
73
+ return {
74
+ success: false,
75
+ _docID: null,
76
+ message: "Error adding anymal to account",
77
+ };
78
+ }
79
+ },
80
+ []
81
+ );
82
+ }
@@ -0,0 +1,47 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useDeleteAnymalFromDatabase() {
4
+ return useCallback(
5
+ async (
6
+ dbAuthToken: string,
7
+ endpoint: string,
8
+ anymalDocID: string
9
+ ): Promise<void> => {
10
+ if (!dbAuthToken || !endpoint || !anymalDocID) return;
11
+
12
+ try {
13
+ const mutation = `
14
+ mutation Delete_Anymal($docId: [ID]) {
15
+ delete_Anymal(docID: $docId) {
16
+ _docID
17
+ }
18
+ }
19
+ `;
20
+
21
+ const response = await fetch(endpoint, {
22
+ method: "POST",
23
+ headers: {
24
+ "Content-Type": "application/json",
25
+ Authorization: `Bearer ${dbAuthToken}`,
26
+ },
27
+ body: JSON.stringify({
28
+ query: mutation,
29
+ variables: { docId: [anymalDocID] },
30
+ }),
31
+ });
32
+
33
+ if (!response.ok)
34
+ throw new Error(
35
+ `Failed to delete anymal. HTTP status ${response.status}`
36
+ );
37
+
38
+ const { errors } = await response.json();
39
+ if (errors) throw new Error(`GraphQL error: ${JSON.stringify(errors)}`);
40
+ } catch (error) {
41
+ console.error("Error deleting anymal from database:", error);
42
+ throw error;
43
+ }
44
+ },
45
+ []
46
+ );
47
+ }
@@ -0,0 +1,63 @@
1
+ import { encodeFunctionData, parseGwei } from "viem";
2
+ import { PET_NFT_ABI } from "../../helpers/BlockchainAbiHelper";
3
+ import { useCallback } from "react";
4
+
5
+ export function useMintAnymalNFT() {
6
+ return useCallback(
7
+ async (
8
+ pid: string,
9
+ nftId: string,
10
+ dbAuthToken: string,
11
+ validationContractAddress: string,
12
+ smartAccount: any,
13
+ bundlerClient: any
14
+ ): Promise<{
15
+ success: boolean;
16
+ message: string;
17
+ }> => {
18
+ if (
19
+ !dbAuthToken ||
20
+ !nftId ||
21
+ !bundlerClient ||
22
+ !smartAccount ||
23
+ !pid ||
24
+ !validationContractAddress
25
+ ) {
26
+ return {
27
+ success: false,
28
+ message: "Missing authentication token OR NFT ID.",
29
+ };
30
+ }
31
+
32
+ const callData = encodeFunctionData({
33
+ abi: PET_NFT_ABI,
34
+ functionName: "submitMetadataByCampaignName",
35
+ args: [
36
+ pid,
37
+ nftId,
38
+ `https://dev-nft.petastic.com/metadata/${nftId}`,
39
+ "petastic-signup-campaign-1",
40
+ ],
41
+ });
42
+
43
+ const userOpHash = await bundlerClient.sendUserOperation({
44
+ account: smartAccount,
45
+ calls: [
46
+ {
47
+ to: validationContractAddress,
48
+ data: callData,
49
+ },
50
+ ],
51
+ maxPriorityFeePerGas: parseGwei("0.001"),
52
+ maxFeePerGas: parseGwei("0.001"),
53
+ });
54
+
55
+ await bundlerClient.waitForUserOperationReceipt({
56
+ hash: userOpHash,
57
+ });
58
+
59
+ return { success: true, message: "Pet Passport Created!" };
60
+ },
61
+ []
62
+ );
63
+ }
@@ -0,0 +1,37 @@
1
+ import { useCallback } from "react";
2
+ import { AnymalNftMetadataInputData } from "../../types/Anymal";
3
+
4
+ export function useSaveAnymalMetadata() {
5
+ return useCallback(
6
+ async (
7
+ idToken: string,
8
+ publicKey: string,
9
+ nftMetadataInput: AnymalNftMetadataInputData,
10
+ authServiceBaseUrl: string
11
+ ) => {
12
+ const response = await fetch(`${authServiceBaseUrl}/process-nft`, {
13
+ method: "POST",
14
+ headers: {
15
+ "Content-Type": "application/json",
16
+ Authorization: "Bearer " + idToken,
17
+ },
18
+ body: JSON.stringify({
19
+ appPubKey: publicKey,
20
+ nftMetadataInput: nftMetadataInput,
21
+ }),
22
+ });
23
+
24
+ const data = await response.json();
25
+
26
+ if (data.errors) {
27
+ throw new Error(`GraphQL error: ${JSON.stringify(data.errors)}`);
28
+ }
29
+
30
+ return {
31
+ success: true,
32
+ message: data[0],
33
+ };
34
+ },
35
+ []
36
+ );
37
+ }
@@ -0,0 +1,59 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useUpdateAnymalWithNFT() {
4
+ return useCallback(
5
+ async (
6
+ anymalPassportId: string,
7
+ anymalDocId: string,
8
+ dbAuthToken: string,
9
+ endpoint: string
10
+ ): Promise<{ success: boolean }> => {
11
+ if (!dbAuthToken || !anymalPassportId || !anymalDocId || !endpoint) {
12
+ return {
13
+ success: false,
14
+ };
15
+ }
16
+
17
+ try {
18
+ const mutation = `
19
+ mutation {
20
+ update_Anymal(
21
+ docID: [${JSON.stringify(anymalDocId)}],
22
+ input: {
23
+ passportID: "${anymalPassportId}"
24
+ }
25
+ ) {
26
+ passportID
27
+ }
28
+ }
29
+ `;
30
+
31
+ const response = await fetch(endpoint, {
32
+ method: "POST",
33
+ headers: {
34
+ "Content-Type": "application/json",
35
+ Authorization: `Bearer ${dbAuthToken}`,
36
+ },
37
+ body: JSON.stringify({ query: mutation }),
38
+ });
39
+
40
+ if (!response.ok) {
41
+ return { success: false };
42
+ }
43
+
44
+ const { errors } = await response.json();
45
+
46
+ if (errors) {
47
+ console.log(`GQL error: ${JSON.stringify(errors)}`);
48
+ return { success: false };
49
+ }
50
+
51
+ return { success: true };
52
+ } catch (error) {
53
+ console.error("Error updating Anymal with NFT ID:", error);
54
+ return { success: false };
55
+ }
56
+ },
57
+ []
58
+ );
59
+ }