@polkadot/util-crypto#xxhashAsHex TypeScript Examples
The following examples show how to use
@polkadot/util-crypto#xxhashAsHex.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Status.tsx From crust-apps with Apache License 2.0 | 5 votes |
function filterEvents (allAccounts: string[], t: <T = string> (key: string, opts?: Record<string, unknown>) => T, optionsAll?: KeyringOptions, events?: EventRecord[]): ActionStatus[] | null {
const eventHash = xxhashAsHex(stringToU8a(JSON.stringify(events)));
if (!optionsAll || !events || eventHash === prevEventHash) {
return null;
}
prevEventHash = eventHash;
return events
.map(({ event: { data, method, section } }): ActionStatus | null => {
if (section === 'balances' && method === 'Transfer') {
const account = data[1].toString();
if (allAccounts.includes(account)) {
return {
account,
action: `${section}.${method}`,
message: t<string>('transfer received'),
status: 'event'
};
}
} else if (section === 'democracy') {
const index = data[0].toString();
return {
action: `${section}.${method}`,
message: t<string>('update on #{{index}}', {
replace: {
index
}
}),
status: 'event'
};
}
return null;
})
.filter((item): item is ActionStatus => !!item);
}
Example #2
Source File: Events.tsx From crust-apps with Apache License 2.0 | 5 votes |
function EventsBase ({ children }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [state, setState] = useState<Events>([]);
useEffect((): void => {
// No unsub, global context - destroyed on app close
api.isReady.then((): void => {
let prevBlockHash: string | null = null;
let prevEventHash: string | null = null;
api.query.system.events((records): void => {
const newEvents: IndexedEvent[] = records
.map((record, index) => ({ indexes: [index], record }))
.filter(({ record: { event: { method, section } } }) =>
section !== 'system' &&
(method !== 'Deposit' || !['balances', 'treasury'].includes(section)) &&
(section !== 'inclusion' || !['CandidateBacked', 'CandidateIncluded'].includes(method))
)
.reduce((combined: IndexedEvent[], e): IndexedEvent[] => {
const prev = combined.find(({ record: { event: { method, section } } }) => e.record.event.section === section && e.record.event.method === method);
if (prev) {
prev.indexes.push(...e.indexes);
} else {
combined.push(e);
}
return combined;
}, [])
.reverse();
const newEventHash = xxhashAsHex(stringToU8a(JSON.stringify(newEvents)));
if (newEventHash !== prevEventHash && newEvents.length) {
prevEventHash = newEventHash;
// retrieve the last header, this will map to the current state
api.rpc.chain.getHeader().then((header): void => {
const blockNumber = header.number.unwrap();
const blockHash = header.hash.toHex();
if (blockHash !== prevBlockHash) {
prevBlockHash = blockHash;
setState((events) => [
...newEvents.map(({ indexes, record }): KeyedEvent => ({
blockHash,
blockNumber,
indexes,
key: `${blockNumber.toNumber()}-${blockHash}-${indexes.join('.')}`,
record
})),
// remove all events for the previous same-height blockNumber
...events.filter((p) => !p.blockNumber?.eq(blockNumber))
].slice(0, MAX_EVENTS));
}
}).catch(console.error);
}
}).catch(console.error);
}).catch(console.error);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<EventsContext.Provider value={state}>
{children}
</EventsContext.Provider>
);
}
Example #3
Source File: storage.ts From interbtc-api with Apache License 2.0 | 5 votes |
export function getStorageKey(moduleName: string, storageItemName: string): string {
return xxhashAsHex(moduleName, 128) + stripHexPrefix(xxhashAsHex(storageItemName, 128));
}
Example #4
Source File: Status.tsx From subscan-multisig-react with Apache License 2.0 | 5 votes |
function filterEvents(
allAccounts: string[],
t: <T = string>(key: string, opts?: Record<string, unknown>) => T,
optionsAll?: KeyringOptions,
events?: EventRecord[]
): ActionStatus[] | null {
const eventHash = xxhashAsHex(stringToU8a(JSON.stringify(events)));
if (!optionsAll || !events || eventHash === prevEventHash) {
return null;
}
prevEventHash = eventHash;
return events
.map(({ event: { data, method, section } }): ActionStatus | null => {
if (section === 'balances' && method === 'Transfer') {
const account = data[1].toString();
if (allAccounts.includes(account)) {
return {
account,
action: `${section}.${method}`,
message: t<string>('transfer received'),
status: 'event',
};
}
} else if (section === 'democracy') {
const index = data[0].toString();
return {
action: `${section}.${method}`,
message: t<string>('update on #{{index}}', {
replace: {
index,
},
}),
status: 'event',
};
}
return null;
})
.filter((item): item is ActionStatus => !!item);
}
Example #5
Source File: Events.tsx From subscan-multisig-react with Apache License 2.0 | 5 votes |
async function manageEvents(
api: ApiPromise,
prev: PrevHashes,
records: Vec<EventRecord>,
setState: React.Dispatch<React.SetStateAction<Events>>
): Promise<void> {
const newEvents: IndexedEvent[] = records
.map((record, index) => ({ indexes: [index], record }))
.filter(
({
record: {
event: { method, section },
},
}) =>
section !== 'system' &&
(!['balances', 'treasury'].includes(section) || !['Deposit'].includes(method)) &&
(!['parasInclusion', 'inclusion'].includes(section) ||
!['CandidateBacked', 'CandidateIncluded'].includes(method))
)
.reduce((combined: IndexedEvent[], e): IndexedEvent[] => {
const prev = combined.find(
({
record: {
event: { method, section },
},
}) => e.record.event.section === section && e.record.event.method === method
);
if (prev) {
prev.indexes.push(...e.indexes);
} else {
combined.push(e);
}
return combined;
}, [])
.reverse();
const newEventHash = xxhashAsHex(stringToU8a(stringify(newEvents)));
if (newEventHash !== prev.event && newEvents.length) {
prev.event = newEventHash;
// retrieve the last header, this will map to the current state
const header = await api.rpc.chain.getHeader(records.createdAtHash);
const blockNumber = header.number.unwrap();
const blockHash = header.hash.toHex();
if (blockHash !== prev.block) {
prev.block = blockHash;
setState(({ events }) => ({
eventCount: records.length,
events: [
...newEvents.map(
({ indexes, record }): KeyedEvent => ({
blockHash,
blockNumber,
indexes,
key: `${blockNumber.toNumber()}-${blockHash}-${indexes.join('.')}`,
record,
})
),
// remove all events for the previous same-height blockNumber
...events.filter((p) => !p.blockNumber?.eq(blockNumber)),
].slice(0, MAX_EVENTS),
}));
}
} else {
setState(({ events }) => ({
eventCount: records.length,
events,
}));
}
}