Creating bitcoin transactions using bitcoinlib
Just found out stack overflow had a site for bitcoin anyways... I am here today to look for an answer of a long standing question of mine, I have been trying to make a python file which uses bitcoinlib to send a transaction with 2 outputs, 1 to a selected wallet and 1 to a fee collector wallet. When I run my code it says "Error occurred: Create transaction: No unspent transaction outputs found or no key available for UTXO's".
I have tried looking around for help, asked AI, sent funds to all accolated address of the private key (legacy, segwit, ect) to ensure it had funds and still says the same.
This is the function im using :
import random
import string
from bitcoinlib.wallets import Wallet, wallet_delete
from bitcoinlib.transactions import Output
from bitcoinlib.keys import Key
def send_with_fee(wif_key, recipient_address, amount_btc):
"""
Send BTC to a user-specified address and include a small service fee in a batch transaction.
:param wif_key: WIF private key (Testnet)
:param recipient_address: Destination address from the user
:param amount_btc: BTC to send to the recipient (not including fee)
"""
fee_address = "tb1pts2mvh4yhvj0ajr8qpqtkr2xqd9v9rupaa42acfqzjqstyfcx09qcrvepf"
fee_percent = 0.001
fee_amount_btc = max(0.0001, round(float(amount_btc) * fee_percent, 8))
# Convert BTC to satoshis
amount_btc_satoshis = int(float(amount_btc) * 1e8)
fee_amount_btc_satoshis = int(fee_amount_btc * 1e8)
wallet_name = "wallet_" + ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
# Clean up any existing wallet with same name
try:
wallet_delete(wallet_name)
except Exception as e:
print(f"WWarning: {e}")
try:
key = Key(import_key=wif_key, network='testnet')
w = Wallet.create(wallet_name, keys=key, network='testnet')
outputs = [
(recipient_address, amount_btc_satoshis), # Use the integer value for amount
(fee_address, fee_amount_btc_satoshis) # Use the integer value for fee
]
tx = w.transaction_create(outputs, fee=None, replace_by_fee=True)
print(f"💌 Sent {amount_btc} BTC to {recipient_address}")
print(f"💎 Service Fee: {fee_amount_btc} BTC to {fee_address}")
print(f"🎉 TXID: {tx.txid}")
return tx
except Exception as e:
print(f"Error occurred: {e}")
from Recent Questions - Bitcoin Stack Exchange https://ift.tt/9xE2toy
via IFTTT