You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
70 lines
1.5 KiB
/*!
|
|
* lib/bitcoind-rpc/fees.js
|
|
* Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved.
|
|
*/
|
|
'use strict'
|
|
|
|
const util = require('../util')
|
|
const errors = require('../errors')
|
|
const Logger = require('../logger')
|
|
const network = require('../bitcoin/network')
|
|
const keys = require('../../keys')[network.key]
|
|
const { createRpcClient } = require('./rpc-client')
|
|
const latestBlock = require('./latest-block')
|
|
|
|
|
|
/**
|
|
* A singleton providing information about network fees
|
|
*/
|
|
class Fees {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
constructor() {
|
|
this.block = -1
|
|
this.targets = [2, 4, 6, 12, 24]
|
|
this.fees = {}
|
|
this.feeType = keys.bitcoind.feeType
|
|
|
|
this.rpcClient = createRpcClient()
|
|
|
|
this.refresh()
|
|
}
|
|
|
|
/**
|
|
* Refresh and return the current fees
|
|
* @returns {Promise<object>}
|
|
*/
|
|
async getFees() {
|
|
try {
|
|
if (latestBlock.height > this.block)
|
|
await this.refresh()
|
|
|
|
return this.fees
|
|
|
|
} catch(err) {
|
|
return Promise.reject(errors.generic.GEN)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Refresh the current fees
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async refresh() {
|
|
await util.parallelCall(this.targets, async tgt => {
|
|
try {
|
|
const level = await this.rpcClient.estimatesmartfee({ conf_target: tgt, estimate_mode: this.feeType })
|
|
this.fees[tgt] = (level.errors && level.errors.length > 0) ? 0 : Math.round(level.feerate * 1e5)
|
|
} catch(e) {
|
|
Logger.error(e, 'Bitcoind RPC : Fees.refresh()')
|
|
this.fees[tgt] = 0
|
|
}
|
|
})
|
|
this.block = latestBlock.height
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new Fees()
|
|
|