Skip to content

Commit

Permalink
Get current block height
Browse files Browse the repository at this point in the history
  • Loading branch information
tombeynon committed Apr 6, 2022
1 parent 71382bb commit 078570f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
12 changes: 11 additions & 1 deletion chains/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ function Chain(client, data) {
const assets = assetlist && assetlist.assets.map(el => ChainAsset(el));
const apis = ChainApis(client, path, chain.apis || {});

function getBlockHeight(){
return apis.bestHeight()
}

function baseAsset(){
return assets && assets[0]
}

return {
path: path,
chainId: chain.chain_id,
Expand All @@ -15,7 +23,9 @@ function Chain(client, data) {
assets,
apis,
data,
...data
...data,
getBlockHeight,
baseAsset
};
}

Expand Down
15 changes: 14 additions & 1 deletion chains/chainApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ function ChainApis(client, path, apis) {
return best && best.address
}

async function bestHeight(type) {
let urls
if(type){
urls = Object.values(await current(type))
}else{
urls = Object.values(await current()).reduce((sum, urls) => {
return sum.concat(Object.values(urls))
}, [])
}
return Math.max(...urls.map(el => el.blockHeight).filter(Number.isFinite))
}

async function bestUrls(type) {
let urls = Object.values(await current(type)).filter(el => el.available)
const bestHeight = Math.max(...urls.map(el => el.blockHeight).filter(Number.isFinite))
Expand Down Expand Up @@ -50,12 +62,13 @@ function ChainApis(client, path, apis) {
return {}
}
const currentUrls = await client.json.get('health:' + path, '$')
return currentUrls[type] || {}
return type ? (currentUrls[type] || {}) : currentUrls
}

return {
bestAddress,
bestUrls,
bestHeight,
apis,
current
}
Expand Down
13 changes: 7 additions & 6 deletions chains/chainsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Router from 'koa-router';
import { renderJson } from '../utils.js';

function ChainsController(registry) {
function summary(chain) {
async function summary(chain) {
const { chain_name, network_type, pretty_name, chain_id, status } = chain.chain;
const baseAsset = chain.assets && chain.assets[0];
const baseAsset = chain.baseAsset()
return {
name: chain_name,
path: chain.path,
Expand All @@ -16,6 +16,7 @@ function ChainsController(registry) {
symbol: baseAsset && baseAsset.symbol,
coingecko_id: baseAsset && baseAsset.coingecko_id,
image: baseAsset && baseAsset.image,
height: await chain.getBlockHeight(),
apis: chain.apis
};
}
Expand All @@ -34,15 +35,15 @@ function ChainsController(registry) {
commit: commit.oid,
timestamp: commit.commit.author.timestamp
},
chains: chains.map(chain => {
return summary(chain);
})
chains: await Promise.all(chains.map(async chain => {
return await summary(chain);
}))
});
});

router.get('/:chain', async (ctx, next) => {
const chain = await registry.getChain(ctx.params.chain);
renderJson(ctx, chain && summary(chain));
renderJson(ctx, chain && await summary(chain));
});

router.get('/:chain/:dataset', async (ctx, next) => {
Expand Down
10 changes: 7 additions & 3 deletions status/statusController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ const StatusController = (client, registry) => {
return {
chains: await Promise.all(chains.map(async chain => {
const status = await chainStatus(chain)
return _.pick(status, ['name', 'available', 'rpc.available', 'rpc.best', 'rest.available', 'rest.best'])
return _.pick(status, ['name', 'height', 'available', 'rpc.available', 'rpc.height', 'rpc.best', 'rest.available', 'rest.height', 'rest.best'])
}))
}
}

const chainStatus = async (chain) => {
const apis = chain.apis
const data = {
name: chain.name,
height: await apis.bestHeight(),
}
return ['rpc', 'rest'].reduce(async (asyncSum, type) => {
const sum = await asyncSum
const available = await apis.bestAddress(type)
sum.name = chain.name
sum.available = sum.available === false ? false : !!available
sum[type] = {
available: !!available,
height: await apis.bestHeight(type),
best: await apis.bestUrls(type),
current: await apis.current(type)
}
return sum
}, {})
}, data)
}

function routes() {
Expand Down

0 comments on commit 078570f

Please sign in to comment.