@turtleclub/hooks 0.5.0-beta.6 → 0.5.0-beta.8
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/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/v2/lib/query-config.ts +13 -1
- package/src/v2/schemas/shared.ts +3 -3
- package/src/v2/streams/api.ts +59 -0
- package/src/v2/streams/hooks.ts +43 -0
- package/src/v2/streams/index.ts +11 -0
- package/src/v2/streams/queries.ts +14 -0
- package/src/v2/streams/schemas.ts +132 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turtleclub/hooks",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-beta.
|
|
4
|
+
"version": "0.5.0-beta.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"access": "public"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "0b675d696684bba909e149eb26fb4bba7322a82e"
|
|
58
58
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
+
import { QueryFunction, UseQueryOptions } from "@tanstack/react-query";
|
|
2
|
+
|
|
1
3
|
// Default TanStack Query configuration
|
|
2
4
|
// Override per-query as needed
|
|
3
|
-
|
|
4
5
|
export const queryDefaults = {
|
|
5
6
|
staleTime: 5 * 60 * 1000, // 5 minutes - data is fresh
|
|
6
7
|
gcTime: 10 * 60 * 1000, // 10 minutes - cache garbage collection (formerly cacheTime)
|
|
7
8
|
retry: 1, // Retry failed requests once
|
|
8
9
|
refetchOnWindowFocus: false, // Don't refetch on tab focus
|
|
9
10
|
} as const;
|
|
11
|
+
|
|
12
|
+
export function createQueryOptions<TData, TError = Error>(
|
|
13
|
+
queryConfig: { queryKey: readonly unknown[]; queryFn: QueryFunction<TData, any> },
|
|
14
|
+
options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">
|
|
15
|
+
) {
|
|
16
|
+
return {
|
|
17
|
+
...queryDefaults,
|
|
18
|
+
...queryConfig,
|
|
19
|
+
...options,
|
|
20
|
+
} as UseQueryOptions<TData, TError, TData, any>;
|
|
21
|
+
}
|
package/src/v2/schemas/shared.ts
CHANGED
|
@@ -21,9 +21,9 @@ export const tokenSchema = z.object({
|
|
|
21
21
|
logoUrl: z.string().optional(),
|
|
22
22
|
isNative: z.boolean(),
|
|
23
23
|
priceUsd: z.number().optional(),
|
|
24
|
-
price: z.
|
|
25
|
-
amount: z.
|
|
26
|
-
rawAmount: z.
|
|
24
|
+
price: z.string().optional(),
|
|
25
|
+
amount: z.string().optional(),
|
|
26
|
+
rawAmount: z.string().optional(),
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
export const organizationSchema = z.object({
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { apiClient } from "@/v2";
|
|
2
|
+
import {
|
|
3
|
+
GetStreamsQuery,
|
|
4
|
+
getStreamsOutputSchema,
|
|
5
|
+
StreamsSchema,
|
|
6
|
+
getSupportedChainsOutputSchema,
|
|
7
|
+
SupportedChainsResponse,
|
|
8
|
+
SignatureRequestInput,
|
|
9
|
+
signatureRequestOutputSchema,
|
|
10
|
+
SignatureRequestOutput,
|
|
11
|
+
} from "./schemas";
|
|
12
|
+
|
|
13
|
+
export async function getStreams(query?: GetStreamsQuery): Promise<StreamsSchema["streams"]> {
|
|
14
|
+
const params = new URLSearchParams();
|
|
15
|
+
if (query?.streamId) params.set("id", query.streamId);
|
|
16
|
+
if (query?.userId) params.set("userId", query.userId);
|
|
17
|
+
if (query?.organizationId) params.set("organizationId", query.organizationId);
|
|
18
|
+
if (query?.withSnapshots) params.append("withSnapshots", "true");
|
|
19
|
+
if (query?.usersCount && query?.withSnapshots) params.append("usersCount", "true");
|
|
20
|
+
|
|
21
|
+
const queryString = params.toString();
|
|
22
|
+
const endpoint = `/streams${queryString ? `?${queryString}` : ""}`;
|
|
23
|
+
|
|
24
|
+
const data = await apiClient.fetch(endpoint, { method: "GET" });
|
|
25
|
+
const result = getStreamsOutputSchema.safeParse(data);
|
|
26
|
+
|
|
27
|
+
if (!result.success) {
|
|
28
|
+
throw new Error(`Failed to parse streams: ${result.error.message}`);
|
|
29
|
+
}
|
|
30
|
+
return result.data.streams;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function getSupportedChains(): Promise<SupportedChainsResponse["chains"]> {
|
|
34
|
+
const endpoint = "/streams/supported_chains";
|
|
35
|
+
const data = await apiClient.fetch(endpoint, { method: "GET" });
|
|
36
|
+
const result = getSupportedChainsOutputSchema.safeParse(data);
|
|
37
|
+
|
|
38
|
+
if (!result.success) {
|
|
39
|
+
throw new Error(`Failed to parse supported chains: ${result.error.message}`);
|
|
40
|
+
}
|
|
41
|
+
return result.data.chains;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function signatureRequest(
|
|
45
|
+
organizationId: string,
|
|
46
|
+
input: SignatureRequestInput
|
|
47
|
+
): Promise<SignatureRequestOutput> {
|
|
48
|
+
const endpoint = `/streams/request_signature/${organizationId}`;
|
|
49
|
+
const data = await apiClient.fetch(endpoint, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
body: input,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const result = signatureRequestOutputSchema.safeParse(data);
|
|
55
|
+
if (!result.success) {
|
|
56
|
+
throw new Error(`Failed to parse signature response: ${result.error.message}`);
|
|
57
|
+
}
|
|
58
|
+
return result.data;
|
|
59
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useQuery, useMutation, UseMutationOptions, UseQueryOptions } from "@tanstack/react-query";
|
|
2
|
+
import { createQueryOptions } from "../lib/query-config";
|
|
3
|
+
import { streamsQueries } from "./queries";
|
|
4
|
+
import { signatureRequest } from "./api";
|
|
5
|
+
import {
|
|
6
|
+
GetStreamsQuery,
|
|
7
|
+
SignatureRequestInput,
|
|
8
|
+
SignatureRequestOutput,
|
|
9
|
+
Stream,
|
|
10
|
+
SupportedChainsResponse,
|
|
11
|
+
} from "./schemas";
|
|
12
|
+
|
|
13
|
+
export interface UseStreamsOptions
|
|
14
|
+
extends Omit<UseQueryOptions<Stream[], Error>, "queryKey" | "queryFn"> {
|
|
15
|
+
query?: GetStreamsQuery;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function useStreams({ query, ...options }: UseStreamsOptions = {}) {
|
|
19
|
+
return useQuery(createQueryOptions(streamsQueries.list(query), options));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface UseStreamSupportedChainsOptions
|
|
23
|
+
extends Omit<UseQueryOptions<SupportedChainsResponse["chains"], Error>, "queryKey" | "queryFn"> {}
|
|
24
|
+
|
|
25
|
+
export function useStreamSupportedChains(options?: UseStreamSupportedChainsOptions) {
|
|
26
|
+
return useQuery(createQueryOptions(streamsQueries.supportedChains, options));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function useSignatureRequest(
|
|
30
|
+
options?: Omit<
|
|
31
|
+
UseMutationOptions<
|
|
32
|
+
SignatureRequestOutput,
|
|
33
|
+
Error,
|
|
34
|
+
{ organizationId: string; data: SignatureRequestInput }
|
|
35
|
+
>,
|
|
36
|
+
"mutationFn"
|
|
37
|
+
>
|
|
38
|
+
) {
|
|
39
|
+
return useMutation({
|
|
40
|
+
mutationFn: ({ organizationId, data }) => signatureRequest(organizationId, data),
|
|
41
|
+
...options,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createQueryKeys } from "@lukemorales/query-key-factory";
|
|
2
|
+
import { getStreams, getSupportedChains } from "./api";
|
|
3
|
+
import { GetStreamsQuery } from "./schemas";
|
|
4
|
+
|
|
5
|
+
export const streamsQueries = createQueryKeys("streams", {
|
|
6
|
+
list: (query?: GetStreamsQuery) => ({
|
|
7
|
+
queryKey: [query ?? "all"],
|
|
8
|
+
queryFn: () => getStreams(query),
|
|
9
|
+
}),
|
|
10
|
+
supportedChains: {
|
|
11
|
+
queryKey: null,
|
|
12
|
+
queryFn: () => getSupportedChains(),
|
|
13
|
+
},
|
|
14
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
// --- Shared & Base Schemas ---
|
|
4
|
+
|
|
5
|
+
export const snapshotSchema = z.object({
|
|
6
|
+
amountDistributed: z.string(),
|
|
7
|
+
createdAt: z.string().datetime(),
|
|
8
|
+
rootHash: z.string().nullable(),
|
|
9
|
+
timestamp: z.string().datetime(),
|
|
10
|
+
updatedAt: z.string().datetime(),
|
|
11
|
+
userCount: z.number().optional(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const customArgsTokensPerUsdSchema = z.object({
|
|
15
|
+
tokensPerUSD: z.string(),
|
|
16
|
+
targetChainId: z.number(),
|
|
17
|
+
targetTokenAddress: z.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const customArgsAprSchema = z.object({
|
|
21
|
+
apr: z.string(),
|
|
22
|
+
targetChainId: z.number(),
|
|
23
|
+
targetTokenAddress: z.string(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const streamSchema = z.object({
|
|
27
|
+
admin: z.string(),
|
|
28
|
+
chainId: z.number(),
|
|
29
|
+
chargedFee: z.string(),
|
|
30
|
+
contractAddress: z.string(),
|
|
31
|
+
createdAt: z.string().datetime(),
|
|
32
|
+
customArgs: z.union([customArgsTokensPerUsdSchema, customArgsAprSchema]),
|
|
33
|
+
deletedAt: z.string().datetime().nullable(),
|
|
34
|
+
endTimestamp: z.string().datetime(),
|
|
35
|
+
finalizationTimestamp: z.string().datetime(),
|
|
36
|
+
id: z.string().uuid(),
|
|
37
|
+
isPaused: z.boolean(),
|
|
38
|
+
lastSnapshot: snapshotSchema.nullable(),
|
|
39
|
+
orgId: z.string().uuid(),
|
|
40
|
+
rewardToken: z.string(),
|
|
41
|
+
snapshots: z.array(snapshotSchema).optional(),
|
|
42
|
+
startTimestamp: z.string().datetime(),
|
|
43
|
+
strategy: z.string(),
|
|
44
|
+
totalAmount: z.string(),
|
|
45
|
+
type: z.number(),
|
|
46
|
+
updatedAt: z.string().datetime(),
|
|
47
|
+
userId: z.string().uuid(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// --- API Request/Response Schemas ---
|
|
51
|
+
|
|
52
|
+
export const getStreamsOutputSchema = z.object({
|
|
53
|
+
streams: z.array(streamSchema),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export const getSupportedChainsOutputSchema = z.object({
|
|
57
|
+
success: z.boolean(),
|
|
58
|
+
chains: z.array(
|
|
59
|
+
z.object({
|
|
60
|
+
id: z.string(),
|
|
61
|
+
name: z.string(),
|
|
62
|
+
slug: z.string(),
|
|
63
|
+
chainId: z.number(),
|
|
64
|
+
logoUrl: z.string(),
|
|
65
|
+
ecosystem: z.string(),
|
|
66
|
+
status: z.string(),
|
|
67
|
+
explorerUrl: z.string(),
|
|
68
|
+
campaignFactory: z.string(), // Address as string
|
|
69
|
+
})
|
|
70
|
+
),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const signatureRequestInputSchema = z.object({
|
|
74
|
+
campaignType: z.number(),
|
|
75
|
+
chainId: z.number(),
|
|
76
|
+
customArgs: z.intersection(
|
|
77
|
+
z.object({
|
|
78
|
+
targetChainId: z.number(),
|
|
79
|
+
targetTokenAddress: z.string(),
|
|
80
|
+
}),
|
|
81
|
+
z.union([z.object({ tokensPerUsd: z.string() }), z.object({ apr: z.string() })])
|
|
82
|
+
),
|
|
83
|
+
endTimestamp: z.string(),
|
|
84
|
+
finalizationTimestamp: z.string(),
|
|
85
|
+
rewardToken: z.string(),
|
|
86
|
+
startTimestamp: z.string(),
|
|
87
|
+
totalAmount: z.string(),
|
|
88
|
+
walletAddress: z.string(),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export const signatureRequestOutputSchema = z.object({
|
|
92
|
+
success: z.boolean(),
|
|
93
|
+
message: z.string(),
|
|
94
|
+
txParams: z.object({
|
|
95
|
+
chainId: z.number(),
|
|
96
|
+
sender: z.string(),
|
|
97
|
+
params: z.object({
|
|
98
|
+
params: z.object({
|
|
99
|
+
UserId: z.array(z.number()),
|
|
100
|
+
OrgId: z.array(z.number()),
|
|
101
|
+
CampaignType: z.number(),
|
|
102
|
+
RewardToken: z.string(),
|
|
103
|
+
TotalAmount: z.number(),
|
|
104
|
+
FeeBps: z.number(),
|
|
105
|
+
StartTimestamp: z.number(),
|
|
106
|
+
EndTimestamp: z.number(),
|
|
107
|
+
FinalizationTimestamp: z.number(),
|
|
108
|
+
CampaignData: z.string(),
|
|
109
|
+
}),
|
|
110
|
+
signature: z.string(),
|
|
111
|
+
salt: z.string(),
|
|
112
|
+
deadline: z.number(),
|
|
113
|
+
}),
|
|
114
|
+
}),
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
export const getStreamsQuerySchema = z.object({
|
|
118
|
+
streamId: z.string().optional(),
|
|
119
|
+
userId: z.string().optional(),
|
|
120
|
+
organizationId: z.string().optional(),
|
|
121
|
+
withSnapshots: z.boolean().optional(),
|
|
122
|
+
usersCount: z.boolean().optional(),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// --- Types ---
|
|
126
|
+
|
|
127
|
+
export type Stream = z.infer<typeof streamSchema>;
|
|
128
|
+
export type StreamsSchema = z.infer<typeof getStreamsOutputSchema>;
|
|
129
|
+
export type SupportedChainsResponse = z.infer<typeof getSupportedChainsOutputSchema>;
|
|
130
|
+
export type SignatureRequestInput = z.infer<typeof signatureRequestInputSchema>;
|
|
131
|
+
export type SignatureRequestOutput = z.infer<typeof signatureRequestOutputSchema>;
|
|
132
|
+
export type GetStreamsQuery = z.infer<typeof getStreamsQuerySchema>;
|