-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuseEscrowedEXA.ts
169 lines (143 loc) · 4.78 KB
/
useEscrowedEXA.ts
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { useCallback, useEffect, useState, useMemo } from 'react';
import { Address, zeroAddress, getAddress, type ContractFunctionConfig } from 'viem';
import {
escrowedExaABI,
useEscrowedExaBalanceOf,
useEscrowedExaReserveRatio,
useEscrowedExaReserves,
useEscrowedExaVestingPeriod,
sablierV2LockupLinearABI,
} from 'types/abi';
import useContract from './useContract';
import { useEXA } from './useEXA';
import useGraphClient from './useGraphClient';
import { useWeb3 } from './useWeb3';
import { getStreams } from 'queries/getStreams';
import { useContractReads } from 'wagmi';
import { useSablierV2LockupLinear } from './useSablier';
export const useEscrowedEXA = () => {
return useContract('esEXA', escrowedExaABI);
};
type Stream = {
id: string;
tokenId: string;
recipient: Address;
startTime: string;
endTime: string;
depositAmount: string;
withdrawnAmount: string;
duration: string;
intactAmount: string;
canceled: boolean;
cancelable: boolean;
};
export function useUpdateStreams() {
const EXA = useEXA();
const esEXA = useEscrowedEXA();
const { walletAddress } = useWeb3();
const request = useGraphClient();
const [activeStreams, setActiveStreams] = useState<Stream[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const fetchStreams = useCallback(async () => {
if (EXA && esEXA) {
setLoading(true);
let data;
try {
data = await request<{ streams: Stream[] }>(
getStreams(EXA.address.toLowerCase(), walletAddress || zeroAddress, esEXA.address.toLowerCase(), false),
'sablier',
);
} catch (error) {
data = null;
setLoading(false);
}
if (!data) return;
const filteredStreams = data.streams.filter((stream: Stream) => {
const startTime = Number(stream.startTime);
const endTime = Number(stream.endTime);
const duration = Number(stream.duration);
const intactAmount = BigInt(stream.intactAmount);
return startTime + duration === endTime && intactAmount > BigInt(0);
});
setActiveStreams(filteredStreams);
setLoading(false);
}
}, [EXA, esEXA, request, walletAddress]);
useEffect(() => {
fetchStreams();
}, [fetchStreams]);
return { activeStreams, loading, refetch: fetchStreams };
}
export const useEscrowedEXAReserves = (stream: bigint) => {
const { chain } = useWeb3();
const esEXA = useEscrowedEXA();
return useEscrowedExaReserves({
chainId: chain.id,
address: esEXA?.address,
args: [stream],
staleTime: 30_000,
});
};
export const useEscrowedEXABalance = () => {
const { chain, walletAddress } = useWeb3();
const esEXA = useEscrowedEXA();
return useEscrowedExaBalanceOf({
chainId: chain.id,
address: esEXA?.address,
args: [walletAddress ?? zeroAddress],
staleTime: 30_000,
});
};
export const useEscrowedEXAReserveRatio = () => {
const { chain } = useWeb3();
const esEXA = useEscrowedEXA();
return useEscrowedExaReserveRatio({
chainId: chain.id,
address: esEXA?.address,
staleTime: 30_000,
});
};
export const useEscrowedEXAVestingPeriod = () => {
const { chain } = useWeb3();
const esEXA = useEscrowedEXA();
return useEscrowedExaVestingPeriod({
chainId: chain.id,
address: esEXA?.address,
staleTime: 30_000,
});
};
export const useEscrowEXATotals = (streams: number[]) => {
const { chain } = useWeb3();
const sablier = useSablierV2LockupLinear();
const esEXA = useEscrowedEXA();
const { data: reserves, isLoading: reserveIsLoading } = useContractReads<
ContractFunctionConfig<typeof escrowedExaABI, 'reserves'>[]
>({
contracts: streams.map((stream) => ({
abi: escrowedExaABI,
address: esEXA && getAddress(esEXA.address),
functionName: 'reserves',
args: [BigInt(stream)],
chainId: chain.id,
})),
});
const { data: withdrawables, isLoading: withdrawableIsLoading } = useContractReads<
ContractFunctionConfig<typeof sablierV2LockupLinearABI, 'withdrawableAmountOf'>[]
>({
contracts: streams.map((stream) => ({
abi: sablierV2LockupLinearABI,
address: sablier && getAddress(sablier.address),
functionName: 'withdrawableAmountOf',
args: [BigInt(stream)],
chainId: chain.id,
})),
});
const sum = useCallback((arr: typeof reserves): bigint | undefined => {
if (arr === undefined) return undefined;
if (arr.some(({ status }) => status === 'failure')) return undefined;
return arr.reduce((total, { status, result }) => (status === 'success' ? total + result : total), 0n);
}, []);
const totalReserve = useMemo(() => sum(reserves), [reserves, sum]);
const totalWithdrawable = useMemo(() => sum(withdrawables), [withdrawables, sum]);
return { totalReserve, reserveIsLoading, totalWithdrawable, withdrawableIsLoading };
};