@@ -20,6 +20,8 @@ export enum CantMigrateReason {
20
20
NoAvailableStakingSlots = 'The program has no more avaiable slots' ,
21
21
NotStakedForMinimumDuration = 'Pearl has not been staked for the required minimum duration' ,
22
22
PearlCurrentlyRunning = 'Unable to switch while Pearl is running' ,
23
+ LoadingServices = 'Loading services...' ,
24
+ CannotFindStakingContractInfo = 'Cannot obtain staking contract information' ,
23
25
}
24
26
25
27
type MigrateValidation =
@@ -34,18 +36,26 @@ type MigrateValidation =
34
36
export const useMigrate = ( stakingProgramId : StakingProgramId ) => {
35
37
const { serviceStatus } = useServices ( ) ;
36
38
const { serviceTemplate } = useServiceTemplates ( ) ;
37
- const { isBalanceLoaded, safeBalance, totalOlasStakedBalance } = useBalance ( ) ;
39
+ const {
40
+ isBalanceLoaded,
41
+ masterSafeBalance : safeBalance ,
42
+ totalOlasStakedBalance,
43
+ } = useBalance ( ) ;
38
44
const { activeStakingProgramId, activeStakingProgramMeta } =
39
45
useStakingProgram ( ) ;
46
+
40
47
const {
41
48
activeStakingContractInfo,
42
49
isServiceStaked,
43
50
isServiceStakedForMinimumDuration,
44
51
isStakingContractInfoLoaded,
45
- isRewardsAvailable,
46
- hasEnoughServiceSlots,
52
+ stakingContractInfoRecord,
47
53
} = useStakingContractInfo ( ) ;
48
54
55
+ const stakingContractInfo = stakingContractInfoRecord ?. [ stakingProgramId ] ;
56
+
57
+ const { hasInitialLoaded : isServicesLoaded } = useServices ( ) ;
58
+
49
59
const minimumOlasRequiredToMigrate = useMemo (
50
60
( ) => getMinimumStakedAmountRequired ( serviceTemplate , stakingProgramId ) ,
51
61
[ serviceTemplate , stakingProgramId ] ,
@@ -67,19 +77,37 @@ export const useMigrate = (stakingProgramId: StakingProgramId) => {
67
77
totalOlasStakedBalance ,
68
78
] ) ;
69
79
80
+ const hasEnoughOlasForFirstRun = useMemo ( ( ) => {
81
+ if ( ! isBalanceLoaded ) return false ;
82
+ if ( isNil ( safeBalance ?. OLAS ) ) return false ;
83
+ if ( isNil ( minimumOlasRequiredToMigrate ) ) return false ;
84
+
85
+ return safeBalance . OLAS >= minimumOlasRequiredToMigrate ;
86
+ } , [ isBalanceLoaded , minimumOlasRequiredToMigrate , safeBalance ] ) ;
87
+
70
88
const migrateValidation = useMemo < MigrateValidation > ( ( ) => {
71
- // loading requirements
89
+ if ( ! isServicesLoaded ) {
90
+ return { canMigrate : false , reason : CantMigrateReason . LoadingServices } ;
91
+ }
92
+
72
93
if ( ! isBalanceLoaded ) {
73
94
return { canMigrate : false , reason : CantMigrateReason . LoadingBalance } ;
74
95
}
75
96
76
- if ( ! isStakingContractInfoLoaded ) {
97
+ if ( isServicesLoaded && ! isStakingContractInfoLoaded ) {
77
98
return {
78
99
canMigrate : false ,
79
100
reason : CantMigrateReason . LoadingStakingContractInfo ,
80
101
} ;
81
102
}
82
103
104
+ if ( ! stakingContractInfo ) {
105
+ return {
106
+ canMigrate : false ,
107
+ reason : CantMigrateReason . CannotFindStakingContractInfo ,
108
+ } ;
109
+ }
110
+
83
111
// general requirements
84
112
if ( activeStakingProgramId === stakingProgramId ) {
85
113
return {
@@ -88,14 +116,17 @@ export const useMigrate = (stakingProgramId: StakingProgramId) => {
88
116
} ;
89
117
}
90
118
91
- if ( ! isRewardsAvailable ) {
119
+ if ( ( stakingContractInfo . availableRewards ?? 0 ) <= 0 ) {
92
120
return {
93
121
canMigrate : false ,
94
122
reason : CantMigrateReason . NoAvailableRewards ,
95
123
} ;
96
124
}
97
125
98
- if ( ! hasEnoughServiceSlots ) {
126
+ if (
127
+ ( stakingContractInfo . serviceIds ?? [ ] ) . length >=
128
+ ( stakingContractInfo . maxNumServices ?? 0 )
129
+ ) {
99
130
return {
100
131
canMigrate : false ,
101
132
reason : CantMigrateReason . NoAvailableStakingSlots ,
@@ -145,12 +176,12 @@ export const useMigrate = (stakingProgramId: StakingProgramId) => {
145
176
146
177
return { canMigrate : true } ;
147
178
} , [
179
+ isServicesLoaded ,
148
180
isBalanceLoaded ,
149
181
isStakingContractInfoLoaded ,
182
+ stakingContractInfo ,
150
183
activeStakingProgramId ,
151
184
stakingProgramId ,
152
- isRewardsAvailable ,
153
- hasEnoughServiceSlots ,
154
185
hasEnoughOlasToMigrate ,
155
186
isServiceStaked ,
156
187
activeStakingProgramMeta ?. canMigrateTo ,
@@ -159,7 +190,49 @@ export const useMigrate = (stakingProgramId: StakingProgramId) => {
159
190
serviceStatus ,
160
191
] ) ;
161
192
193
+ const firstDeployValidation = useMemo < MigrateValidation > ( ( ) => {
194
+ if ( ! isServicesLoaded ) {
195
+ return { canMigrate : false , reason : CantMigrateReason . LoadingServices } ;
196
+ }
197
+
198
+ if ( ! isBalanceLoaded ) {
199
+ return { canMigrate : false , reason : CantMigrateReason . LoadingBalance } ;
200
+ }
201
+
202
+ if ( ! hasEnoughOlasForFirstRun ) {
203
+ return {
204
+ canMigrate : false ,
205
+ reason : CantMigrateReason . InsufficientOlasToMigrate ,
206
+ } ;
207
+ }
208
+
209
+ const stakingContractInfo = stakingContractInfoRecord ?. [ stakingProgramId ] ;
210
+
211
+ if ( stakingContractInfo ?. availableRewards === 0 ) {
212
+ return {
213
+ canMigrate : false ,
214
+ reason : CantMigrateReason . NoAvailableRewards ,
215
+ } ;
216
+ }
217
+
218
+ if ( ! stakingContractInfo ?. maxNumServices ) {
219
+ return {
220
+ canMigrate : false ,
221
+ reason : CantMigrateReason . NoAvailableStakingSlots ,
222
+ } ;
223
+ }
224
+
225
+ return { canMigrate : true } ;
226
+ } , [
227
+ isServicesLoaded ,
228
+ isBalanceLoaded ,
229
+ hasEnoughOlasForFirstRun ,
230
+ stakingContractInfoRecord ,
231
+ stakingProgramId ,
232
+ ] ) ;
233
+
162
234
return {
163
235
migrateValidation,
236
+ firstDeployValidation,
164
237
} ;
165
238
} ;
0 commit comments