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>

389 lines (368 loc) 13.4 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { TrustlessWorkConfig: () => TrustlessWorkConfig, development: () => development, mainNet: () => mainNet, useApproveMilestone: () => useApproveMilestone, useChangeMilestoneStatus: () => useChangeMilestoneStatus, useFundEscrow: () => useFundEscrow, useGetEscrowFromIndexerByContractIds: () => useGetEscrowFromIndexerByContractIds, useGetEscrowsFromIndexerByRole: () => useGetEscrowsFromIndexerByRole, useGetEscrowsFromIndexerBySigner: () => useGetEscrowsFromIndexerBySigner, useGetMultipleEscrowBalances: () => useGetMultipleEscrowBalances, useInitializeEscrow: () => useInitializeEscrow, useReleaseFunds: () => useReleaseFunds, useResolveDispute: () => useResolveDispute, useSendTransaction: () => useSendTransaction, useStartDispute: () => useStartDispute, useUpdateEscrow: () => useUpdateEscrow, useUpdateFromTxHash: () => useUpdateFromTxHash, useWithdrawRemainingFunds: () => useWithdrawRemainingFunds }); module.exports = __toCommonJS(index_exports); // src/provider.tsx var import_react = __toESM(require("react")); // src/client.ts var import_axios = __toESM(require("axios")); var TrustlessWorkClient = class { constructor(baseURL, apiKey) { this.axios = import_axios.default.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 = import_react.default.createContext({ client: null }); var TrustlessWorkConfig = ({ baseURL, apiKey, children }) => { const [client] = (0, import_react.useState)(() => new TrustlessWorkClient(baseURL, apiKey)); return /* @__PURE__ */ import_react.default.createElement(TrustlessWorkContext.Provider, { value: { client } }, children); }; function useTrustlessWorkClient() { const ctx = (0, import_react.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) }; } // src/index.ts var mainNet = "https://api.trustlesswork.com"; var development = "https://dev.api.trustlesswork.com"; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TrustlessWorkConfig, development, mainNet, useApproveMilestone, useChangeMilestoneStatus, useFundEscrow, useGetEscrowFromIndexerByContractIds, useGetEscrowsFromIndexerByRole, useGetEscrowsFromIndexerBySigner, useGetMultipleEscrowBalances, useInitializeEscrow, useReleaseFunds, useResolveDispute, useSendTransaction, useStartDispute, useUpdateEscrow, useUpdateFromTxHash, useWithdrawRemainingFunds });