Bitcoin

How to automatically enable transaction fees in Python script code?

There is a great Python script that creates transactions to pay with BTC coins, but no commission functionality is written or added to the code. Please tell me how to add a fee. What should I change in my code?

from io import BytesIO
from ecc import *
from tx import *

pk = PrivateKey.parse("Kyyd********privkey********D5LHv")

pk.address()

# constructing an unsigned transaction

# input
prev_tx = bytes.fromhex("9087a21953d35be57b80cc4f8a9e96b8d66e8588ecafb2af8f97b1b71789e0ce")
prev_index = 0
tx_in = TxIn(prev_tx, prev_index, b'', 0xffffffff)
tx_in._script_pubkey = Tx.get_address_data(pk.address())('script_pubkey')
tx_in._value = 500000
tx_ins = ( tx_in )

# outputs
tx_outs = (
    # sending some money to 1ND7BjA2zqqq7Nk3rJDecBbpw6bL3KebbG3
    TxOut(100000, Tx.get_address_data("1ND7BjA2zqqq7Nk3rJDecBbpw6bL3KebbG3")('script_pubkey').serialize()),
    # change goes back to the same address
    TxOut(390000, Tx.get_address_data(pk.address())('script_pubkey').serialize())
)

tx = Tx(1, tx_ins, tx_outs, 0, testnet=True)

Related Articles

Back to top button