@trustless-work/blocks 0.0.5 → 0.0.7

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.
Files changed (44) hide show
  1. package/README.md +39 -13
  2. package/bin/index.js +1128 -1137
  3. package/package.json +44 -44
  4. package/templates/escrows/details/EscrowDetailDialog.tsx +3 -3
  5. package/templates/escrows/details/GeneralInformation.tsx +2 -2
  6. package/templates/escrows/details/SuccessReleaseDialog.tsx +2 -3
  7. package/templates/escrows/details/useDetailsEscrow.ts +2 -2
  8. package/templates/escrows/escrows-by-role/cards/EscrowsCards.tsx +42 -16
  9. package/templates/escrows/escrows-by-role/table/EscrowsTable.tsx +45 -18
  10. package/templates/escrows/escrows-by-role/useEscrowsByRole.shared.ts +39 -25
  11. package/templates/escrows/escrows-by-signer/cards/EscrowsCards.tsx +33 -16
  12. package/templates/escrows/escrows-by-signer/table/EscrowsTable.tsx +34 -17
  13. package/templates/escrows/escrows-by-signer/useEscrowsBySigner.shared.ts +38 -25
  14. package/templates/escrows/single-release/approve-milestone/button/ApproveMilestone.tsx +14 -1
  15. package/templates/escrows/single-release/approve-milestone/dialog/ApproveMilestone.tsx +1 -1
  16. package/templates/escrows/single-release/approve-milestone/form/ApproveMilestone.tsx +1 -1
  17. package/templates/escrows/single-release/approve-milestone/shared/useApproveMilestone.ts +14 -1
  18. package/templates/escrows/single-release/change-milestone-status/button/ChangeMilestoneStatus.tsx +16 -1
  19. package/templates/escrows/single-release/change-milestone-status/dialog/ChangeMilestoneStatus.tsx +1 -1
  20. package/templates/escrows/single-release/change-milestone-status/form/ChangeMilestoneStatus.tsx +1 -1
  21. package/templates/escrows/single-release/change-milestone-status/shared/useChangeMilestoneStatus.ts +14 -1
  22. package/templates/escrows/single-release/dispute-escrow/button/DisputeEscrow.tsx +13 -1
  23. package/templates/escrows/single-release/fund-escrow/button/FundEscrow.tsx +13 -1
  24. package/templates/escrows/single-release/fund-escrow/shared/useFundEscrow.ts +14 -1
  25. package/templates/escrows/single-release/initialize-escrow/shared/useInitializeEscrow.ts +17 -1
  26. package/templates/escrows/single-release/release-escrow/button/ReleaseEscrow.tsx +15 -3
  27. package/templates/escrows/single-release/resolve-dispute/button/ResolveDispute.tsx +13 -1
  28. package/templates/escrows/single-release/resolve-dispute/dialog/ResolveDispute.tsx +1 -1
  29. package/templates/escrows/single-release/resolve-dispute/shared/useResolveDispute.ts +14 -1
  30. package/templates/escrows/single-release/update-escrow/shared/useUpdateEscrow.ts +224 -211
  31. package/templates/handle-errors/handle.ts +16 -0
  32. package/templates/helpers/format.helper.ts +31 -0
  33. package/templates/helpers/useCopy.ts +5 -0
  34. package/templates/{escrows/escrow-context → providers}/EscrowAmountProvider.tsx +3 -0
  35. package/templates/providers/EscrowDialogsProvider.tsx +61 -0
  36. package/templates/{escrows/escrow-context → providers}/EscrowProvider.tsx +30 -0
  37. package/templates/providers/ReactQueryClientProvider.tsx +17 -1
  38. package/templates/tanstack/useEscrowsByRoleQuery.ts +14 -0
  39. package/templates/tanstack/useEscrowsBySignerQuery.ts +13 -0
  40. package/templates/tanstack/useEscrowsMutations.ts +36 -0
  41. package/templates/wallet-kit/trustlines.ts +7 -0
  42. package/templates/wallet-kit/validators.ts +6 -0
  43. package/templates/wallet-kit/wallet-kit.ts +13 -0
  44. package/templates/escrows/escrow-context/EscrowDialogsProvider.tsx +0 -108
@@ -35,6 +35,9 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
35
35
  );
36
36
  const [userRolesInEscrow, setUserRolesInEscrowState] = useState<string[]>([]);
37
37
 
38
+ /**
39
+ * Get the selected escrow from the local storage
40
+ */
38
41
  useEffect(() => {
39
42
  try {
40
43
  const stored = localStorage.getItem(LOCAL_STORAGE_KEY);
@@ -47,6 +50,11 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
47
50
  }
48
51
  }, []);
49
52
 
53
+ /**
54
+ * Persist the selected escrow to the local storage
55
+ *
56
+ * @param value - The escrow to persist
57
+ */
50
58
  const persist = (value: Escrow | null) => {
51
59
  if (value) {
52
60
  localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(value));
@@ -55,6 +63,11 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
55
63
  }
56
64
  };
57
65
 
66
+ /**
67
+ * Update the selected escrow
68
+ *
69
+ * @param updater - The updater function
70
+ */
58
71
  const updateEscrow: EscrowContextType["updateEscrow"] = (updater) => {
59
72
  setSelectedEscrowState((current) => {
60
73
  if (!current) return current;
@@ -67,6 +80,12 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
67
80
  });
68
81
  };
69
82
 
83
+ /**
84
+ * Set a field of the selected escrow
85
+ *
86
+ * @param key - The key of the field to set
87
+ * @param value - The value to set
88
+ */
70
89
  const setEscrowField: EscrowContextType["setEscrowField"] = (key, value) => {
71
90
  setSelectedEscrowState((current) => {
72
91
  if (!current) return current;
@@ -76,11 +95,19 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
76
95
  });
77
96
  };
78
97
 
98
+ /**
99
+ * Clear the selected escrow
100
+ */
79
101
  const clearEscrow = () => {
80
102
  setSelectedEscrowState(null);
81
103
  persist(null);
82
104
  };
83
105
 
106
+ /**
107
+ * Set the user roles in the escrow
108
+ *
109
+ * @param roles - The roles to set
110
+ */
84
111
  const setUserRolesInEscrow = useCallback((roles: string[]) => {
85
112
  setUserRolesInEscrowState((prev) => {
86
113
  // Avoid unnecessary updates to prevent re-renders
@@ -94,6 +121,9 @@ export const EscrowProvider = ({ children }: { children: ReactNode }) => {
94
121
  });
95
122
  }, []);
96
123
 
124
+ /**
125
+ * Check if the user has an escrow
126
+ */
97
127
  const hasEscrow = useMemo(() => Boolean(selectedEscrow), [selectedEscrow]);
98
128
 
99
129
  return (
@@ -3,6 +3,14 @@
3
3
  import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
4
4
  import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
5
5
 
6
+ /**
7
+ * Query Client, you can configure the default options for the query client
8
+ *
9
+ * - Stale Time: 5 minutes
10
+ * - GC Time: 30 minutes
11
+ * - Retry: 1
12
+ * - Refetch on Window Focus: false
13
+ */
6
14
  const queryClient = new QueryClient({
7
15
  defaultOptions: {
8
16
  queries: {
@@ -14,6 +22,12 @@ const queryClient = new QueryClient({
14
22
  },
15
23
  });
16
24
 
25
+ /**
26
+ * React Query Client Provider
27
+ *
28
+ * @param children - The children
29
+ * @returns The React Query Client Provider
30
+ */
17
31
  export function ReactQueryClientProvider({
18
32
  children,
19
33
  }: {
@@ -22,7 +36,9 @@ export function ReactQueryClientProvider({
22
36
  return (
23
37
  <QueryClientProvider client={queryClient}>
24
38
  {children}
25
- <ReactQueryDevtools initialIsOpen={false} />
39
+ {process.env.NODE_ENV !== "production" ? (
40
+ <ReactQueryDevtools initialIsOpen={false} />
41
+ ) : null}
26
42
  </QueryClientProvider>
27
43
  );
28
44
  }
@@ -14,6 +14,12 @@ type UseEscrowsByRoleQueryParams = Omit<
14
14
  validateOnChain?: boolean;
15
15
  };
16
16
 
17
+ /**
18
+ * Use the query to get the escrows by role
19
+ *
20
+ * @param params - The parameters for the query
21
+ * @returns The query result
22
+ */
17
23
  export const useEscrowsByRoleQuery = ({
18
24
  role,
19
25
  roleAddress,
@@ -32,6 +38,7 @@ export const useEscrowsByRoleQuery = ({
32
38
  enabled = true,
33
39
  validateOnChain = true,
34
40
  }: UseEscrowsByRoleQueryParams) => {
41
+ // Get the escrows by role
35
42
  const { getEscrowsByRole } = useGetEscrowsFromIndexerByRole();
36
43
 
37
44
  return useQuery({
@@ -57,6 +64,13 @@ export const useEscrowsByRoleQuery = ({
57
64
  if (!role) {
58
65
  throw new Error("Role is required to fetch escrows by role");
59
66
  }
67
+
68
+ /**
69
+ * Call the query to get the escrows from the Trustless Work Indexer
70
+ *
71
+ * @param params - The parameters for the query
72
+ * @returns The query result
73
+ */
60
74
  const escrows = await getEscrowsByRole({
61
75
  role,
62
76
  roleAddress,
@@ -11,6 +11,12 @@ interface UseEscrowsBySignerQueryParams
11
11
  validateOnChain?: boolean;
12
12
  }
13
13
 
14
+ /**
15
+ * Use the query to get the escrows by signer
16
+ *
17
+ * @param params - The parameters for the query
18
+ * @returns The query result
19
+ */
14
20
  export const useEscrowsBySignerQuery = ({
15
21
  signer,
16
22
  isActive,
@@ -30,6 +36,7 @@ export const useEscrowsBySignerQuery = ({
30
36
  }: UseEscrowsBySignerQueryParams) => {
31
37
  const { getEscrowsBySigner } = useGetEscrowsFromIndexerBySigner();
32
38
 
39
+ // Get the escrows by signer
33
40
  return useQuery({
34
41
  queryKey: [
35
42
  "escrows",
@@ -49,6 +56,12 @@ export const useEscrowsBySignerQuery = ({
49
56
  validateOnChain,
50
57
  ],
51
58
  queryFn: async (): Promise<Escrow[]> => {
59
+ /**
60
+ * Call the query to get the escrows from the Trustless Work Indexer
61
+ *
62
+ * @param params - The parameters for the query
63
+ * @returns The query result
64
+ */
52
65
  const escrows = await getEscrowsBySigner({
53
66
  signer,
54
67
  isActive,
@@ -26,6 +26,18 @@ import {
26
26
  } from "@trustless-work/escrow";
27
27
  import { signTransaction } from "../wallet-kit/wallet-kit";
28
28
 
29
+ /**
30
+ * Use the mutations to interact with the escrows
31
+ *
32
+ * - Deploy Escrow
33
+ * - Update Escrow
34
+ * - Fund Escrow
35
+ * - Change Milestone Status
36
+ * - Approve Milestone
37
+ * - Start Dispute
38
+ * - Release Funds
39
+ * - Resolve Dispute
40
+ */
29
41
  export const useEscrowsMutations = () => {
30
42
  const queryClient = useQueryClient();
31
43
  const { deployEscrow } = useInitializeEscrow();
@@ -38,6 +50,9 @@ export const useEscrowsMutations = () => {
38
50
  const { releaseFunds } = useReleaseFunds();
39
51
  const { resolveDispute } = useResolveDispute();
40
52
 
53
+ /**
54
+ * Deploy Escrow
55
+ */
41
56
  const deployEscrowMutation = useMutation({
42
57
  mutationFn: async ({
43
58
  payload,
@@ -83,6 +98,9 @@ export const useEscrowsMutations = () => {
83
98
  },
84
99
  });
85
100
 
101
+ /**
102
+ * Update Escrow
103
+ */
86
104
  const updateEscrowMutation = useMutation({
87
105
  mutationFn: async ({
88
106
  payload,
@@ -128,6 +146,9 @@ export const useEscrowsMutations = () => {
128
146
  },
129
147
  });
130
148
 
149
+ /**
150
+ * Fund Escrow
151
+ */
131
152
  const fundEscrowMutation = useMutation({
132
153
  mutationFn: async ({
133
154
  payload,
@@ -174,6 +195,9 @@ export const useEscrowsMutations = () => {
174
195
  },
175
196
  });
176
197
 
198
+ /**
199
+ * Approve Milestone
200
+ */
177
201
  const approveMilestoneMutation = useMutation({
178
202
  mutationFn: async ({
179
203
  payload,
@@ -217,6 +241,9 @@ export const useEscrowsMutations = () => {
217
241
  },
218
242
  });
219
243
 
244
+ /**
245
+ * Change Milestone Status
246
+ */
220
247
  const changeMilestoneStatusMutation = useMutation({
221
248
  mutationFn: async ({
222
249
  payload,
@@ -263,6 +290,9 @@ export const useEscrowsMutations = () => {
263
290
  },
264
291
  });
265
292
 
293
+ /**
294
+ * Start Dispute
295
+ */
266
296
  const startDisputeMutation = useMutation({
267
297
  mutationFn: async ({
268
298
  payload,
@@ -308,6 +338,9 @@ export const useEscrowsMutations = () => {
308
338
  },
309
339
  });
310
340
 
341
+ /**
342
+ * Release Funds
343
+ */
311
344
  const releaseFundsMutation = useMutation({
312
345
  mutationFn: async ({
313
346
  payload,
@@ -353,6 +386,9 @@ export const useEscrowsMutations = () => {
353
386
  },
354
387
  });
355
388
 
389
+ /**
390
+ * Resolve Dispute
391
+ */
356
392
  const resolveDisputeMutation = useMutation({
357
393
  mutationFn: async ({
358
394
  payload,
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Trustlines | Non-Native Tokens from Stellar
3
+ *
4
+ * @description Trustlines are the tokens that are used to pay for the escrow
5
+ * @description The trustlines are filtered by the network
6
+ * @description The trustlines are filtered by the network in the trustlineOptions
7
+ */
1
8
  export const trustlines = [
2
9
  // TESTNET
3
10
  {
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Validator for the wallet address
3
+ *
4
+ * @param wallet - The wallet address
5
+ * @returns True if the wallet address is valid, false otherwise
6
+ */
1
7
  export const isValidWallet = (wallet: string) => {
2
8
  if (wallet.length !== 56 || wallet[0] !== "G") {
3
9
  return false;
@@ -6,6 +6,13 @@ import {
6
6
  FreighterModule,
7
7
  } from "@creit.tech/stellar-wallets-kit";
8
8
 
9
+ /**
10
+ * Stellar Wallet Kit
11
+ *
12
+ * @description The Stellar Wallet Kit is used to connect to the wallet
13
+ * @description The Stellar Wallet Kit is used to sign transactions
14
+ * @description The Stellar Wallet Kit is used to get the wallet address
15
+ */
9
16
  export const kit: StellarWalletsKit = new StellarWalletsKit({
10
17
  network: WalletNetwork.TESTNET,
11
18
  selectedWalletId: FREIGHTER_ID,
@@ -17,6 +24,12 @@ interface SignTransactionParams {
17
24
  address: string;
18
25
  }
19
26
 
27
+ /**
28
+ * Sign Transaction Params
29
+ *
30
+ * @param unsignedTransaction - The unsigned transaction
31
+ * @param address - The address of the wallet
32
+ */
20
33
  export const signTransaction = async ({
21
34
  unsignedTransaction,
22
35
  address,
@@ -1,108 +0,0 @@
1
- "use client";
2
-
3
- import * as React from "react";
4
- import { createContext, useContext, useMemo, useState } from "react";
5
-
6
- export type DialogState = {
7
- isOpen: boolean;
8
- setIsOpen: (open: boolean) => void;
9
- };
10
-
11
- export type DialogStates = {
12
- second: DialogState;
13
- completeMilestone: DialogState;
14
- qr: DialogState;
15
- resolveDispute: DialogState;
16
- editMilestone: DialogState;
17
- editEntities: DialogState;
18
- editBasicProperties: DialogState;
19
- successRelease: DialogState;
20
- successResolveDispute: DialogState;
21
- };
22
-
23
- export type StatusStates = {};
24
-
25
- type EscrowDialogsContextType = DialogStates & StatusStates;
26
-
27
- const EscrowDialogsContext = createContext<
28
- EscrowDialogsContextType | undefined
29
- >(undefined);
30
-
31
- export function EscrowDialogsProvider({
32
- children,
33
- }: {
34
- children: React.ReactNode;
35
- }) {
36
- const [secondOpen, setSecondOpen] = useState(false);
37
- const [completeMilestoneOpen, setCompleteMilestoneOpen] = useState(false);
38
- const [qrOpen, setQrOpen] = useState(false);
39
- const [resolveDisputeOpen, setResolveDisputeOpen] = useState(false);
40
- const [editMilestoneOpen, setEditMilestoneOpen] = useState(false);
41
- const [editEntitiesOpen, setEditEntitiesOpen] = useState(false);
42
- const [editBasicPropertiesOpen, setEditBasicPropertiesOpen] = useState(false);
43
- const [successReleaseOpen, setSuccessReleaseOpen] = useState(false);
44
- const [successResolveDisputeOpen, setSuccessResolveDisputeOpen] =
45
- useState(false);
46
-
47
- const value = useMemo<EscrowDialogsContextType>(
48
- () => ({
49
- second: { isOpen: secondOpen, setIsOpen: setSecondOpen },
50
- completeMilestone: {
51
- isOpen: completeMilestoneOpen,
52
- setIsOpen: setCompleteMilestoneOpen,
53
- },
54
- qr: { isOpen: qrOpen, setIsOpen: setQrOpen },
55
- resolveDispute: {
56
- isOpen: resolveDisputeOpen,
57
- setIsOpen: setResolveDisputeOpen,
58
- },
59
- editMilestone: {
60
- isOpen: editMilestoneOpen,
61
- setIsOpen: setEditMilestoneOpen,
62
- },
63
- editEntities: {
64
- isOpen: editEntitiesOpen,
65
- setIsOpen: setEditEntitiesOpen,
66
- },
67
- editBasicProperties: {
68
- isOpen: editBasicPropertiesOpen,
69
- setIsOpen: setEditBasicPropertiesOpen,
70
- },
71
- successRelease: {
72
- isOpen: successReleaseOpen,
73
- setIsOpen: setSuccessReleaseOpen,
74
- },
75
- successResolveDispute: {
76
- isOpen: successResolveDisputeOpen,
77
- setIsOpen: setSuccessResolveDisputeOpen,
78
- },
79
- }),
80
- [
81
- secondOpen,
82
- completeMilestoneOpen,
83
- qrOpen,
84
- resolveDisputeOpen,
85
- editMilestoneOpen,
86
- editEntitiesOpen,
87
- editBasicPropertiesOpen,
88
- successReleaseOpen,
89
- successResolveDisputeOpen,
90
- ]
91
- );
92
-
93
- return (
94
- <EscrowDialogsContext.Provider value={value}>
95
- {children}
96
- </EscrowDialogsContext.Provider>
97
- );
98
- }
99
-
100
- export function useEscrowDialogs() {
101
- const ctx = useContext(EscrowDialogsContext);
102
- if (!ctx) {
103
- throw new Error(
104
- "useEscrowDialogs must be used within EscrowDialogsProvider"
105
- );
106
- }
107
- return ctx;
108
- }