[1124] - Use Mempool For Fee Rate (#1171)
* Use mempool for fee rate * Add minor logic change * Restore carousel items
This commit is contained in:
parent
c6ab776091
commit
691818e779
|
@ -1,17 +1,16 @@
|
|||
import lndService from 'ln-service'
|
||||
import lnd from '@/api/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) {
|
||||
const url = 'https://mempool.space/api/v1/fees/recommended'
|
||||
const chainFee = await fetch(url)
|
||||
.then((res) => res.json())
|
||||
.then((body) => body.hourFee)
|
||||
.catch((err) => {
|
||||
console.error('fetchChainFee', err)
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
cache.set('fee', { fee: chainFee, createdAt: Date.now() })
|
||||
return chainFee
|
||||
}
|
||||
|
|
|
@ -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 }) {
|
||||
const [asSats, setAsSats] = useState(undefined)
|
||||
const [pos, setPos] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
const satSelection = window.localStorage.getItem('asSats')
|
||||
setAsSats(satSelection ?? 'fiat')
|
||||
const selection = window.localStorage.getItem(STORAGE_KEY) ?? DEFAULT_SELECTION
|
||||
setAsSats(selection)
|
||||
setPos(carousel.findIndex((item) => item === selection))
|
||||
}, [])
|
||||
|
||||
const { price, fiatSymbol } = usePrice()
|
||||
const { height: blockHeight, halving } = useBlockHeight()
|
||||
const { fee: chainFee } = useChainFee()
|
||||
|
||||
// Options: yep, 1btc, blockHeight, undefined
|
||||
// yep -> 1btc -> blockHeight -> chainFee -> undefined -> yep
|
||||
const handleClick = () => {
|
||||
if (asSats === 'yep') {
|
||||
window.localStorage.setItem('asSats', '1btc')
|
||||
setAsSats('1btc')
|
||||
} else if (asSats === '1btc') {
|
||||
window.localStorage.setItem('asSats', 'blockHeight')
|
||||
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 nextPos = (pos + 1) % carousel.length
|
||||
|
||||
window.localStorage.setItem(STORAGE_KEY, carousel[nextPos])
|
||||
setAsSats(carousel[nextPos])
|
||||
setPos(nextPos)
|
||||
}
|
||||
|
||||
const compClassName = (className || '') + ' text-reset pointer'
|
||||
|
|
Loading…
Reference in New Issue