From 46e34a78a2c3b0d4534d6509307f92a76b03388a Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Fri, 26 Apr 2024 10:03:24 +0100 Subject: [PATCH 1/2] feat: add lamassu bumpfee script --- bin/lamassu-btc-bumpfee | 44 +++++++++++++++++++++++++ lib/blockexplorers/mempool.space.js | 24 +++++++++----- lib/plugins/wallet/bitcoind/bitcoind.js | 1 + 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 bin/lamassu-btc-bumpfee diff --git a/bin/lamassu-btc-bumpfee b/bin/lamassu-btc-bumpfee new file mode 100644 index 00000000..8bf8cd04 --- /dev/null +++ b/bin/lamassu-btc-bumpfee @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +const inquirer = require('inquirer') + +const bitcoind = require('../lib/plugins/wallet/bitcoind/bitcoind') +const BN = require('../lib/bn') +const mempool = require('../lib/blockexplorers/mempool.space') + +const txId = process.argv[2] +if (!txId) { + console.error('Please provide a BTC transaction hash as input.') + process.exit(1) +} + +const bumpTransactionFee = async (txId) => { + const txData = await bitcoind.fetch('gettransaction', [txId, true, true]) + + const fee = new BN(txData.fee).abs().shiftedBy(8).decimalPlaces(0) + const size = txData.decoded.vsize + const satPerVb = fee.div(size) + + console.log(`Current fee: ${satPerVb.toFixed(2).toString()} sat/vB`) + + const recommendedFees = mempool.getSatBEstimateFees() + + console.log('Recommended fees (sat/vB):', recommendedFees) + + const { selectedFee } = await inquirer.prompt([ + { + type: 'list', + name: 'selectedFee', + message: 'Select a fee higher than the current one:', + choices: Object.entries(recommendedFees) + .filter(([_, value]) => satPerVb.gt(value)) + .map(([key, value]) => ({name: `${key}: ${value} sat/vB`, value})), + }, + ]) + + await bitcoind.fetch('bumpfee', [txId, {fee_rate: selectedFee}]) + + console.log(`Fee bumped to ${selectedFee.toFixed(2)} sat/vB`) +} + +bumpTransactionFee(txId) \ No newline at end of file diff --git a/lib/blockexplorers/mempool.space.js b/lib/blockexplorers/mempool.space.js index 389d6727..383c1c72 100644 --- a/lib/blockexplorers/mempool.space.js +++ b/lib/blockexplorers/mempool.space.js @@ -1,8 +1,16 @@ -const axios = require("axios"); - -const getSatBEstimateFee = () => { - return axios.get('https://mempool.space/api/v1/fees/recommended') - .then(r => r.data.hourFee) -} - -module.exports = { getSatBEstimateFee } \ No newline at end of file +const axios = require("axios"); + +const getSatBEstimateFee = () => { + return axios.get('https://mempool.space/api/v1/fees/recommended') + .then(r => r.data.hourFee) +} + +const getSatBEstimateFees = () => { + return axios.get('https://mempool.space/api/v1/fees/recommended') + .then(r => r.data) +} + +module.exports = { + getSatBEstimateFees, + getSatBEstimateFee +} \ No newline at end of file diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index ad979a5c..ba5d17ae 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -219,5 +219,6 @@ module.exports = { sendCoinsBatch, checkBlockchainStatus, getTxHashesByAddress, + fetch, SUPPORTS_BATCHING } From 1e6f762c2c3507e30dfba6dbb7e59729321ff9fe Mon Sep 17 00:00:00 2001 From: Rafael Date: Tue, 3 Dec 2024 17:16:52 +0000 Subject: [PATCH 2/2] fix: comparator, missing await, print new txid --- bin/lamassu-btc-bumpfee | 11 +++++++---- package.json | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bin/lamassu-btc-bumpfee b/bin/lamassu-btc-bumpfee index 8bf8cd04..bd17b3af 100644 --- a/bin/lamassu-btc-bumpfee +++ b/bin/lamassu-btc-bumpfee @@ -21,7 +21,7 @@ const bumpTransactionFee = async (txId) => { console.log(`Current fee: ${satPerVb.toFixed(2).toString()} sat/vB`) - const recommendedFees = mempool.getSatBEstimateFees() + const recommendedFees = await mempool.getSatBEstimateFees() console.log('Recommended fees (sat/vB):', recommendedFees) @@ -31,14 +31,17 @@ const bumpTransactionFee = async (txId) => { name: 'selectedFee', message: 'Select a fee higher than the current one:', choices: Object.entries(recommendedFees) - .filter(([_, value]) => satPerVb.gt(value)) + .filter(([_, value]) => satPerVb.lt(value)) .map(([key, value]) => ({name: `${key}: ${value} sat/vB`, value})), }, ]) - await bitcoind.fetch('bumpfee', [txId, {fee_rate: selectedFee}]) + const { txid } = await bitcoind.fetch('bumpfee', [txId, {fee_rate: selectedFee}]) - console.log(`Fee bumped to ${selectedFee.toFixed(2)} sat/vB`) + console.log(` +Fee bumped to ${selectedFee.toFixed(2)} sat/vB +Transaction ID: ${txid} +`) } bumpTransactionFee(txId) \ No newline at end of file diff --git a/package.json b/package.json index 9d4511c6..47798153 100644 --- a/package.json +++ b/package.json @@ -113,6 +113,7 @@ "lamassu-ofac-update": "./bin/lamassu-ofac-update", "lamassu-send-coins": "./bin/lamassu-send-coins", "lamassu-update-to-mnemonic": "./bin/lamassu-update-to-mnemonic", + "lamassu-btc-bumpfee": "./bin/lamassu-btc-bumpfee", "lamassu-update-wallet-nodes": "./bin/lamassu-update-wallet-nodes", "lamassu-configure-frontcamera": "./bin/lamassu-configure-frontcamera", "lamassu-ofac-update-sources": "./bin/lamassu-ofac-update-sources",