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