@polkadot/util#stringify TypeScript Examples

The following examples show how to use @polkadot/util#stringify. 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: Events.tsx    From subscan-multisig-react with Apache License 2.0 5 votes vote down vote up
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,
    }));
  }
}