-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuseProtocolStats.ts
More file actions
60 lines (53 loc) · 1.71 KB
/
Copy pathuseProtocolStats.ts
File metadata and controls
60 lines (53 loc) · 1.71 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
import { useMemo } from 'react'
import type { Abi } from 'viem'
import { useReadContracts } from 'wagmi'
import { FeeProxyABI, type AffiliateStats } from '../contracts'
import { useAffiliates } from './useAffiliates'
import { useFeeProxyAddress } from './useFeeProxyAddress'
/**
* Network-wide aggregate stats for the Home hero card. Enumerates affiliates
* from registration events, then batch-reads each `affiliateStats` and sums
* the headline figures. No indexer needed; fine for the current scale.
*/
export function useProtocolStats() {
const { feeProxy, configured } = useFeeProxyAddress()
const { affiliates, isLoading: listLoading } = useAffiliates()
const contracts = useMemo(
() =>
affiliates.map((a) => ({
address: feeProxy,
abi: FeeProxyABI as Abi,
functionName: 'affiliateStats',
args: [a.affiliate],
})),
[affiliates, feeProxy],
)
const { data, isLoading: statsLoading } = useReadContracts({
contracts: contracts as any,
query: { enabled: configured && affiliates.length > 0 },
})
const agg = useMemo(() => {
let totalForwarded = 0n
let totalFees = 0n
let totalGross = 0n
let totalTx = 0n
if (data) {
for (const r of data) {
if (r.status === 'success' && r.result) {
const s = r.result as unknown as AffiliateStats
totalForwarded += s.totalForwardedAssets
totalFees += s.totalFees
totalGross += s.totalGrossAssets
totalTx += s.txCount
}
}
}
return { totalForwarded, totalFees, totalGross, totalTx }
}, [data])
return {
affiliateCount: affiliates.length,
...agg,
configured,
isLoading: listLoading || statsLoading,
}
}