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>

330 lines (311 loc) 10.7 kB
// src/provider.tsx import React, { useContext, useState } from "react"; // 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["x-api-key"] = apiKey; return config; }); } /** * Send a transaction * @param signedXdr - The signed XDR transaction string * @returns The response from the API SendTransactionResponse | InitializeEscrowResponse | UpdateEscrowResponse */ sendTransaction(signedXdr) { return this.axios.post("/helper/send-transaction", { signedXdr }).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); } /** * Resolve a dispute * @param data - The data (WithdrawRemainingFundsPayload) to resolve * @returns The response from the API EscrowRequestResponse */ withdrawRemainingFunds(data) { const endpoint = "withdraw-remaining-funds"; const type = "multi-release"; 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 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); } /** * Get multiple escrows from the indexed by signer * @param data - The data (GetEscrowsFromIndexerBySignerParams) to get * @returns The response from the API GetEscrowsFromDBResponse */ getEscrowsFromIndexerBySigner(data) { return this.axios.get(`/helper/get-escrows-by-signer`, { params: data }).then((r) => r.data); } /** * Get multiple escrows from the indexed by role * @param data - The data (GetEscrowsFromIndexerByRoleParams) to get * @returns The response from the API GetEscrowsFromIndexerResponse */ getEscrowsFromIndexerByRole(data) { return this.axios.get(`/helper/get-escrows-by-role`, { params: data }).then((r) => r.data); } /** * Get multiple escrows from the indexed by contractIds * @param data - The data (GetEscrowFromIndexerByContractIdsParams) to get * @returns The response from the API GetEscrowsFromIndexerResponse */ getEscrowFromIndexerByContractIds(data) { return this.axios.get( `/helper/get-escrow-by-contract-ids`, { params: data } ).then((r) => r.data); } /** * Update escrow data from a transaction hash * @param payload - Object containing the transaction hash * @returns UpdateFromTxHashResponse */ updateFromTxHash(payload) { return this.axios.post("/indexer/update-from-txHash", payload).then((r) => r.data); } }; // src/provider.tsx var TrustlessWorkContext = React.createContext({ client: null }); var TrustlessWorkConfig = ({ baseURL, apiKey, children }) => { const [client] = useState(() => new TrustlessWorkClient(baseURL, apiKey)); return /* @__PURE__ */ React.createElement(TrustlessWorkContext.Provider, { value: { client } }, 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(); return { deployEscrow: (payload, type) => client.initializeEscrow(payload, type) }; } // src/hooks/useSendTransaction.ts function useSendTransaction() { const client = useTrustlessWorkClient(); return { sendTransaction: (signedXdr) => client.sendTransaction(signedXdr) }; } // src/hooks/useGetEscrowFromIndexerByContractIds.ts function useGetEscrowFromIndexerByContractIds() { const client = useTrustlessWorkClient(); return { getEscrowByContractIds: (params) => client.getEscrowFromIndexerByContractIds(params) }; } // src/hooks/useUpdateEscrow.ts function useUpdateEscrow() { const client = useTrustlessWorkClient(); return { updateEscrow: (payload, type) => client.updateEscrow(payload, type) }; } // src/hooks/useStartDispute.ts function useStartDispute() { const client = useTrustlessWorkClient(); return { startDispute: (payload, type) => client.startDispute(payload, type) }; } // src/hooks/useResolveDispute.ts function useResolveDispute() { const client = useTrustlessWorkClient(); return { resolveDispute: (payload, type) => client.resolveDispute(payload, type) }; } // src/hooks/useWithdrawRemainingFunds.ts function useWithdrawRemainingFunds() { const client = useTrustlessWorkClient(); return { withdrawRemainingFunds: (payload) => client.withdrawRemainingFunds(payload) }; } // src/hooks/useGetMultipleEscrowBalances.ts function useGetMultipleEscrowBalances() { const client = useTrustlessWorkClient(); return { getMultipleBalances: (payload) => client.getMultipleEscrowBalances(payload) }; } // src/hooks/useReleaseFunds.ts function useReleaseFunds() { const client = useTrustlessWorkClient(); return { releaseFunds: (payload, type) => client.releaseFunds(payload, type) }; } // src/hooks/useFundEscrow.ts function useFundEscrow() { const client = useTrustlessWorkClient(); return { fundEscrow: (payload, type) => client.fundEscrow(payload, type) }; } // src/hooks/useChangeMilestoneStatus.ts function useChangeMilestoneStatus() { const client = useTrustlessWorkClient(); return { changeMilestoneStatus: (payload, type) => client.changeMilestoneStatus(payload, type) }; } // src/hooks/useApproveMilestone.ts function useApproveMilestone() { const client = useTrustlessWorkClient(); return { approveMilestone: (payload, type) => client.approveMilestone(payload, type) }; } // src/hooks/useGetEscrowsFromIndexerBySigner.ts function useGetEscrowsFromIndexerBySigner() { const client = useTrustlessWorkClient(); return { getEscrowsBySigner: (params) => client.getEscrowsFromIndexerBySigner(params) }; } // src/hooks/useGetEscrowsFromIndexerByRole.ts function useGetEscrowsFromIndexerByRole() { const client = useTrustlessWorkClient(); return { getEscrowsByRole: (params) => client.getEscrowsFromIndexerByRole(params) }; } // src/hooks/useUpdateFromTxHash.ts function useUpdateFromTxHash() { const client = useTrustlessWorkClient(); return { updateFromTxHash: (payload) => client.updateFromTxHash(payload) }; } export { TrustlessWorkConfig, useInitializeEscrow, useSendTransaction, useGetEscrowFromIndexerByContractIds, useUpdateEscrow, useStartDispute, useResolveDispute, useWithdrawRemainingFunds, useGetMultipleEscrowBalances, useReleaseFunds, useFundEscrow, useChangeMilestoneStatus, useApproveMilestone, useGetEscrowsFromIndexerBySigner, useGetEscrowsFromIndexerByRole, useUpdateFromTxHash };