-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathuseStakingContracts.ts
More file actions
81 lines (71 loc) · 3.08 KB
/
Copy pathuseStakingContracts.ts
File metadata and controls
81 lines (71 loc) · 3.08 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
import { useMemo } from 'react';
import { STAKING_PROGRAMS } from '@/config/stakingPrograms';
import { StakingProgramId } from '@/constants';
import { useServices, useStakingProgram } from '@/hooks';
export const useStakingContracts = () => {
const { selectedAgentConfig, selectedAgentType, selectedService } =
useServices();
const { evmHomeChainId } = selectedAgentConfig;
const { isActiveStakingProgramLoaded, activeStakingProgramId } =
useStakingProgram();
// The program stored on the middleware service record — set when the user
// picks a contract, so it reflects an actual user choice (unlike the
// agent-config default).
const serviceStakingProgramId =
selectedService?.chain_configs?.[selectedService?.home_chain]?.chain_data
?.user_params?.staking_program_id ?? null;
// "Current" must never fall back to the agent-config default: showing the
// default as the joined/selected contract fabricates a stake the user never
// made (OPE-1841). Prefer the on-chain (subgraph) value, then the
// service-stored choice; otherwise admit we don't know.
const currentStakingProgramId = isActiveStakingProgramLoaded
? (activeStakingProgramId ?? serviceStakingProgramId)
: null;
// Memoize so the array ref is stable across renders — `Object.keys(...).map(...)`
// otherwise returns a fresh array every call, defeating downstream `useMemo`
// and forcing every consumer effect that lists `orderedStakingProgramIds` in
// its deps to fire on every render.
const availableStakingProgramIds = useMemo(
() =>
Object.keys(STAKING_PROGRAMS[evmHomeChainId]).map(
(stakingProgramIdKey) => stakingProgramIdKey as StakingProgramId,
),
[evmHomeChainId],
);
const orderedStakingProgramIds = useMemo(
() =>
availableStakingProgramIds.reduce(
(acc: StakingProgramId[], stakingProgramId: StakingProgramId) => {
if (!isActiveStakingProgramLoaded) return acc;
// Put the current staking program at the top — even if deprecated,
// the user must be able to see the contract they are actually
// staked in, otherwise the list can never mark it as selected.
if (stakingProgramId === currentStakingProgramId)
return [stakingProgramId, ...acc];
// If the program is deprecated, ignore it
if (STAKING_PROGRAMS[evmHomeChainId][stakingProgramId].deprecated)
return acc;
// if the program is not supported by the agent type, ignore it
if (
!STAKING_PROGRAMS[selectedAgentConfig.evmHomeChainId][
stakingProgramId
].agentsSupported.includes(selectedAgentType)
) {
return acc;
}
// Otherwise, append to the end
return [...acc, stakingProgramId];
},
[],
),
[
availableStakingProgramIds,
isActiveStakingProgramLoaded,
currentStakingProgramId,
selectedAgentConfig.evmHomeChainId,
selectedAgentType,
evmHomeChainId,
],
);
return { currentStakingProgramId, orderedStakingProgramIds };
};