UNPKG

@bpanel/chain-sockets

Version:

A utility plugin for bPanel for managing blockchain state in your redux store

64 lines (54 loc) 1.65 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.watchChain = watchChain; exports.subscribeBlockConnect = subscribeBlockConnect; exports.setChainTip = setChainTip; var _bcoin = require('bcoin'); var _bpanelUtils = require('@bpanel/bpanel-utils'); var _hsd = require('hsd'); function watchChain() { return { type: 'EMIT_SOCKET', bsock: { type: 'broadcast', message: 'watch chain' } }; } function subscribeBlockConnect() { return { type: 'EMIT_SOCKET', bsock: { type: 'subscribe', message: 'block connect', responseEvent: 'new block' } }; } function setChainTip(entry) { return function (dispatch, getState) { var currentClient = getState().clients.currentClient; var blockMeta = void 0; if (currentClient.chain === 'handshake') blockMeta = _hsd.ChainEntry.fromRaw(entry);else blockMeta = _bcoin.ChainEntry.fromRaw(entry); var calcProgress = _bpanelUtils.chain.calcProgress; var _blockMeta = blockMeta, time = _blockMeta.time, hash = _blockMeta.hash, height = _blockMeta.height; var genesis = getState().chain.genesis.time; var prevProgress = getState().chain.progress; var progress = calcProgress(genesis, time); var chain = { tip: hash, progress: progress, height: height }; if (progress > 0.8 || progress - prevProgress > 0.0000001) { // only update the chain tip if // progress is noticeably different // should resolve some frontend performance issues return dispatch({ type: 'SET_CHAIN_TIP', payload: chain }); } }; }