@satellite-earth/clock v2.0.0
View full documentation at https://docs.satellite.earth/
Usage
const Clock = require('@satellite-earth/clock');
const Web3 = require('web3');
// ...
// Create the clock
const clock = new Clock();
// Get a web3 instance to read blockchain data
const web3 = new Web3(provider);
// Populate the clock with data from blocks in the given range
const await clock.synchronize(web3);
// Now the clock is ready to use
Constructor
Array
- (optional) Array of objects to initialize block data, each containing sequential block datanumber
,hash
, andtimestamp
Properties
initialized
clock.initialized
Boolean
- If clock has been been initialized (i.e. if it contains record of any blocks)
min
clock.min
Number|null
- Earliest synced block number (null
if clock is uninitialized)
max
clock.max
Number|null
- Latest synced block number (null
if clock is uninitialized)
Methods
init
clock.init(blocks);
Initialize clock's internal data structure. Useful for loading saved clock data instead of having to resynchronize with the blockchain.
Parameters
Array
- Array of objects, each containing sequential block datanumber
,hash
, andtimestamp
synchronize
await clock.synchronize(earth, { startBlock, toBlock });
Populate clock with block data directly from the blockchain. By default, the clock starts synchronizing from it's last synced position, or the latest block available.
Parameters
Core
- Earth core instance (used to communicate with blockchain)Object
- OptionsstartBlock
-Number
- Block number of last block to synctoBlock
-Number
- Block number of first block to sync
Returns
Promise
returns Object
- New block data
readHash
const block = clock.readHash(hash);
console.log(block); // { hash, number, timestamp }
Lookup a block's number and timestamp by its hash. Return undefined
if block not found.
Parameters
String
- Block hashNumber
- (optional) Number of blocks between this block and maximum synced block required to return value. Default0
Returns
Obejct|undefined
- Block data
readNumber
const block = clock.readNumber(hash);
console.log(block); // { hash, number, timestamp }
Lookup a block's hash and timestamp by its number. Return undefined
if block not found.
Parameters
Number
- Block numberNumber
- (optional) Number of blocks between this block an maximum synced block required to return value. Default0
Returns
Obejct|undefined
- Block data
compareHash
const delta = clock.compareHash(hashA, hashB);
console.log(delta) // 42
Given two blockhashes, return the difference in their corresponding block's numbers. If either block is not found, return 0
. Useful for sorting blocks by time via hash.
Parameters
String
- Hash of one blockString
- Hash of other block
Returns
Number
- Block number difference
advance
console.log(clock.max); // 10337501
clock.advance({ number, timestamp, hash, parentHash });
console.log(clock.max); // 10337502
Add a block to the clock's internal data structure. Throws an error if parentHash
does not match hash of current maximum block.
Parameters
Object
- Block data must be an object containingnumber
,timestamp
,hash
, andparentHash
exportBlocks
const blocks = clock.exportBlocks();
console.log(blocks); // [ { number, timestamp, hash }, . . . ]
Get array of block objects. Useful for exporting block data to be cached elsewhere.
Parameters
Object
- (optional) range to export, e.g{ min: 10337500 max: 10339000 }
(default all blocks)
Returns
Array
- Array of objects containing block data