Bitcoin

How to get transaction fee information using Python on Electrum server?

You can get the raw transaction and parse it using: electrum and bitcoinlib Fairly easily:

import bitcoinlib

from electrum.daemon import Daemon
from electrum.simple_config import SimpleConfig

class TxInfoClient:
    """Transaction info client"""

    def __init__(self):
        common_config = parse_config()
        electrum_config = SimpleConfig(
            "testnet": False, "server": common_config("electrum")("server")
        )
        self.daemon = Daemon(electrum_config, listen_jsonrpc=False)

    def get_tx(self, tx_id):
        """Returns a bitcoinlib.transactions.Transaction:
        https://bitcoinlib.readthedocs.io/en/latest/source/bitcoinlib.transactions.html#bitcoinlib.transactions.Transaction
        """
        raw_tx = self.daemon.network.run_from_another_thread(
            self.daemon.network.get_transaction(tx_id)
        )
        return bitcoinlib.transactions.Transaction.parse_hex(raw_tx)

However, since Bitcoin Tx fees cannot be calculated from a single transaction, the resulting transaction object will not contain any fee information.

You can manually look through and query the input transactions as suggested in that link. However, I’m looking for a simple solution using a well-built and maintained library for this.

Is there a way to make this happen using: bitcoinlib and electrumOr is it another mainstream library?

Related Articles

Back to top button