-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement block timestamp helper functions
- add blockTime helper to get a block's timestamp or estimated mining date - implement a boolean helper that allows for different language depending on whether the block in question has been mined or not.
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const MILLISECONDS_IN_A_SECOND = 1000 | ||
|
||
export const blockTime = (eth) => | ||
async (blockNumber, showBlock = true, averageBlockTime = 13.965) => { | ||
let timestamp | ||
const currentBlock = await eth.getBlockNumber() | ||
|
||
if (currentBlock >= blockNumber) { | ||
timestamp = (await eth.getBlock(blockNumber)).timestamp * MILLISECONDS_IN_A_SECOND | ||
} else { | ||
const { timestamp: currentTimestamp } = await eth.getBlock(currentBlock) | ||
const blockDuration = (blockNumber - currentBlock) * averageBlockTime | ||
timestamp = (currentTimestamp + blockDuration) * MILLISECONDS_IN_A_SECOND | ||
} | ||
|
||
return { | ||
type: 'string', | ||
value: `${new Date(timestamp).toDateString()}${showBlock ? ` (block number: ${blockNumber})` : ''}` | ||
} | ||
} | ||
|
||
export const isBlockMined = (eth) => | ||
async (blockNumber) => { | ||
const currentBlock = await eth.getBlockNumber() | ||
return { | ||
type: 'bool', | ||
value: currentBlock >= blockNumber | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters