Squash event extraction for block details (#5193)

This commit is contained in:
Jaco 2021-05-03 14:51:57 +03:00 committed by GitHub
parent 3f9bb30485
commit c996ace09b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 46 deletions

View File

@ -38,7 +38,9 @@ function formatTuple (tuple: Tuple): React.ReactNode {
function JustificationList ({ value }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const headerRef = useRef([[t('justifications'), 'start']]);
const headerRef = useRef([
[t('justifications'), 'start']
]);
const justifications = value.unwrapOr(null);

View File

@ -101,7 +101,9 @@ function formatItem (item: DigestItem): React.ReactNode {
function Logs ({ value }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const headerRef = useRef([[t('logs'), 'start']]);
const headerRef = useRef([
[t('logs'), 'start']
]);
return (
<Table

View File

@ -19,51 +19,27 @@ interface Props {
signedBlock?: SignedBlock;
}
function extractEventDetails (events?: KeyedEvent[]): [BN?, BN?, BN?] {
return events
? events.reduce(([deposits, transfers, weight], { record: { event: { data, method, section } } }) => [
section === 'balances' && method === 'Deposit'
? deposits.iadd(data[1] as Balance)
: deposits,
section === 'balances' && method === 'Transfer'
? transfers.iadd(data[2] as Balance)
: transfers,
section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method)
? weight.iadd(((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight)
: weight
], [new BN(0), new BN(0), new BN(0)])
: [];
}
function Summary ({ events, maxBlockWeight, signedBlock }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const totalWeight = useMemo(
() => events
?.filter(({ record: { event: { method, section } } }) =>
section === 'system' &&
['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method)
)
.reduce((weight: BN, { record: { event: { data, method } } }) =>
weight.iadd(
(method === 'ExtrinsicSuccess'
? data[0] as DispatchInfo
: data[1] as DispatchInfo
).weight
), new BN(0)
),
[events]
);
const deposits = useMemo(
() => events
?.filter(({ record: { event: { method, section } } }) =>
section === 'balances' &&
['Deposit'].includes(method)
)
.reduce((deposits: BN, { record: { event: { data } } }) =>
deposits.iadd(
data[1] as Balance
), new BN(0)
),
[events]
);
const transfers = useMemo(
() => events
?.filter(({ record: { event: { method, section } } }) =>
section === 'balances' &&
['Transfer'].includes(method)
)
.reduce((deposits: BN, { record: { event: { data } } }) =>
deposits.iadd(
data[2] as Balance
), new BN(0)
),
const [deposits, transfers, weight] = useMemo(
() => extractEventDetails(events),
[events]
);
@ -88,10 +64,10 @@ function Summary ({ events, maxBlockWeight, signedBlock }: Props): React.ReactEl
progress={{
hideValue: true,
total: maxBlockWeight,
value: totalWeight
value: weight
}}
>
{formatNumber(totalWeight)}
{formatNumber(weight)}
</CardSummary>
</section>
)}