Skip to content

Commit

Permalink
implement block timestamp helper functions
Browse files Browse the repository at this point in the history
- 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
topocount committed Jan 9, 2020
1 parent e306a11 commit 22cbe9a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/helpers/blockTime.js
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
}
}
9 changes: 7 additions & 2 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import formatPct from './formatPct'
import tokenAmount from './tokenAmount'
import transformTime from './transformTime'
import radspec from './radspec'
import { blockTime, isBlockMined } from './blockTime'

const defaultHelpers = {
formatDate,
Expand All @@ -14,7 +15,9 @@ const defaultHelpers = {
formatPct,
fromHex,
radspec,
echo
echo,
blockTime,
isBlockMined
}

export {
Expand All @@ -27,5 +30,7 @@ export {
fromHex,
radspec,
transformTime,
tokenAmount
tokenAmount,
blockTime,
isBlockMined
}
12 changes: 12 additions & 0 deletions test/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,18 @@ const cases = [
source: "`_bool ? 'h' + _var + 'o' : 'bye'`",
bindings: { _bool: bool(true), _var: string('ell') }
}, 'hello'],
[{
source: 'get a past date: `@blockTime(block, showBlock)`',
bindings: { block: int('8765432'), showBlock: bool(false) }
}, 'get a past date: Fri Oct 18 2019'],
[{
source: 'get a future date: `@blockTime(block)`',
bindings: { block: int('20976543') }
}, 'get a future date: Wed Mar 19 2025 (block number: 20976543)'],
[{
source: 'see if block is mined: `@isBlockMined(block)`',
bindings: { block: int('98765430') }
}, 'see if block is mined: false'],

// External calls with multiple return values
[{
Expand Down

0 comments on commit 22cbe9a

Please sign in to comment.