stacker.news/api/resolvers/chainFee.js
st4rgut24 e9a5d22a6e
Add chain fees to price carousel (#658)
* add chain fees to price carousel

* restore check for valid carousel values

* add nym to contributors

---------

Co-authored-by: stargut <stargut@starguts-MacBook-Pro.local>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-12-20 16:06:22 -06:00

38 lines
898 B
JavaScript

import lndService from 'ln-service'
import lnd from '../lnd'
const cache = new Map()
const expiresIn = 1000 * 30 // 30 seconds in milliseconds
async function fetchChainFeeRate () {
let chainFee = 0
try {
const fee = await lndService.getChainFeeRate({ lnd })
chainFee = fee.tokens_per_vbyte
} catch (err) {
console.error('fetchChainFee', err)
}
cache.set('fee', { fee: chainFee, createdAt: Date.now() })
return chainFee
}
async function getChainFeeRate () {
if (cache.has('fee')) {
const { fee, createdAt } = cache.get('fee')
const expired = createdAt + expiresIn < Date.now()
if (expired) fetchChainFeeRate().catch(console.error) // update cache
return fee
} else {
fetchChainFeeRate().catch(console.error)
}
return 0
}
export default {
Query: {
chainFee: async (parent, opts, ctx) => {
return await getChainFeeRate()
}
}
}