-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-report.ts
More file actions
155 lines (133 loc) · 4.72 KB
/
use-report.ts
File metadata and controls
155 lines (133 loc) · 4.72 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
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
import { useEffect, useState, useCallback } from 'react';
import invariant from 'tiny-invariant';
import { useQuery } from '@tanstack/react-query';
import { Address, encodeFunctionData } from 'viem';
import { usePublicClient, useReadContract } from 'wagmi';
import {
getLazyOracleContract,
useVaultInfo,
VAULT_DEFAULT_REPORT_FRESHNESS_DELTA,
VAULT_SHOULD_REPORT_THRESHOLD,
vaultTexts,
} from 'modules/vaults';
import { getVaultHubContract } from '../../modules/vaults/contracts/vault-hub';
import {
STRATEGY_EAGER,
STRATEGY_IMMUTABLE,
} from 'consts/react-query-strategies';
import { getContractAddress } from 'config';
import { VaultHubAbi } from 'abi/vault-hub';
import { fetchReportMerkle } from './ipfs';
import { LazyOracleAbi } from 'abi/lazy-oracle';
const UI_UPDATE_INTERVAL = 5000; // 5 second
const toBlockchainTime = () => Math.floor(Date.now() / 1000);
// query to fetch constant onchain with optimistic data
// will always return bigint and throw warning if onchain!=default
const useReportFreshnessDelta = () => {
const publicClient = usePublicClient();
return useQuery({
queryKey: ['reportFreshnessDelta', publicClient?.chain.id],
placeholderData: VAULT_DEFAULT_REPORT_FRESHNESS_DELTA,
initialData: VAULT_DEFAULT_REPORT_FRESHNESS_DELTA,
queryFn: async () => {
const hub = getVaultHubContract(publicClient);
const delta = await hub.read.REPORT_FRESHNESS_DELTA();
if (delta != VAULT_DEFAULT_REPORT_FRESHNESS_DELTA) {
console.warn(
`[useReportFreshnessDelta] ⚠️⚠️⚠️ Onchain REPORT_FRESHNESS_DELTA(${delta.toString()}) does not match the default value (${VAULT_DEFAULT_REPORT_FRESHNESS_DELTA.toString()}) ⚠️⚠️⚠️ This must be addressed`,
);
}
return delta;
},
// this is okay as delta won't have overflow values
select: (data) => Number(data.toString()),
...STRATEGY_IMMUTABLE,
}).data;
};
// returns status of the report for current vault
export const useReportStatus = () => {
const [time, setTime] = useState<number | null>(null);
// SSR safe timer
useEffect(() => {
setTime(toBlockchainTime);
const interval = setInterval(() => {
setTime(toBlockchainTime);
}, UI_UPDATE_INTERVAL);
return () => {
clearInterval(interval);
};
}, []);
const { activeVault } = useVaultInfo();
const publicClient = usePublicClient();
const vaultHubAddress = getContractAddress(publicClient.chain.id, 'vaultHub');
const lazyOracleAddress = getContractAddress(
publicClient.chain.id,
'lazyOracle',
);
const reportFreshnessDelta = useReportFreshnessDelta();
const vaultReport = useReadContract({
address: vaultHubAddress,
abi: VaultHubAbi,
functionName: 'vaultRecord',
args: [activeVault?.address as Address],
query: { ...STRATEGY_EAGER, enabled: !!activeVault && !!publicClient },
});
const vaultHubReport = useReadContract({
address: lazyOracleAddress,
abi: LazyOracleAbi,
functionName: 'latestReportData',
query: { ...STRATEGY_EAGER },
});
const shouldSkipCheck = !!(time == null || !vaultReport.data);
// optimistically say the report is fresh if we don't have data just yet
const isReportFresh =
shouldSkipCheck ||
time - Number(vaultReport.data.report.timestamp) < reportFreshnessDelta;
const isReportAvailable =
vaultReport.data && vaultHubReport.data
? vaultReport.data.report.timestamp < vaultHubReport.data[0]
: false;
// when new report is available but old is still fresh
// we can show suggestive reporting UI
const shouldApplyReport = !!(
isReportAvailable &&
time &&
vaultReport.data &&
(time - Number(vaultReport.data.report.timestamp)) / reportFreshnessDelta >=
VAULT_SHOULD_REPORT_THRESHOLD
);
const prepareReportCall = useCallback(async () => {
invariant(activeVault, 'activeVault is required');
const lazyOracle = getLazyOracleContract(publicClient);
const reportCid = (await lazyOracle.read.latestReportData())[2];
const report = await fetchReportMerkle(
publicClient.chain.id,
reportCid,
activeVault.address,
);
return {
loadingActionText: vaultTexts.actions.report.loading,
to: lazyOracle.address,
data: encodeFunctionData({
abi: lazyOracle.abi,
functionName: 'updateVaultData',
args: [
activeVault.address,
report.totalValueWei,
report.fee,
report.liabilityShares,
report.slashingReserve,
report.proof,
],
}),
};
}, [activeVault, publicClient]);
return {
...vaultReport,
prepareReportCall,
isLoading: vaultReport.isLoading || shouldSkipCheck,
isReportFresh,
isReportAvailable,
shouldApplyReport,
};
};