forked from deezy-inc/sat-hunter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfees.js
More file actions
36 lines (33 loc) · 1.21 KB
/
fees.js
File metadata and controls
36 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const axios = require('axios')
const FEE_PREF = process.env.FEE_PREFERENCE || 'medium'
if (FEE_PREF !== 'high' && FEE_PREF !== 'medium' && FEE_PREF !== 'low') {
throw new Error('FEE_PREFERENCE must be one of: high, medium, low')
}
// Get fee rate from mempool.space
async function get_fee_rate() {
if (process.env.AUTO_RBF) {
return get_min_next_block_fee_rate()
}
const { data } = await axios.get('https://mempool.space/api/v1/fees/recommended').catch(err => {
console.error(err)
return { data: {} }
})
if (FEE_PREF === 'high') return data.fastestFee
if (FEE_PREF === 'medium') return data.halfHourFee
if (FEE_PREF === 'low') return data.hourFee
throw new Error('FEE_PREFERENCE must be one of: high, medium, low')
}
async function get_min_next_block_fee_rate() {
const { data } = await axios.get('https://mempool.space/api/v1/fees/mempool-blocks').catch(err => {
console.error(err)
return { }
})
if (!data) {
throw new Error('Could not get mempool blocks')
}
// We add 3 right now because we want to be sure we get into the next block.
return Math.round((data[0].feeRange[0] + 3) * 10) / 10
}
module.exports = {
get_fee_rate
}