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"
print(f"
print(f"
return tx
except Exception as e:
print(f"Error occurred: {e}")
Подробнее здесь: https://stackoverflow.com/questions/796 ... bitcoinlib