fix: fee unit and batch fees

This commit is contained in:
Rafael Taranto 2024-01-25 15:28:17 +00:00
parent 575dbeabe5
commit d3533c44f0
2 changed files with 13 additions and 12 deletions

View file

@ -1,8 +1,8 @@
const axios = require("axios"); const axios = require("axios");
const getEstimateFeeBtc = () => { const getSatBEstimateFee = () => {
return axios.get('https://mempool.space/api/v1/fees/recommended') return axios.get('https://mempool.space/api/v1/fees/recommended')
.then(r => r.data.hourFee) .then(r => r.data.hourFee)
} }
module.exports = { getEstimateFeeBtc } module.exports = { getSatBEstimateFee }

View file

@ -1,6 +1,6 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const { getEstimateFeeBtc } = require('../../../blockexplorers/mempool.space') const { getSatBEstimateFee } = require('../../../blockexplorers/mempool.space')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error') const E = require('../../../error')
@ -56,14 +56,14 @@ function balance (account, cryptoCode, settings, operatorId) {
} }
function estimateFee () { function estimateFee () {
return getEstimateFeeBtc() return getSatBEstimateFee()
.then(result => BN(result)) .then(result => BN(result))
.catch(err => { .catch(err => {
logger.error('failure estimating fes', err) logger.error('failure estimating fes', err)
}) })
} }
function calculateFeeDiscount (feeMultiplier = 1) { function calculateFeeDiscount (feeMultiplier = 1, unitScale) {
// 0 makes bitcoind do automatic fee selection // 0 makes bitcoind do automatic fee selection
const AUTOMATIC_FEE = 0 const AUTOMATIC_FEE = 0
return estimateFee() return estimateFee()
@ -72,7 +72,9 @@ function calculateFeeDiscount (feeMultiplier = 1) {
logger.info('failure estimating fee, using bitcoind automatic fee selection') logger.info('failure estimating fee, using bitcoind automatic fee selection')
return AUTOMATIC_FEE return AUTOMATIC_FEE
} }
const newFee = estimatedFee.times(feeMultiplier) // transform from sat/vB to BTC/kvB and apply the multipler
const newFee = estimatedFee.shiftedBy(-unitScale+3).times(feeMultiplier)
console.log(newFee.toFixed())
if (newFee.lt(0.00001) || newFee.gt(0.1)) { if (newFee.lt(0.00001) || newFee.gt(0.1)) {
logger.info('fee outside safety parameters, defaulting to automatic fee selection') logger.info('fee outside safety parameters, defaulting to automatic fee selection')
return AUTOMATIC_FEE return AUTOMATIC_FEE
@ -86,11 +88,8 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8) const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeMultiplier)) .then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(it => { .then(() => fetch('settxfee', [newFee]))
const newFee = it.shiftedBy(-unitScale).toFixed(8)
return fetch('settxfee', [newFee])
})
.then(() => fetch('sendtoaddress', [toAddress, coins])) .then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))
@ -105,7 +104,7 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) { function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeMultiplier)) .then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(newFee => fetch('settxfee', [newFee])) .then(newFee => fetch('settxfee', [newFee]))
.then(() => _.reduce((acc, value) => ({ .then(() => _.reduce((acc, value) => ({
...acc, ...acc,
@ -209,6 +208,8 @@ function getTxHashesByAddress (cryptoCode, address) {
.then(_.map(({ hash }) => hash)) .then(_.map(({ hash }) => hash))
} }
calculateFeeDiscount(1, 8).then(console.log)
module.exports = { module.exports = {
balance, balance,
sendCoins, sendCoins,