forked from Cookie-Jar-DAO/cookie-jar-v3
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseStreamingData.ts
More file actions
128 lines (112 loc) · 3.35 KB
/
useStreamingData.ts
File metadata and controls
128 lines (112 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use client";
import { useQuery } from "@tanstack/react-query";
import { formatUnits } from "viem";
import { useSuperfluidFramework } from "../blockchain/useSuperfluidFramework";
import { useStreamsByReceiver } from "../blockchain/useSuperfluidSubgraph";
/**
* Stream configuration structure
*/
export interface StreamingConfig {
streamingEnabled: boolean;
requireStreamApproval: boolean;
maxStreamRate: bigint;
minStreamDuration: number;
}
/**
* Individual stream data
*/
export interface StreamInfo {
id: string;
sender: string;
token: string;
tokenSymbol?: string;
ratePerSecond: bigint;
totalStreamed: bigint;
isActive: boolean;
lastUpdated: number;
flowRate: bigint;
}
/**
* Hook for reading streaming data using Superfluid SDK
*/
export const useStreamingData = (jarAddress: `0x${string}`) => {
// Superfluid framework data (not currently used)
useSuperfluidFramework();
// Query all active streams to this jar using The Graph subgraph
const {
data: subgraphStreams,
isLoading: isLoadingSubgraph,
error: subgraphError,
refetch,
} = useStreamsByReceiver(jarAddress);
// Transform subgraph data into StreamInfo format
const {
data: streamsQuery,
isLoading,
error,
} = useQuery({
queryKey: ["superfluidStreams", "transformed", jarAddress, subgraphStreams],
queryFn: async (): Promise<StreamInfo[]> => {
if (!subgraphStreams) return [];
// Transform subgraph stream data to our StreamInfo format
return subgraphStreams.map((stream) => {
const currentFlowRate = BigInt(stream.currentFlowRate);
const streamedUntil = BigInt(stream.streamedUntilUpdatedAt);
const lastUpdated = Number(stream.updatedAtTimestamp) * 1000;
return {
id: stream.id,
sender: stream.sender.id,
token: stream.token.id,
tokenSymbol: stream.token.symbol,
ratePerSecond: currentFlowRate,
totalStreamed: streamedUntil,
isActive: currentFlowRate > 0n,
lastUpdated,
flowRate: currentFlowRate,
};
});
},
enabled: !!subgraphStreams,
staleTime: 30_000, // 30 seconds
});
// Get streaming configuration (mock for now - could be added to contract if needed)
const streamingConfig: StreamingConfig = {
streamingEnabled: true,
requireStreamApproval: true,
maxStreamRate: BigInt("1000000000000000000"), // 1 token per second
minStreamDuration: 3600, // 1 hour
};
// Helper function to calculate claimable amount for a stream
const calculateClaimable = (stream: StreamInfo): bigint => {
const now = BigInt(Math.floor(Date.now() / 1000));
const lastUpdated = BigInt(Math.floor(stream.lastUpdated / 1000));
const timeElapsed = now - lastUpdated;
return timeElapsed * stream.ratePerSecond;
};
// Helper function to format stream rate
const formatStreamRate = (
ratePerSecond: bigint,
decimals: number = 18,
): string => {
const ratePerHour = ratePerSecond * BigInt(3600);
const ratePerDay = ratePerSecond * BigInt(86400);
if (ratePerDay < BigInt(10) ** BigInt(decimals)) {
return `${formatUnits(ratePerHour, decimals)}/hour`;
} else {
return `${formatUnits(ratePerDay, decimals)}/day`;
}
};
return {
// Raw data
streamingConfig,
// Processed data
streams: streamsQuery || [],
isLoadingStreams: isLoading || isLoadingSubgraph,
streamsError: error || subgraphError,
// Utilities
calculateClaimable,
formatStreamRate,
// Query controls
refetchStreams: refetch,
};
};