[1124] - Use Mempool For Fee Rate (#1171)

* Use mempool for fee rate

* Add minor logic change

* Restore carousel items
This commit is contained in:
Tom Smith 2024-05-15 16:26:49 +01:00 committed by GitHub
parent c6ab776091
commit 691818e779
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 33 deletions

View File

@ -1,17 +1,16 @@
import lndService from 'ln-service'
import lnd from '@/api/lnd'
const cache = new Map() const cache = new Map()
const expiresIn = 1000 * 30 // 30 seconds in milliseconds const expiresIn = 1000 * 30 // 30 seconds in milliseconds
async function fetchChainFeeRate () { async function fetchChainFeeRate () {
let chainFee = 0 const url = 'https://mempool.space/api/v1/fees/recommended'
try { const chainFee = await fetch(url)
const fee = await lndService.getChainFeeRate({ lnd }) .then((res) => res.json())
chainFee = fee.tokens_per_vbyte .then((body) => body.hourFee)
} catch (err) { .catch((err) => {
console.error('fetchChainFee', err) console.error('fetchChainFee', err)
} return 0
})
cache.set('fee', { fee: chainFee, createdAt: Date.now() }) cache.set('fee', { fee: chainFee, createdAt: Date.now() })
return chainFee return chainFee
} }

View File

@ -43,39 +43,38 @@ export function PriceProvider ({ price, children }) {
) )
} }
const STORAGE_KEY = 'asSats'
const DEFAULT_SELECTION = 'fiat'
const carousel = [
'fiat',
'yep',
'1btc',
'blockHeight',
'chainFee',
'halving'
]
export default function Price ({ className }) { export default function Price ({ className }) {
const [asSats, setAsSats] = useState(undefined) const [asSats, setAsSats] = useState(undefined)
const [pos, setPos] = useState(0)
useEffect(() => { useEffect(() => {
const satSelection = window.localStorage.getItem('asSats') const selection = window.localStorage.getItem(STORAGE_KEY) ?? DEFAULT_SELECTION
setAsSats(satSelection ?? 'fiat') setAsSats(selection)
setPos(carousel.findIndex((item) => item === selection))
}, []) }, [])
const { price, fiatSymbol } = usePrice() const { price, fiatSymbol } = usePrice()
const { height: blockHeight, halving } = useBlockHeight() const { height: blockHeight, halving } = useBlockHeight()
const { fee: chainFee } = useChainFee() const { fee: chainFee } = useChainFee()
// Options: yep, 1btc, blockHeight, undefined
// yep -> 1btc -> blockHeight -> chainFee -> undefined -> yep
const handleClick = () => { const handleClick = () => {
if (asSats === 'yep') { const nextPos = (pos + 1) % carousel.length
window.localStorage.setItem('asSats', '1btc')
setAsSats('1btc') window.localStorage.setItem(STORAGE_KEY, carousel[nextPos])
} else if (asSats === '1btc') { setAsSats(carousel[nextPos])
window.localStorage.setItem('asSats', 'blockHeight') setPos(nextPos)
setAsSats('blockHeight')
} else if (asSats === 'blockHeight') {
window.localStorage.setItem('asSats', 'chainFee')
setAsSats('chainFee')
} else if (asSats === 'chainFee') {
window.localStorage.setItem('asSats', 'halving')
setAsSats('halving')
} else if (asSats === 'halving') {
window.localStorage.removeItem('asSats')
setAsSats('fiat')
} else {
window.localStorage.setItem('asSats', 'yep')
setAsSats('yep')
}
} }
const compClassName = (className || '') + ' text-reset pointer' const compClassName = (className || '') + ' text-reset pointer'