Moonbeam: display account id from author id (#5363)
* draft get account id from author id * draft block mapping * add authorFrom mapping type and cleanup * remove console.log * Update packages/react-query/src/BlockAuthors.tsx Co-authored-by: Jaco <jacogr@gmail.com> * add more safeguards checks * Update packages/react-query/src/BlockAuthors.tsx Co-authored-by: Jaco <jacogr@gmail.com> * revert change on lock * add missing import Co-authored-by: Jaco <jacogr@gmail.com>
This commit is contained in:
parent
9d6b1a604c
commit
c67e201fb6
@ -9,8 +9,12 @@ import { Link } from 'react-router-dom';
|
||||
import { AddressMini, Digits } from '@polkadot/react-components';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
// TODO update HeaderExtended in api-derive
|
||||
export interface HeaderExtendedWithMapping extends HeaderExtended {
|
||||
authorFromMapping?: string;
|
||||
}
|
||||
interface Props {
|
||||
value: HeaderExtended;
|
||||
value: HeaderExtendedWithMapping;
|
||||
}
|
||||
|
||||
function BlockHeader ({ value }: Props): React.ReactElement<Props> | null {
|
||||
@ -27,9 +31,11 @@ function BlockHeader ({ value }: Props): React.ReactElement<Props> | null {
|
||||
</td>
|
||||
<td className='all hash overflow'>{hashHex}</td>
|
||||
<td className='address'>
|
||||
{value.author && (
|
||||
<AddressMini value={value.author} />
|
||||
)}
|
||||
{value.authorFromMapping
|
||||
? <AddressMini value={value.authorFromMapping} />
|
||||
: value.author && (
|
||||
<AddressMini value={value.author} />
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
|
||||
@ -7,15 +7,20 @@ import type { EraRewardPoints } from '@polkadot/types/interfaces';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
import { formatNumber, isFunction } from '@polkadot/util';
|
||||
|
||||
// TODO update HeaderExtended in api-derive
|
||||
export interface HeaderExtendedWithMapping extends HeaderExtended {
|
||||
authorFromMapping?: string;
|
||||
}
|
||||
|
||||
export interface Authors {
|
||||
byAuthor: Record<string, string>;
|
||||
eraPoints: Record<string, string>;
|
||||
lastBlockAuthors: string[];
|
||||
lastBlockNumber?: string;
|
||||
lastHeader?: HeaderExtended;
|
||||
lastHeaders: HeaderExtended[];
|
||||
lastHeader?: HeaderExtendedWithMapping;
|
||||
lastHeaders: HeaderExtendedWithMapping[];
|
||||
}
|
||||
|
||||
interface Props {
|
||||
@ -38,9 +43,10 @@ function BlockAuthorsBase ({ children }: Props): React.ReactElement<Props> {
|
||||
useEffect((): void => {
|
||||
// No unsub, global context - destroyed on app close
|
||||
api.isReady.then((): void => {
|
||||
let lastHeaders: HeaderExtended[] = [];
|
||||
let lastHeaders: HeaderExtendedWithMapping[] = [];
|
||||
let lastBlockAuthors: string[] = [];
|
||||
let lastBlockNumber = '';
|
||||
const isAuthorMapping = isFunction(api.query.authorMapping?.mapping);
|
||||
|
||||
// subscribe to all validators
|
||||
api.query.session && api.query.session.validators((validatorIds): void => {
|
||||
@ -48,10 +54,19 @@ function BlockAuthorsBase ({ children }: Props): React.ReactElement<Props> {
|
||||
}).catch(console.error);
|
||||
|
||||
// subscribe to new headers
|
||||
api.derive.chain.subscribeNewHeads((lastHeader): void => {
|
||||
api.derive.chain.subscribeNewHeads(async (lastHeader: HeaderExtendedWithMapping): Promise<void> => {
|
||||
if (lastHeader?.number) {
|
||||
const blockNumber = lastHeader.number.unwrap();
|
||||
const thisBlockAuthor = lastHeader.author?.toString();
|
||||
let thisBlockAuthor = '';
|
||||
|
||||
if (lastHeader.author) {
|
||||
thisBlockAuthor = lastHeader.author.toString();
|
||||
} else if (isAuthorMapping && lastHeader.digest.logs && lastHeader.digest.logs[0] && lastHeader.digest.logs[0].isConsensus && lastHeader.digest.logs[0].asConsensus[1]) {
|
||||
// Some blockchains such as Moonbeam need to fetch the author accountId from a mapping
|
||||
thisBlockAuthor = (await api.query.authorMapping.mapping(lastHeader.digest.logs[0].asConsensus[1])).toString();
|
||||
lastHeader.authorFromMapping = thisBlockAuthor;
|
||||
}
|
||||
|
||||
const thisBlockNumber = formatNumber(blockNumber);
|
||||
|
||||
if (thisBlockAuthor) {
|
||||
@ -67,7 +82,7 @@ function BlockAuthorsBase ({ children }: Props): React.ReactElement<Props> {
|
||||
|
||||
lastHeaders = lastHeaders
|
||||
.filter((old, index) => index < MAX_HEADERS && old.number.unwrap().lt(blockNumber))
|
||||
.reduce((next, header): HeaderExtended[] => {
|
||||
.reduce((next, header): HeaderExtendedWithMapping[] => {
|
||||
next.push(header);
|
||||
|
||||
return next;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user