Skip to content

Commit 6fc6c8f

Browse files
committed
refactor: update some deposit calculation
1 parent 1b98694 commit 6fc6c8f

File tree

1 file changed

+114
-33
lines changed

1 file changed

+114
-33
lines changed

packages/extension-polkagate/src/hooks/useReservedDetails.ts

Lines changed: 114 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { Option } from '@polkadot/types';
4+
import type { Option, StorageKey } from '@polkadot/types';
55
import type { Balance } from '@polkadot/types/interfaces';
66
// @ts-ignore
7-
import type { PalletReferendaReferendumInfoRankedCollectiveTally, PalletReferendaReferendumStatusRankedCollectiveTally, PalletSocietyBid, PalletSocietyCandidacy } from '@polkadot/types/lookup';
7+
import type { PalletSocietyBid, PalletSocietyCandidacy } from '@polkadot/types/lookup';
88
import type { Proxy } from '../util/types';
99

1010
import { useCallback, useEffect, useMemo, useState } from 'react';
@@ -14,6 +14,8 @@ import { BN, BN_ZERO } from '@polkadot/util';
1414
import { ASSET_HUBS, PROXY_CHAINS } from '../util/constants';
1515
import useActiveRecoveries from './useActiveRecoveries';
1616
import { useInfo } from '.';
17+
import type { AnyTuple, Codec } from '@polkadot/types-codec/types';
18+
import type { ApiPromise } from '@polkadot/api';
1719

1820
type Item = 'identity' | 'proxy' | 'bounty' | 'recovery' | 'referenda' | 'index' | 'society' | 'multisig' | 'preimage' | 'assets' | 'uniques' | 'NFT';
1921
export type Reserved = { [key in Item]?: Balance | null };
@@ -137,37 +139,62 @@ export default function useReservedDetails (address: string | undefined): Reserv
137139
if (api.query?.['referenda']?.['referendumInfoFor']) {
138140
setValue('referenda', undefined);
139141

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+
140155
let referendaDepositSum = BN_ZERO;
141156

142157
api.query['referenda']['referendumInfoFor'].entries().then((referenda) => {
143-
referenda.forEach(([_, value]) => {
158+
referenda.forEach(([num, value]) => {
144159
if (!value.isEmpty) {
145160
// @ts-ignore
146-
const ref = (value.unwrap()) as PalletReferendaReferendumInfoRankedCollectiveTally | undefined;
161+
const ref = value.toPrimitive() as Referenda;
147162

148-
if (!ref) {
163+
if (!ref || 'approved' in ref) {
149164
return;
150165
}
151166

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;
159172

160-
if (info?.submissionDeposit && info.submissionDeposit.who.toString() === formatted) {
161-
referendaDepositSum = referendaDepositSum.add(info.submissionDeposit.amount);
173+
referendaDepositSum = referendaDepositSum.add(new BN(amount));
174+
}
162175
}
163176

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+
}
166181

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));
169184
}
170185
}
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+
// }
171198
}
172199
});
173200

@@ -275,28 +302,82 @@ export default function useReservedDetails (address: string | undefined): Reserv
275302
if (api.query?.['preimage']?.['requestStatusFor']) {
276303
setValue('preimage', undefined);
277304

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+
}
279323

280-
api.query['preimage']['requestStatusFor'].entries().then((preimages) => {
281324
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+
}
284328

285-
if (status.unrequested) {
286-
const [accountId, deposit] = status.unrequested.ticket;
329+
const status = value.toPrimitive() as unknown as Preimage;
287330

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;
291335
}
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);
293349
});
294350

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+
});
300381
} else {
301382
setValue('preimage', null);
302383
}

0 commit comments

Comments
 (0)