@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>
479 lines (462 loc) • 16.5 kB
JavaScript
;
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,
useGetEscrow: () => useGetEscrow,
useGetMultipleEscrowBalances: () => useGetMultipleEscrowBalances,
useInitializeEscrow: () => useInitializeEscrow,
useReleaseFunds: () => useReleaseFunds,
useResolveDispute: () => useResolveDispute,
useSendTransaction: () => useSendTransaction,
useStartDispute: () => useStartDispute,
useUpdateEscrow: () => useUpdateEscrow
});
module.exports = __toCommonJS(index_exports);
// src/provider.tsx
var import_react = __toESM(require("react"));
var import_react_query = require("@tanstack/react-query");
// 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["Authorization"] = `Bearer ${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);
}
/**
* 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
* @param type - The type of escrow (single-release or multi-release) to get
* @returns The response from the API Escrow
*/
getEscrow(data, type) {
return this.axios.get(
`/escrow/${type}/get-escrow`,
{
params: data
}
).then((r) => r.data);
}
/**
* Get multiple balances
* @param data - The data (GetBalanceParams) to get
* @param type - The type of escrow (single-release or multi-release) to get
* @returns The response from the API GetEscrowBalancesResponse
*/
getMultipleEscrowBalances(data, type) {
return this.axios.get(
`/escrow/${type}/get-multiple-escrow-balance`,
{
params: data
}
).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));
const [queryClient] = (0, import_react.useState)(() => new import_react_query.QueryClient());
return /* @__PURE__ */ import_react.default.createElement(TrustlessWorkContext.Provider, { value: { client } }, /* @__PURE__ */ import_react.default.createElement(import_react_query.QueryClientProvider, { client: queryClient }, 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
var import_react_query2 = require("@tanstack/react-query");
function useInitializeEscrow() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query2.useQueryClient)();
const mutation = (0, import_react_query2.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
var import_react_query3 = require("@tanstack/react-query");
function useSendTransaction() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query3.useQueryClient)();
const mutation = (0, import_react_query3.useMutation)({
mutationFn: (signedXdr) => client.sendTransaction(signedXdr),
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ["sendTransaction"] });
}
});
return {
sendTransaction: mutation.mutateAsync,
isPending: mutation.isPending,
isError: mutation.isError,
isSuccess: mutation.isSuccess
};
}
// src/hooks/useGetEscrow.ts
var import_react_query4 = require("@tanstack/react-query");
function useGetEscrow() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query4.useQueryClient)();
const mutation = (0, import_react_query4.useMutation)({
mutationFn: ({
payload,
type
}) => client.getEscrow(payload, type),
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
var import_react_query5 = require("@tanstack/react-query");
function useUpdateEscrow() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query5.useQueryClient)();
const mutation = (0, import_react_query5.useMutation)({
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
var import_react_query6 = require("@tanstack/react-query");
function useStartDispute() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query6.useQueryClient)();
const mutation = (0, import_react_query6.useMutation)({
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
var import_react_query7 = require("@tanstack/react-query");
function useResolveDispute() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query7.useQueryClient)();
const mutation = (0, import_react_query7.useMutation)({
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
var import_react_query8 = require("@tanstack/react-query");
function useGetMultipleEscrowBalances() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query8.useQueryClient)();
const mutation = (0, import_react_query8.useMutation)({
mutationFn: ({
payload,
type
}) => client.getMultipleEscrowBalances(payload, type),
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
var import_react_query9 = require("@tanstack/react-query");
function useReleaseFunds() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query9.useQueryClient)();
const mutation = (0, import_react_query9.useMutation)({
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
var import_react_query10 = require("@tanstack/react-query");
function useFundEscrow() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query10.useQueryClient)();
const mutation = (0, import_react_query10.useMutation)({
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
var import_react_query11 = require("@tanstack/react-query");
function useChangeMilestoneStatus() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query11.useQueryClient)();
const mutation = (0, import_react_query11.useMutation)({
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
var import_react_query12 = require("@tanstack/react-query");
function useApproveMilestone() {
const client = useTrustlessWorkClient();
const queryClient = (0, import_react_query12.useQueryClient)();
const mutation = (0, import_react_query12.useMutation)({
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
};
}
// 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,
useGetEscrow,
useGetMultipleEscrowBalances,
useInitializeEscrow,
useReleaseFunds,
useResolveDispute,
useSendTransaction,
useStartDispute,
useUpdateEscrow
});