Sha256: 5a90de018dd6eb27e709e40b754b66c2e45003b2ec8c89bdea3dcd1b5ebf361b

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

import { fetchHeadersLast, fetchHeight } from '../../api-node/blocks';
import { wait } from '../utils';
import detectInterval from './detectInterval';
const storage = Object.create(null);
export default function (base, current) {
    return Promise.all([
        getInterval(base),
        current == undefined ? fetchHeight(base).then(({ height }) => height + 1) : current
    ]).then(([interval, current]) => loop(interval, current));
    function loop(interval, current) {
        return fetchHeadersLast(base).then(({ height, timestamp }) => {
            if (height >= current) {
                return { height };
            }
            const blocksToWait = current - height;
            const now = Date.now();
            const timeout = (((blocksToWait - 1) * interval) + ((interval - Math.abs(now - timestamp)))) * 0.8;
            return wait(inRange(timeout, 200, (interval * blocksToWait) * 0.8)).then(() => loop(interval, current));
        });
    }
}
function inRange(current, min, max) {
    return Math.round(Math.min(Math.max(current, min), max));
}
function getInterval(base) {
    if (storage[base]) {
        return Promise.resolve(storage[base]);
    }
    else {
        return detectInterval(base).then(interval => {
            storage[base] = interval;
            return interval;
        });
    }
}
//# sourceMappingURL=waitHeight.js.map

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
waves_lib-0.1.0 js/node_modules/@waves/node-api-js/es/tools/blocks/waitHeight.js