stacker.news/api/resolvers/blockHeight.js

28 lines
702 B
JavaScript
Raw Permalink Normal View History

import { isServiceEnabled } from '@/lib/sndev'
import { cachedFetcher } from '@/lib/fetch'
import { getHeight } from 'ln-service'
2024-10-09 00:26:29 +00:00
const getBlockHeight = cachedFetcher(async function fetchBlockHeight ({ lnd }) {
try {
const { current_block_height: height } = await getHeight({ lnd })
return height
} catch (err) {
console.error('getBlockHeight', err)
return 0
}
}, {
maxSize: 1,
cacheExpiry: 60 * 1000, // 1 minute
2024-10-06 22:58:46 +00:00
forceRefreshThreshold: 0,
keyGenerator: () => 'getBlockHeight'
})
export default {
Query: {
blockHeight: async (parent, opts, { lnd }) => {
if (!isServiceEnabled('payments')) return 0
return await getBlockHeight({ lnd }) || 0
}
}
}