UNPKG

@trustless-work/escrow

Version:

<p align="center"> <img src="https://github.com/user-attachments/assets/5b182044-dceb-41f5-acf0-da22dea7c98a" alt="CLR-S (2)"> </p>

416 lines (400 loc) 13.7 kB
// src/hooks/useInitializeEscrow.ts import { useMutation, useQueryClient } from "@tanstack/react-query"; // src/provider.tsx import React, { useContext, useState, useEffect } from "react"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; // src/client.ts import axios from "axios"; var TrustlessWorkClient = class { constructor(baseURL, apiKey) { this.axios = axios.create({ baseURL }); if (apiKey) this.setApiKey(apiKey); } /** * Set the API key for the client * @param apiKey - The API key to set */ setApiKey(apiKey) { this.axios.interceptors.request.clear(); this.axios.interceptors.request.use((config) => { config.headers = config.headers ?? {}; config.headers["Authorization"] = `Bearer ${apiKey}`; return config; }); } /** * Send a transaction * @param data - The data (SendTransactionPayload) to send * @returns The response from the API SendTransactionResponse | InitializeEscrowResponse | UpdateEscrowResponse */ sendTransaction(data) { return this.axios.post("/helper/send-transaction", data).then((r) => r.data); } /** * Initialize an escrow * @param data - The data (InitializeEscrowPayload, this can be a single-release or multi-release) to initialize * @param type - The type of escrow (single-release or multi-release) to initialize * @returns The response from the API EscrowRequestResponse, but you can set as InitializeEscrowResponse */ initializeEscrow(data, type) { return this.axios.post(`/deployer/${type}`, data).then((r) => r.data); } /** * Update an escrow * @param data - The data (UpdateEscrowPayload, this can be a single-release or multi-release) to update * @param type - The type of escrow (single-release or multi-release) to update * @returns The response from the API EscrowRequestResponse, but you can set as UpdateEscrowResponse */ updateEscrow(data, type) { return this.axios.put(`/escrow/${type}/update-escrow`, data).then((r) => r.data); } /** * Change the status of a milestone * @param data - The data (ChangeMilestoneStatusPayload, this can be a single-release or multi-release) to change * @param type - The type of escrow (single-release or multi-release) to change * @returns The response from the API EscrowRequestResponse */ changeMilestoneStatus(data, type) { return this.axios.post( `/escrow/${type}/change-milestone-status`, data ).then((r) => r.data); } /** * Approve a milestone * @param data - The data (ApproveMilestonePayload) to approve * @param type - The type of escrow (single-release or multi-release) to approve * @returns The response from the API EscrowRequestResponse */ approveMilestone(data, type) { return this.axios.post(`/escrow/${type}/approve-milestone`, data).then((r) => r.data); } /** * Fund an escrow * @param data - The data (FundEscrowPayload) to fund * @param type - The type of escrow (single-release or multi-release) to fund * @returns The response from the API EscrowRequestResponse */ fundEscrow(data, type) { return this.axios.post(`/escrow/${type}/fund-escrow`, data).then((r) => r.data); } /** * Release funds from an escrow * @param data - The data (ReleaseFundsPayload) to release * @param type - The type of escrow (single-release or multi-release) to release * @returns The response from the API EscrowRequestResponse */ releaseFunds(data, type) { const endpoint = type === "single-release" ? "release-funds" : "release-milestone-funds"; return this.axios.post(`/escrow/${type}/${endpoint}`, data).then((r) => r.data); } /** * Resolve a dispute * @param data - The data (ResolveDisputePayload) to resolve * @param type - The type of escrow (single-release or multi-release) to resolve * @returns The response from the API EscrowRequestResponse */ resolveDispute(data, type) { const endpoint = type === "single-release" ? "resolve-dispute" : "resolve-milestone-dispute"; return this.axios.post(`/escrow/${type}/${endpoint}`, data).then((r) => r.data); } /** * Start a dispute * @param data - The data (StartDisputePayload) to start * @param type - The type of escrow (single-release or multi-release) to start * @returns The response from the API EscrowRequestResponse */ startDispute(data, type) { const endpoint = type === "single-release" ? "dispute-escrow" : "dispute-milestone"; return this.axios.post(`/escrow/${type}/${endpoint}`, data).then((r) => r.data); } /** * Get an escrow * @param data - The data (GetEscrowParams) to get * @returns The response from the API Escrow */ getEscrow(data) { return this.axios.get("/escrow/get-escrow", { params: data }).then((r) => r.data); } /** * Get multiple balances * @param data - The data (GetBalanceParams) to get * @returns The response from the API GetEscrowBalancesResponse */ getMultipleEscrowBalances(data) { return this.axios.get("/helper/get-multiple-escrow-balance", { params: data }).then((r) => r.data); } }; // src/provider.tsx var TrustlessWorkContext = React.createContext({ client: null }); var TrustlessWorkConfig = ({ baseURL, apiKey, children }) => { const [client, setClient] = useState(null); const [queryClient] = useState(() => new QueryClient()); useEffect(() => { const newClient = new TrustlessWorkClient(baseURL, apiKey); setClient(newClient); }, [baseURL, apiKey]); return /* @__PURE__ */ React.createElement(TrustlessWorkContext.Provider, { value: { client } }, /* @__PURE__ */ React.createElement(QueryClientProvider, { client: queryClient }, children)); }; function useTrustlessWorkClient() { const ctx = useContext(TrustlessWorkContext); if (!ctx.client) { throw new Error( "useTrustlessWorkClient must be inside TrustlessWorkConfig" ); } return ctx.client; } // src/hooks/useInitializeEscrow.ts function useInitializeEscrow() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient(); const mutation = useMutation({ mutationFn: ({ payload, type }) => client.initializeEscrow(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { deployEscrow: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useSendTransaction.ts import { useMutation as useMutation2, useQueryClient as useQueryClient2 } from "@tanstack/react-query"; function useSendTransaction() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient2(); const mutation = useMutation2({ mutationFn: (payload) => client.sendTransaction(payload), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["sendTransaction"] }); } }); return { sendTransaction: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess }; } // src/hooks/useGetEscrow.ts import { useMutation as useMutation3, useQueryClient as useQueryClient3 } from "@tanstack/react-query"; function useGetEscrow() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient3(); const mutation = useMutation3({ mutationFn: (payload) => client.getEscrow(payload), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { getEscrow: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, escrow: mutation.data }; } // src/hooks/useUpdateEscrow.ts import { useMutation as useMutation4, useQueryClient as useQueryClient4 } from "@tanstack/react-query"; function useUpdateEscrow() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient4(); const mutation = useMutation4({ mutationFn: ({ payload, type }) => client.updateEscrow(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { updateEscrow: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useStartDispute.ts import { useMutation as useMutation5, useQueryClient as useQueryClient5 } from "@tanstack/react-query"; function useStartDispute() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient5(); const mutation = useMutation5({ mutationFn: ({ payload, type }) => client.startDispute(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { startDispute: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useResolveDispute.ts import { useMutation as useMutation6, useQueryClient as useQueryClient6 } from "@tanstack/react-query"; function useResolveDispute() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient6(); const mutation = useMutation6({ mutationFn: ({ payload, type }) => client.resolveDispute(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { resolveDispute: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useGetMultipleEscrowBalances.ts import { useMutation as useMutation7, useQueryClient as useQueryClient7 } from "@tanstack/react-query"; function useGetMultipleEscrowBalances() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient7(); const mutation = useMutation7({ mutationFn: (payload) => client.getMultipleEscrowBalances(payload), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { getMultipleBalances: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, balances: mutation.data }; } // src/hooks/useReleaseFunds.ts import { useMutation as useMutation8, useQueryClient as useQueryClient8 } from "@tanstack/react-query"; function useReleaseFunds() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient8(); const mutation = useMutation8({ mutationFn: ({ payload, type }) => client.releaseFunds(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { releaseFunds: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useFundEscrow.ts import { useMutation as useMutation9, useQueryClient as useQueryClient9 } from "@tanstack/react-query"; function useFundEscrow() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient9(); const mutation = useMutation9({ mutationFn: ({ payload, type }) => client.fundEscrow(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { fundEscrow: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useChangeMilestoneStatus.ts import { useMutation as useMutation10, useQueryClient as useQueryClient10 } from "@tanstack/react-query"; function useChangeMilestoneStatus() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient10(); const mutation = useMutation10({ mutationFn: ({ payload, type }) => client.changeMilestoneStatus(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { changeMilestoneStatus: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } // src/hooks/useApproveMilestone.ts import { useMutation as useMutation11, useQueryClient as useQueryClient11 } from "@tanstack/react-query"; function useApproveMilestone() { const client = useTrustlessWorkClient(); const queryClient = useQueryClient11(); const mutation = useMutation11({ mutationFn: ({ payload, type }) => client.approveMilestone(payload, type), onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["escrows"] }); } }); return { approveMilestone: mutation.mutateAsync, isPending: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, unsignedTransaction: mutation.data?.unsignedTransaction }; } export { TrustlessWorkConfig, useInitializeEscrow, useSendTransaction, useGetEscrow, useUpdateEscrow, useStartDispute, useResolveDispute, useGetMultipleEscrowBalances, useReleaseFunds, useFundEscrow, useChangeMilestoneStatus, useApproveMilestone };