@zenland/sdk 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/dist/index.cjs +502 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +382 -0
- package/dist/index.d.ts +382 -0
- package/dist/index.js +465 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +743 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +554 -0
- package/dist/react.d.ts +554 -0
- package/dist/react.js +704 -0
- package/dist/react.js.map +1 -0
- package/package.json +78 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/react/context.tsx
|
|
4
|
+
import { createContext, useContext, useMemo } from "react";
|
|
5
|
+
|
|
6
|
+
// src/request.ts
|
|
7
|
+
var ZenlandGraphQLError = class extends Error {
|
|
8
|
+
errors;
|
|
9
|
+
constructor(message, errors) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "ZenlandGraphQLError";
|
|
12
|
+
this.errors = errors;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var ZenlandRequestError = class extends Error {
|
|
16
|
+
status;
|
|
17
|
+
statusText;
|
|
18
|
+
constructor(message, status, statusText) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "ZenlandRequestError";
|
|
21
|
+
this.status = status;
|
|
22
|
+
this.statusText = statusText;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
async function graphqlRequest(baseUrl, document, variables, options) {
|
|
26
|
+
const endpoint = `${baseUrl}/graphql`;
|
|
27
|
+
const res = await fetch(endpoint, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: {
|
|
30
|
+
"content-type": "application/json"
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({ query: document, variables }),
|
|
33
|
+
signal: options?.signal,
|
|
34
|
+
cache: "no-store"
|
|
35
|
+
});
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
const text = await res.text().catch(() => "");
|
|
38
|
+
throw new ZenlandRequestError(
|
|
39
|
+
`Zenland request failed (${res.status} ${res.statusText})${text ? `: ${text}` : ""}`,
|
|
40
|
+
res.status,
|
|
41
|
+
res.statusText
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const json = await res.json();
|
|
45
|
+
if (json.errors?.length) {
|
|
46
|
+
throw new ZenlandGraphQLError(
|
|
47
|
+
json.errors.map((e) => e.message ?? "GraphQL error").join("; "),
|
|
48
|
+
json.errors
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (!json.data) {
|
|
52
|
+
throw new Error("Zenland response missing data.");
|
|
53
|
+
}
|
|
54
|
+
return json.data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/queries.ts
|
|
58
|
+
var AGENT_QUERY = `
|
|
59
|
+
query Agent($id: String!) {
|
|
60
|
+
agent(id: $id) {
|
|
61
|
+
id
|
|
62
|
+
isActive
|
|
63
|
+
isAvailable
|
|
64
|
+
stablecoinDecimals
|
|
65
|
+
stablecoinToken
|
|
66
|
+
stablecoinStake
|
|
67
|
+
daoTokenStake
|
|
68
|
+
disputeFeeBps
|
|
69
|
+
assignmentFeeBps
|
|
70
|
+
description
|
|
71
|
+
contact
|
|
72
|
+
totalResolved
|
|
73
|
+
activeCases
|
|
74
|
+
registrationTime
|
|
75
|
+
lastEngagementTimestamp
|
|
76
|
+
totalEarnings
|
|
77
|
+
totalSlashed
|
|
78
|
+
cases(limit: 5, orderBy: "invitedAt", orderDirection: "desc") {
|
|
79
|
+
items {
|
|
80
|
+
id
|
|
81
|
+
escrow
|
|
82
|
+
invitedAt
|
|
83
|
+
resolvedAt
|
|
84
|
+
timedOut
|
|
85
|
+
escrowRef {
|
|
86
|
+
id
|
|
87
|
+
amount
|
|
88
|
+
token
|
|
89
|
+
state
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
totalCount
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
`;
|
|
97
|
+
var AGENTS_QUERY = `
|
|
98
|
+
query Agents($where: agentFilter, $orderBy: String, $orderDirection: String, $limit: Int, $offset: Int) {
|
|
99
|
+
agents(where: $where, orderBy: $orderBy, orderDirection: $orderDirection, limit: $limit, offset: $offset) {
|
|
100
|
+
totalCount
|
|
101
|
+
items {
|
|
102
|
+
id
|
|
103
|
+
isActive
|
|
104
|
+
isAvailable
|
|
105
|
+
stablecoinDecimals
|
|
106
|
+
stablecoinStake
|
|
107
|
+
daoTokenStake
|
|
108
|
+
disputeFeeBps
|
|
109
|
+
assignmentFeeBps
|
|
110
|
+
description
|
|
111
|
+
contact
|
|
112
|
+
totalResolved
|
|
113
|
+
activeCases
|
|
114
|
+
registrationTime
|
|
115
|
+
lastEngagementTimestamp
|
|
116
|
+
totalEarnings
|
|
117
|
+
totalSlashed
|
|
118
|
+
}
|
|
119
|
+
pageInfo {
|
|
120
|
+
hasNextPage
|
|
121
|
+
hasPreviousPage
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
`;
|
|
126
|
+
var ESCROW_QUERY = `
|
|
127
|
+
query escrow($id: String!) {
|
|
128
|
+
escrow(id: $id) {
|
|
129
|
+
id
|
|
130
|
+
buyer
|
|
131
|
+
seller
|
|
132
|
+
agent
|
|
133
|
+
amount
|
|
134
|
+
token
|
|
135
|
+
state
|
|
136
|
+
createdAt
|
|
137
|
+
sellerAcceptDeadline
|
|
138
|
+
buyerProtectionTime
|
|
139
|
+
termsHash
|
|
140
|
+
version
|
|
141
|
+
fundedAt
|
|
142
|
+
fulfilledAt
|
|
143
|
+
resolvedAt
|
|
144
|
+
agentInvitedAt
|
|
145
|
+
splitProposer
|
|
146
|
+
proposedBuyerBps
|
|
147
|
+
proposedSellerBps
|
|
148
|
+
buyerApprovedSplit
|
|
149
|
+
sellerApprovedSplit
|
|
150
|
+
agentFeeReceived
|
|
151
|
+
buyerReceived
|
|
152
|
+
sellerReceived
|
|
153
|
+
creationFee
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
`;
|
|
157
|
+
var ESCROWS_QUERY = `
|
|
158
|
+
query escrows(
|
|
159
|
+
$limit: Int = 30
|
|
160
|
+
$offset: Int = 0
|
|
161
|
+
$orderBy: String = "createdAt"
|
|
162
|
+
$orderDirection: String = "desc"
|
|
163
|
+
$where: escrowFilter
|
|
164
|
+
) {
|
|
165
|
+
escrows(
|
|
166
|
+
limit: $limit
|
|
167
|
+
offset: $offset
|
|
168
|
+
orderBy: $orderBy
|
|
169
|
+
orderDirection: $orderDirection
|
|
170
|
+
where: $where
|
|
171
|
+
) {
|
|
172
|
+
items {
|
|
173
|
+
id
|
|
174
|
+
buyer
|
|
175
|
+
seller
|
|
176
|
+
agent
|
|
177
|
+
amount
|
|
178
|
+
token
|
|
179
|
+
state
|
|
180
|
+
createdAt
|
|
181
|
+
fundedAt
|
|
182
|
+
fulfilledAt
|
|
183
|
+
sellerAcceptDeadline
|
|
184
|
+
agentInvitedAt
|
|
185
|
+
buyerProtectionTime
|
|
186
|
+
splitProposer
|
|
187
|
+
buyerApprovedSplit
|
|
188
|
+
sellerApprovedSplit
|
|
189
|
+
proposedBuyerBps
|
|
190
|
+
proposedSellerBps
|
|
191
|
+
}
|
|
192
|
+
pageInfo {
|
|
193
|
+
hasNextPage
|
|
194
|
+
hasPreviousPage
|
|
195
|
+
}
|
|
196
|
+
totalCount
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
`;
|
|
200
|
+
var PROTOCOL_STATS_QUERY = `
|
|
201
|
+
query protocolStats($id: String! = "global") {
|
|
202
|
+
protocolStats(id: $id) {
|
|
203
|
+
id
|
|
204
|
+
totalEscrowsCreated
|
|
205
|
+
totalVolumeEscrowed
|
|
206
|
+
totalFeesCollected
|
|
207
|
+
currentTVL
|
|
208
|
+
activeEscrowCount
|
|
209
|
+
totalAgentsRegistered
|
|
210
|
+
activeAgentsCount
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
`;
|
|
214
|
+
var TRANSACTION_LOGS_QUERY = `
|
|
215
|
+
query transactionLogs(
|
|
216
|
+
$escrowAddress: String
|
|
217
|
+
$limit: Int
|
|
218
|
+
$offset: Int
|
|
219
|
+
$orderBy: String
|
|
220
|
+
$orderDirection: String
|
|
221
|
+
) {
|
|
222
|
+
transactionLogs(
|
|
223
|
+
where: { escrowAddress: $escrowAddress }
|
|
224
|
+
limit: $limit
|
|
225
|
+
offset: $offset
|
|
226
|
+
orderBy: $orderBy
|
|
227
|
+
orderDirection: $orderDirection
|
|
228
|
+
) {
|
|
229
|
+
items {
|
|
230
|
+
id
|
|
231
|
+
txHash
|
|
232
|
+
blockNumber
|
|
233
|
+
timestamp
|
|
234
|
+
eventName
|
|
235
|
+
contractAddress
|
|
236
|
+
contractType
|
|
237
|
+
escrowAddress
|
|
238
|
+
agentAddress
|
|
239
|
+
userAddress
|
|
240
|
+
eventData
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
`;
|
|
245
|
+
|
|
246
|
+
// src/domains/escrows.ts
|
|
247
|
+
var STATE_GROUPS = {
|
|
248
|
+
ACTIVE: ["PENDING", "ACTIVE", "FULFILLED"],
|
|
249
|
+
IN_DISPUTE: ["DISPUTED", "AGENT_INVITED"],
|
|
250
|
+
COMPLETED: ["RELEASED", "AGENT_RESOLVED", "REFUNDED", "SPLIT"]
|
|
251
|
+
};
|
|
252
|
+
function createEscrowsDomain(baseUrl) {
|
|
253
|
+
async function list(args) {
|
|
254
|
+
const where = {};
|
|
255
|
+
if (args?.buyer) where.buyer = args.buyer.toLowerCase();
|
|
256
|
+
if (args?.seller) where.seller = args.seller.toLowerCase();
|
|
257
|
+
if (args?.agent) where.agent = args.agent.toLowerCase();
|
|
258
|
+
if (args?.states && args.states.length > 0) {
|
|
259
|
+
where.state_in = args.states;
|
|
260
|
+
} else if (args?.state) {
|
|
261
|
+
where.state = args.state;
|
|
262
|
+
}
|
|
263
|
+
if (args?.user) {
|
|
264
|
+
const userLower = args.user.toLowerCase();
|
|
265
|
+
where.OR = [{ buyer: userLower }, { seller: userLower }, { agent: userLower }];
|
|
266
|
+
}
|
|
267
|
+
const variables = {
|
|
268
|
+
limit: args?.limit ?? 30,
|
|
269
|
+
offset: args?.offset ?? 0,
|
|
270
|
+
orderBy: args?.orderBy ?? "createdAt",
|
|
271
|
+
orderDirection: args?.orderDirection ?? "desc",
|
|
272
|
+
where: Object.keys(where).length > 0 ? where : void 0
|
|
273
|
+
};
|
|
274
|
+
const response = await graphqlRequest(
|
|
275
|
+
baseUrl,
|
|
276
|
+
ESCROWS_QUERY,
|
|
277
|
+
variables
|
|
278
|
+
);
|
|
279
|
+
return response.escrows;
|
|
280
|
+
}
|
|
281
|
+
async function getById(id) {
|
|
282
|
+
const variables = { id: id.toLowerCase() };
|
|
283
|
+
const response = await graphqlRequest(
|
|
284
|
+
baseUrl,
|
|
285
|
+
ESCROW_QUERY,
|
|
286
|
+
variables
|
|
287
|
+
);
|
|
288
|
+
return response.escrow;
|
|
289
|
+
}
|
|
290
|
+
async function getByUser(userAddress, args) {
|
|
291
|
+
return list({ ...args, user: userAddress });
|
|
292
|
+
}
|
|
293
|
+
async function getByStateGroup(stateGroup, args) {
|
|
294
|
+
return list({ ...args, states: [...STATE_GROUPS[stateGroup]] });
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
list,
|
|
298
|
+
getById,
|
|
299
|
+
getByUser,
|
|
300
|
+
getByStateGroup
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/domains/agents.ts
|
|
305
|
+
function createAgentsDomain(baseUrl) {
|
|
306
|
+
async function list(args) {
|
|
307
|
+
const where = {};
|
|
308
|
+
if (args?.onlyActive) where.isActive = true;
|
|
309
|
+
if (args?.onlyAvailable) where.isAvailable = true;
|
|
310
|
+
const variables = {
|
|
311
|
+
limit: args?.limit ?? 30,
|
|
312
|
+
offset: args?.offset ?? 0,
|
|
313
|
+
orderBy: args?.orderBy ?? "totalResolved",
|
|
314
|
+
orderDirection: args?.orderDirection ?? "desc",
|
|
315
|
+
where: Object.keys(where).length > 0 ? where : void 0
|
|
316
|
+
};
|
|
317
|
+
const response = await graphqlRequest(
|
|
318
|
+
baseUrl,
|
|
319
|
+
AGENTS_QUERY,
|
|
320
|
+
variables
|
|
321
|
+
);
|
|
322
|
+
return response.agents;
|
|
323
|
+
}
|
|
324
|
+
async function getById(id) {
|
|
325
|
+
const variables = { id: id.toLowerCase() };
|
|
326
|
+
const response = await graphqlRequest(
|
|
327
|
+
baseUrl,
|
|
328
|
+
AGENT_QUERY,
|
|
329
|
+
variables
|
|
330
|
+
);
|
|
331
|
+
return response.agent;
|
|
332
|
+
}
|
|
333
|
+
async function getAvailable(args) {
|
|
334
|
+
return list({ ...args, onlyActive: true, onlyAvailable: true });
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
list,
|
|
338
|
+
getById,
|
|
339
|
+
getAvailable
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/domains/protocol-stats.ts
|
|
344
|
+
function normalizeProtocolStats(raw) {
|
|
345
|
+
return {
|
|
346
|
+
id: raw.id,
|
|
347
|
+
totalEscrowsCreated: raw.totalEscrowsCreated,
|
|
348
|
+
totalVolumeEscrowed: BigInt(raw.totalVolumeEscrowed),
|
|
349
|
+
totalFeesCollected: BigInt(raw.totalFeesCollected),
|
|
350
|
+
currentTVL: BigInt(raw.currentTVL),
|
|
351
|
+
activeEscrowCount: raw.activeEscrowCount,
|
|
352
|
+
totalAgentsRegistered: raw.totalAgentsRegistered,
|
|
353
|
+
activeAgentsCount: raw.activeAgentsCount
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function createProtocolStatsDomain(baseUrl) {
|
|
357
|
+
async function get() {
|
|
358
|
+
const variables = { id: "global" };
|
|
359
|
+
const response = await graphqlRequest(
|
|
360
|
+
baseUrl,
|
|
361
|
+
PROTOCOL_STATS_QUERY,
|
|
362
|
+
variables
|
|
363
|
+
);
|
|
364
|
+
if (!response.protocolStats) {
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
return normalizeProtocolStats(response.protocolStats);
|
|
368
|
+
}
|
|
369
|
+
async function getRaw() {
|
|
370
|
+
const variables = { id: "global" };
|
|
371
|
+
const response = await graphqlRequest(
|
|
372
|
+
baseUrl,
|
|
373
|
+
PROTOCOL_STATS_QUERY,
|
|
374
|
+
variables
|
|
375
|
+
);
|
|
376
|
+
return response.protocolStats;
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
get,
|
|
380
|
+
getRaw
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// src/domains/transaction-logs.ts
|
|
385
|
+
function createTransactionLogsDomain(baseUrl) {
|
|
386
|
+
async function list(args) {
|
|
387
|
+
const variables = {
|
|
388
|
+
escrowAddress: args?.escrowAddress?.toLowerCase(),
|
|
389
|
+
limit: args?.limit ?? 100,
|
|
390
|
+
offset: args?.offset ?? 0,
|
|
391
|
+
orderBy: args?.orderBy ?? "timestamp",
|
|
392
|
+
orderDirection: args?.orderDirection ?? "asc"
|
|
393
|
+
};
|
|
394
|
+
const response = await graphqlRequest(
|
|
395
|
+
baseUrl,
|
|
396
|
+
TRANSACTION_LOGS_QUERY,
|
|
397
|
+
variables
|
|
398
|
+
);
|
|
399
|
+
return response.transactionLogs.items;
|
|
400
|
+
}
|
|
401
|
+
async function getByEscrow(escrowAddress, args) {
|
|
402
|
+
return list({ ...args, escrowAddress });
|
|
403
|
+
}
|
|
404
|
+
function parseEventData(eventData) {
|
|
405
|
+
try {
|
|
406
|
+
return JSON.parse(eventData);
|
|
407
|
+
} catch {
|
|
408
|
+
return {};
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
list,
|
|
413
|
+
getByEscrow,
|
|
414
|
+
parseEventData
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/client.ts
|
|
419
|
+
var DEFAULT_BASE_URL = "https://api.zen.land";
|
|
420
|
+
function createZenlandClient(config) {
|
|
421
|
+
const baseUrl = normalizeBaseUrl(config?.baseUrl ?? DEFAULT_BASE_URL);
|
|
422
|
+
return {
|
|
423
|
+
baseUrl,
|
|
424
|
+
escrows: createEscrowsDomain(baseUrl),
|
|
425
|
+
agents: createAgentsDomain(baseUrl),
|
|
426
|
+
protocolStats: createProtocolStatsDomain(baseUrl),
|
|
427
|
+
transactionLogs: createTransactionLogsDomain(baseUrl)
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
function normalizeBaseUrl(url) {
|
|
431
|
+
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
432
|
+
}
|
|
433
|
+
var zenland = createZenlandClient();
|
|
434
|
+
|
|
435
|
+
// src/react/context.tsx
|
|
436
|
+
import { jsx } from "react/jsx-runtime";
|
|
437
|
+
var ZenlandContext = createContext(null);
|
|
438
|
+
function ZenlandProvider({ children, config }) {
|
|
439
|
+
const client = useMemo(() => createZenlandClient(config), [config]);
|
|
440
|
+
return /* @__PURE__ */ jsx(ZenlandContext.Provider, { value: client, children });
|
|
441
|
+
}
|
|
442
|
+
function useZenlandClient() {
|
|
443
|
+
const context = useContext(ZenlandContext);
|
|
444
|
+
if (!context) {
|
|
445
|
+
throw new Error("useZenlandClient must be used within a ZenlandProvider");
|
|
446
|
+
}
|
|
447
|
+
return context;
|
|
448
|
+
}
|
|
449
|
+
function useZenlandClientOptional() {
|
|
450
|
+
const context = useContext(ZenlandContext);
|
|
451
|
+
return useMemo(() => context ?? createZenlandClient(), [context]);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/react/hooks/useEscrow.ts
|
|
455
|
+
import { useQuery } from "@tanstack/react-query";
|
|
456
|
+
function useEscrow(id, options) {
|
|
457
|
+
const client = useZenlandClientOptional();
|
|
458
|
+
return useQuery({
|
|
459
|
+
queryKey: ["zenland", "escrow", id],
|
|
460
|
+
queryFn: () => {
|
|
461
|
+
if (!id) return null;
|
|
462
|
+
return client.escrows.getById(id);
|
|
463
|
+
},
|
|
464
|
+
enabled: !!id && (options?.enabled ?? true)
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// src/react/hooks/useEscrows.ts
|
|
469
|
+
import { useQuery as useQuery2 } from "@tanstack/react-query";
|
|
470
|
+
function useEscrows(args) {
|
|
471
|
+
const client = useZenlandClientOptional();
|
|
472
|
+
const { address, role = "all", stateTab, enabled = true } = args ?? {};
|
|
473
|
+
const getStatesFromTab = () => {
|
|
474
|
+
if (!stateTab || stateTab === "all") {
|
|
475
|
+
return args?.states;
|
|
476
|
+
}
|
|
477
|
+
return [...STATE_GROUPS[stateTab]];
|
|
478
|
+
};
|
|
479
|
+
return useQuery2({
|
|
480
|
+
queryKey: ["zenland", "escrows", address, role, stateTab, args?.state, args?.states, args?.limit, args?.offset],
|
|
481
|
+
queryFn: async () => {
|
|
482
|
+
if (!address) {
|
|
483
|
+
return { items: [], totalCount: 0, pageInfo: { hasNextPage: false, hasPreviousPage: false } };
|
|
484
|
+
}
|
|
485
|
+
const states = getStatesFromTab();
|
|
486
|
+
return client.escrows.list({
|
|
487
|
+
limit: args?.limit,
|
|
488
|
+
offset: args?.offset,
|
|
489
|
+
buyer: role === "buyer" ? address : void 0,
|
|
490
|
+
seller: role === "seller" ? address : void 0,
|
|
491
|
+
agent: role === "agent" ? address : void 0,
|
|
492
|
+
user: role === "all" ? address : void 0,
|
|
493
|
+
state: args?.state,
|
|
494
|
+
states
|
|
495
|
+
});
|
|
496
|
+
},
|
|
497
|
+
enabled: !!address && enabled
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/react/hooks/useAgent.ts
|
|
502
|
+
import { useQuery as useQuery3 } from "@tanstack/react-query";
|
|
503
|
+
function useAgent(address, options) {
|
|
504
|
+
const client = useZenlandClientOptional();
|
|
505
|
+
return useQuery3({
|
|
506
|
+
queryKey: ["zenland", "agent", address],
|
|
507
|
+
queryFn: () => {
|
|
508
|
+
if (!address) return null;
|
|
509
|
+
return client.agents.getById(address);
|
|
510
|
+
},
|
|
511
|
+
enabled: !!address && (options?.enabled ?? true)
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// src/react/hooks/useAgents.ts
|
|
516
|
+
import { useQuery as useQuery4 } from "@tanstack/react-query";
|
|
517
|
+
function useAgents(args) {
|
|
518
|
+
const client = useZenlandClientOptional();
|
|
519
|
+
const { enabled = true, ...filterArgs } = args ?? {};
|
|
520
|
+
return useQuery4({
|
|
521
|
+
queryKey: ["zenland", "agents", filterArgs.onlyActive, filterArgs.onlyAvailable, filterArgs.limit, filterArgs.offset],
|
|
522
|
+
queryFn: () => client.agents.list(filterArgs),
|
|
523
|
+
enabled
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// src/react/hooks/useProtocolStats.ts
|
|
528
|
+
import { useQuery as useQuery5 } from "@tanstack/react-query";
|
|
529
|
+
function useProtocolStats(options) {
|
|
530
|
+
const client = useZenlandClientOptional();
|
|
531
|
+
return useQuery5({
|
|
532
|
+
queryKey: ["zenland", "protocolStats"],
|
|
533
|
+
queryFn: () => client.protocolStats.get(),
|
|
534
|
+
enabled: options?.enabled ?? true,
|
|
535
|
+
staleTime: options?.staleTime ?? 30 * 1e3,
|
|
536
|
+
// 30 seconds
|
|
537
|
+
refetchInterval: options?.refetchInterval ?? 60 * 1e3
|
|
538
|
+
// 1 minute
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/react/hooks/useRecentEscrows.ts
|
|
543
|
+
import { useQuery as useQuery6 } from "@tanstack/react-query";
|
|
544
|
+
function useRecentEscrows(options) {
|
|
545
|
+
const client = useZenlandClientOptional();
|
|
546
|
+
const limit = options?.limit ?? 5;
|
|
547
|
+
return useQuery6({
|
|
548
|
+
queryKey: ["zenland", "recentEscrows", limit],
|
|
549
|
+
queryFn: async () => {
|
|
550
|
+
const result = await client.escrows.list({
|
|
551
|
+
limit,
|
|
552
|
+
orderBy: "createdAt",
|
|
553
|
+
orderDirection: "desc"
|
|
554
|
+
});
|
|
555
|
+
return result.items;
|
|
556
|
+
},
|
|
557
|
+
enabled: options?.enabled ?? true,
|
|
558
|
+
staleTime: options?.staleTime ?? 15 * 1e3,
|
|
559
|
+
// 15 seconds
|
|
560
|
+
refetchInterval: options?.refetchInterval ?? 30 * 1e3
|
|
561
|
+
// 30 seconds
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/react/hooks/useUserStats.ts
|
|
566
|
+
import { useQuery as useQuery7 } from "@tanstack/react-query";
|
|
567
|
+
var ACTIVE_STATES = STATE_GROUPS.ACTIVE;
|
|
568
|
+
var DISPUTE_STATES = STATE_GROUPS.IN_DISPUTE;
|
|
569
|
+
var COMPLETED_STATES = STATE_GROUPS.COMPLETED;
|
|
570
|
+
var TVL_STATES = [...ACTIVE_STATES, ...DISPUTE_STATES];
|
|
571
|
+
async function fetchUserEscrowCount(client, userAddress, states) {
|
|
572
|
+
const userLower = userAddress.toLowerCase();
|
|
573
|
+
const result = await client.escrows.list({
|
|
574
|
+
limit: 1,
|
|
575
|
+
user: userLower,
|
|
576
|
+
states: [...states]
|
|
577
|
+
});
|
|
578
|
+
return result.totalCount;
|
|
579
|
+
}
|
|
580
|
+
async function fetchUserTVL(client, userAddress) {
|
|
581
|
+
const userLower = userAddress.toLowerCase();
|
|
582
|
+
const result = await client.escrows.list({
|
|
583
|
+
limit: 1e3,
|
|
584
|
+
user: userLower,
|
|
585
|
+
states: [...TVL_STATES]
|
|
586
|
+
});
|
|
587
|
+
return result.items.reduce((sum, escrow) => sum + BigInt(escrow.amount), BigInt(0));
|
|
588
|
+
}
|
|
589
|
+
async function fetchUserRecentEscrows(client, userAddress, limit = 5) {
|
|
590
|
+
const userLower = userAddress.toLowerCase();
|
|
591
|
+
const result = await client.escrows.list({
|
|
592
|
+
limit,
|
|
593
|
+
user: userLower
|
|
594
|
+
});
|
|
595
|
+
return result.items;
|
|
596
|
+
}
|
|
597
|
+
async function getUserDashboardStats(client, userAddress) {
|
|
598
|
+
const [activeCount, disputeCount, completedCount, tvl, recentEscrows] = await Promise.all([
|
|
599
|
+
fetchUserEscrowCount(client, userAddress, ACTIVE_STATES),
|
|
600
|
+
fetchUserEscrowCount(client, userAddress, DISPUTE_STATES),
|
|
601
|
+
fetchUserEscrowCount(client, userAddress, COMPLETED_STATES),
|
|
602
|
+
fetchUserTVL(client, userAddress),
|
|
603
|
+
fetchUserRecentEscrows(client, userAddress, 5)
|
|
604
|
+
]);
|
|
605
|
+
return {
|
|
606
|
+
activeCount,
|
|
607
|
+
disputeCount,
|
|
608
|
+
completedCount,
|
|
609
|
+
tvl,
|
|
610
|
+
recentEscrows
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
async function getGlobalDashboardStats(client) {
|
|
614
|
+
const [activeCount, disputeCount, completedCount, recentEscrows] = await Promise.all([
|
|
615
|
+
fetchGlobalEscrowCount(client, [...ACTIVE_STATES]),
|
|
616
|
+
fetchGlobalEscrowCount(client, [...DISPUTE_STATES]),
|
|
617
|
+
fetchGlobalEscrowCount(client, [...COMPLETED_STATES]),
|
|
618
|
+
fetchGlobalRecentEscrows(client, 5)
|
|
619
|
+
]);
|
|
620
|
+
return {
|
|
621
|
+
activeCount,
|
|
622
|
+
disputeCount,
|
|
623
|
+
completedCount,
|
|
624
|
+
tvl: null,
|
|
625
|
+
recentEscrows
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
async function fetchGlobalEscrowCount(client, states) {
|
|
629
|
+
const result = await client.escrows.list({
|
|
630
|
+
limit: 1,
|
|
631
|
+
states
|
|
632
|
+
});
|
|
633
|
+
return result.totalCount;
|
|
634
|
+
}
|
|
635
|
+
async function fetchGlobalRecentEscrows(client, limit = 5) {
|
|
636
|
+
const result = await client.escrows.list({ limit });
|
|
637
|
+
return result.items;
|
|
638
|
+
}
|
|
639
|
+
function useUserStats(userAddress) {
|
|
640
|
+
const client = useZenlandClientOptional();
|
|
641
|
+
return useQuery7({
|
|
642
|
+
queryKey: ["zenland", "userStats", userAddress],
|
|
643
|
+
queryFn: async () => {
|
|
644
|
+
if (!userAddress) return null;
|
|
645
|
+
return getUserDashboardStats(client, userAddress);
|
|
646
|
+
},
|
|
647
|
+
enabled: !!userAddress,
|
|
648
|
+
staleTime: 15 * 1e3,
|
|
649
|
+
refetchInterval: 30 * 1e3
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
function useGlobalStats() {
|
|
653
|
+
const client = useZenlandClientOptional();
|
|
654
|
+
return useQuery7({
|
|
655
|
+
queryKey: ["zenland", "globalStats"],
|
|
656
|
+
queryFn: () => getGlobalDashboardStats(client),
|
|
657
|
+
staleTime: 30 * 1e3,
|
|
658
|
+
refetchInterval: 60 * 1e3
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// src/react/hooks/useTransactionLogsByEscrow.ts
|
|
663
|
+
import { useQuery as useQuery8 } from "@tanstack/react-query";
|
|
664
|
+
function useTransactionLogsByEscrow(escrowAddress, options) {
|
|
665
|
+
const client = useZenlandClientOptional();
|
|
666
|
+
const addr = escrowAddress?.toLowerCase();
|
|
667
|
+
return useQuery8({
|
|
668
|
+
queryKey: [
|
|
669
|
+
"zenland",
|
|
670
|
+
"transactionLogs",
|
|
671
|
+
"escrow",
|
|
672
|
+
addr,
|
|
673
|
+
options?.list?.limit,
|
|
674
|
+
options?.list?.offset,
|
|
675
|
+
options?.list?.orderBy,
|
|
676
|
+
options?.list?.orderDirection
|
|
677
|
+
],
|
|
678
|
+
queryFn: async () => {
|
|
679
|
+
if (!addr) return [];
|
|
680
|
+
return client.transactionLogs.getByEscrow(addr, options?.list);
|
|
681
|
+
},
|
|
682
|
+
enabled: !!addr && (options?.enabled ?? true),
|
|
683
|
+
staleTime: options?.staleTime ?? 3e4,
|
|
684
|
+
refetchInterval: options?.refetchInterval,
|
|
685
|
+
refetchOnWindowFocus: false
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
export {
|
|
689
|
+
STATE_GROUPS,
|
|
690
|
+
ZenlandProvider,
|
|
691
|
+
createZenlandClient,
|
|
692
|
+
useAgent,
|
|
693
|
+
useAgents,
|
|
694
|
+
useEscrow,
|
|
695
|
+
useEscrows,
|
|
696
|
+
useGlobalStats,
|
|
697
|
+
useProtocolStats,
|
|
698
|
+
useRecentEscrows,
|
|
699
|
+
useTransactionLogsByEscrow,
|
|
700
|
+
useUserStats,
|
|
701
|
+
useZenlandClient,
|
|
702
|
+
useZenlandClientOptional
|
|
703
|
+
};
|
|
704
|
+
//# sourceMappingURL=react.js.map
|