@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
42 lines • 2.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.lastBlockV2 = lastBlockV2;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const constants_1 = require("../constants");
const api_1 = require("../network/api");
const hgraph_1 = require("../network/hgraph");
const utils_1 = require("./utils");
/**
* Gets the latest "block" information for Hedera.
*
* Hedera doesn't have actual blocks - it uses a timestamp-based consensus model.
* To make Hedera compatible with block-based architecture:
* 1. We fetch the most recent transaction from the mirror node
* 2. Extract its consensus timestamp
* 3. Convert this timestamp into a synthetic block using a hardcoded time window (10 seconds by default)
*/
async function lastBlockV2() {
// see getBlock implementation, block data should be immutable: we do not allow querying blocks on non-finalized time range.
// => we search the most recent transaction, but only in finalized time range (ending 10 seconds ago).
const before = new Date(Date.now() - constants_1.FINALITY_MS - constants_1.SYNTHETIC_BLOCK_WINDOW_SECONDS * 1000);
const [latestTransaction, latestHgraphTimestampNs] = await Promise.all([
api_1.apiClient.getLatestTransaction(before),
hgraph_1.hgraphClient.getLatestIndexedConsensusTimestamp(),
]);
const lastMirrorTimestamp = latestTransaction.consensus_timestamp;
// just like with mirror timestamp, we subtract one full block window from the hgraph timestamp
// without this, getBlock() re-fetches hgraph's latest timestamp independently and may find it has not advanced past the block's end boundary
const lastHgraphTimestamp = (0, utils_1.nanosToSeconds)(latestHgraphTimestampNs).minus(constants_1.SYNTHETIC_BLOCK_WINDOW_SECONDS);
// take the smaller of the two timestamps (mirror node vs hgraph) to ensure we only return blocks for which we have all data available
const consensusTimestamp = bignumber_js_1.default.minimum(lastMirrorTimestamp, lastHgraphTimestamp).toFixed(9);
const syntheticBlock = (0, utils_1.getSyntheticBlock)(consensusTimestamp);
return {
height: syntheticBlock.blockHeight,
hash: syntheticBlock.blockHash,
time: syntheticBlock.blockTime,
};
}
//# sourceMappingURL=lastBlock.v2.js.map