1
1
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- import type { Option } from '@polkadot/types' ;
4
+ import type { Option , StorageKey } from '@polkadot/types' ;
5
5
import type { Balance } from '@polkadot/types/interfaces' ;
6
6
// @ts -ignore
7
- import type { PalletReferendaReferendumInfoRankedCollectiveTally , PalletReferendaReferendumStatusRankedCollectiveTally , PalletSocietyBid , PalletSocietyCandidacy } from '@polkadot/types/lookup' ;
7
+ import type { PalletSocietyBid , PalletSocietyCandidacy } from '@polkadot/types/lookup' ;
8
8
import type { Proxy } from '../util/types' ;
9
9
10
10
import { useCallback , useEffect , useMemo , useState } from 'react' ;
@@ -14,6 +14,8 @@ import { BN, BN_ZERO } from '@polkadot/util';
14
14
import { ASSET_HUBS , PROXY_CHAINS } from '../util/constants' ;
15
15
import useActiveRecoveries from './useActiveRecoveries' ;
16
16
import { useInfo } from '.' ;
17
+ import type { AnyTuple , Codec } from '@polkadot/types-codec/types' ;
18
+ import type { ApiPromise } from '@polkadot/api' ;
17
19
18
20
type Item = 'identity' | 'proxy' | 'bounty' | 'recovery' | 'referenda' | 'index' | 'society' | 'multisig' | 'preimage' | 'assets' | 'uniques' | 'NFT' ;
19
21
export type Reserved = { [ key in Item ] ?: Balance | null } ;
@@ -137,37 +139,62 @@ export default function useReservedDetails (address: string | undefined): Reserv
137
139
if ( api . query ?. [ 'referenda' ] ?. [ 'referendumInfoFor' ] ) {
138
140
setValue ( 'referenda' , undefined ) ;
139
141
142
+ interface ReferendaDeposit {
143
+ who : string ;
144
+ amount : number ;
145
+ }
146
+
147
+ interface Referenda {
148
+ approved ?: [ number , null , null ] ;
149
+ timedOut ?: [ number , ReferendaDeposit , null ] ;
150
+ rejected ?: [ number , ReferendaDeposit , null ] ;
151
+ cancelled ?: [ number , ReferendaDeposit , null ] ;
152
+ ongoing ?: { submissionDeposit : ReferendaDeposit , decisionDeposit : ReferendaDeposit } ;
153
+ }
154
+
140
155
let referendaDepositSum = BN_ZERO ;
141
156
142
157
api . query [ 'referenda' ] [ 'referendumInfoFor' ] . entries ( ) . then ( ( referenda ) => {
143
- referenda . forEach ( ( [ _ , value ] ) => {
158
+ referenda . forEach ( ( [ num , value ] ) => {
144
159
if ( ! value . isEmpty ) {
145
160
// @ts -ignore
146
- const ref = ( value . unwrap ( ) ) as PalletReferendaReferendumInfoRankedCollectiveTally | undefined ;
161
+ const ref = value . toPrimitive ( ) as Referenda ;
147
162
148
- if ( ! ref ) {
163
+ if ( ! ref || 'approved' in ref ) {
149
164
return ;
150
165
}
151
166
152
- const info = ( ref . isCancelled
153
- ? ref . asCancelled
154
- : ref . isRejected
155
- ? ref . asRejected
156
- : ref . isOngoing
157
- ? ref . asOngoing
158
- : ref . isApproved ? ref . asApproved : undefined ) as PalletReferendaReferendumStatusRankedCollectiveTally | undefined ;
167
+ if ( ref . timedOut || ref . rejected || ref . cancelled ) {
168
+ const who = ref ?. timedOut ?. [ 1 ] ?. who || ref ?. rejected ?. [ 1 ] ?. who || ref ?. cancelled ?. [ 1 ] ?. who ;
169
+
170
+ if ( who === formatted ) {
171
+ const amount = ref ?. timedOut ?. [ 1 ] ?. amount ?? ref ?. rejected ?. [ 1 ] ?. amount ?? ref ?. cancelled ?. [ 1 ] ?. amount ?? 0 ;
159
172
160
- if ( info ?. submissionDeposit && info . submissionDeposit . who . toString ( ) === formatted ) {
161
- referendaDepositSum = referendaDepositSum . add ( info . submissionDeposit . amount ) ;
173
+ referendaDepositSum = referendaDepositSum . add ( new BN ( amount ) ) ;
174
+ }
162
175
}
163
176
164
- if ( info ?. decisionDeposit ?. isSome ) {
165
- const decisionDeposit = info ?. decisionDeposit . unwrap ( ) ;
177
+ if ( ref . ongoing ) {
178
+ if ( ref . ongoing . submissionDeposit && ref . ongoing . submissionDeposit . who === formatted ) {
179
+ referendaDepositSum = referendaDepositSum . add ( new BN ( ref . ongoing . submissionDeposit . amount ) ) ;
180
+ }
166
181
167
- if ( decisionDeposit . who . toString ( ) === formatted ) {
168
- referendaDepositSum = referendaDepositSum . add ( decisionDeposit . amount ) ;
182
+ if ( ref . ongoing . decisionDeposit && ref . ongoing . decisionDeposit . who === formatted ) {
183
+ referendaDepositSum = referendaDepositSum . add ( new BN ( ref . ongoing . decisionDeposit . amount ) ) ;
169
184
}
170
185
}
186
+
187
+ // if (info?.submissionDeposit && info.submissionDeposit.who.toString() === formatted) {
188
+ // referendaDepositSum = referendaDepositSum.add(info.submissionDeposit.amount);
189
+ // }
190
+
191
+ // if (info?.decisionDeposit?.isSome) {
192
+ // const decisionDeposit = info?.decisionDeposit.unwrap();
193
+
194
+ // if (decisionDeposit.who.toString() === formatted) {
195
+ // referendaDepositSum = referendaDepositSum.add(decisionDeposit.amount);
196
+ // }
197
+ // }
171
198
}
172
199
} ) ;
173
200
@@ -275,28 +302,82 @@ export default function useReservedDetails (address: string | undefined): Reserv
275
302
if ( api . query ?. [ 'preimage' ] ?. [ 'requestStatusFor' ] ) {
276
303
setValue ( 'preimage' , undefined ) ;
277
304
278
- let sum = BN_ZERO ;
305
+ interface Preimage {
306
+ unrequested ?: {
307
+ len : number ;
308
+ ticket : [ string , number ] ; // address, amount
309
+ deposit ?: [ string , number ] | null ; // address, amount (for old preimage)
310
+ } ;
311
+ requested ?: {
312
+ maybeTicket : [ string , number ] ; // address, amount
313
+ deposit ?: [ string , number ] | null ; // address, amount (for old preimage)
314
+ } | null ;
315
+ }
316
+
317
+ const calculatePreimageDeposit = ( preimages : [ StorageKey < AnyTuple > , Codec ] [ ] | undefined , formatted : string ) : BN => {
318
+ let sum = new BN ( 0 ) ;
319
+
320
+ if ( ! preimages ) {
321
+ return sum ;
322
+ }
279
323
280
- api . query [ 'preimage' ] [ 'requestStatusFor' ] . entries ( ) . then ( ( preimages ) => {
281
324
preimages . forEach ( ( [ _ , value ] ) => {
282
- if ( ! value . isEmpty ) {
283
- const status = value . toPrimitive ( ) as { unrequested ?: { len : number , ticket : [ string , number ] } } ;
325
+ if ( value . isEmpty ) {
326
+ return ;
327
+ }
284
328
285
- if ( status . unrequested ) {
286
- const [ accountId , deposit ] = status . unrequested . ticket ;
329
+ const status = value . toPrimitive ( ) as unknown as Preimage ;
287
330
288
- if ( accountId . toString ( ) === formatted ) {
289
- sum = sum . add ( new BN ( deposit ) ) ;
290
- }
331
+ // Helper function to add deposit if account matches
332
+ const addDepositIfMatched = ( ticket : [ string , number ] | null | undefined ) => {
333
+ if ( ! ticket ) {
334
+ return ;
291
335
}
292
- }
336
+
337
+ const [ accountId , deposit ] = ticket ;
338
+
339
+ if ( accountId === formatted ) {
340
+ sum = sum . add ( new BN ( deposit ) ) ;
341
+ }
342
+ } ;
343
+
344
+ // Check unrequested and requested preimage statuses
345
+ addDepositIfMatched ( status ?. unrequested ?. ticket ) ;
346
+ addDepositIfMatched ( status ?. requested ?. maybeTicket ) ;
347
+ addDepositIfMatched ( status ?. unrequested ?. deposit ) ;
348
+ addDepositIfMatched ( status ?. requested ?. deposit ) ;
293
349
} ) ;
294
350
295
- setValue ( 'preimage' , sum ) ;
296
- } ) . catch ( ( error ) => {
297
- console . error ( error ) ;
298
- setValue ( 'preimage' , null ) ;
299
- } ) ;
351
+ return sum ;
352
+ } ;
353
+
354
+ const fetchPreimageDeposits = async ( ) => {
355
+ if ( ! formatted ) {
356
+ return ;
357
+ }
358
+
359
+ try {
360
+ // Fetch both new and old preimage entries
361
+ const [ newPreimage , oldPreimage ] = await Promise . all ( [
362
+ api . query [ 'preimage' ] [ 'requestStatusFor' ] . entries ( ) ,
363
+ api . query [ 'preimage' ] [ 'statusFor' ] . entries ( )
364
+ ] ) ;
365
+
366
+ // Calculate deposits for both new and old preimages
367
+ const totalSum = calculatePreimageDeposit ( newPreimage , formatted ) . add ( calculatePreimageDeposit ( oldPreimage , formatted ) ) ;
368
+
369
+ setValue ( 'preimage' , totalSum ) ;
370
+ } catch ( error ) {
371
+ console . error ( error ) ;
372
+ setValue ( 'preimage' , null ) ;
373
+ }
374
+ } ;
375
+
376
+ fetchPreimageDeposits ( )
377
+ . catch ( ( error ) => {
378
+ console . error ( error ) ;
379
+ setValue ( 'preimage' , null ) ;
380
+ } ) ;
300
381
} else {
301
382
setValue ( 'preimage' , null ) ;
302
383
}
0 commit comments