2023-10-27 14:13:44 -04:00
import bdkpython as bdk
import unittest
2024-02-08 16:02:15 -05:00
import os
2023-10-27 14:13:44 -04:00
2024-05-10 10:51:07 -04:00
SIGNET_ESPLORA_URL = " http://signet.bitcoindevkit.net "
TESTNET_ESPLORA_URL = " https://esplora.testnet.kuutamo.cloud "
2024-02-08 16:02:15 -05:00
class LiveWalletTest ( unittest . TestCase ) :
def tearDown ( self ) - > None :
if os . path . exists ( " ./bdk_persistence.db " ) :
os . remove ( " ./bdk_persistence.db " )
2023-10-27 14:13:44 -04:00
def test_synced_balance ( self ) :
descriptor : bdk . Descriptor = bdk . Descriptor (
2023-12-07 13:24:39 -06:00
" wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*) " ,
2024-05-10 10:51:07 -04:00
bdk . Network . SIGNET
2023-10-27 14:13:44 -04:00
)
2024-02-08 16:02:15 -05:00
wallet : bdk . Wallet = bdk . Wallet (
descriptor ,
None ,
" ./bdk_persistence.db " ,
2024-05-10 10:51:07 -04:00
bdk . Network . SIGNET
2024-02-08 16:02:15 -05:00
)
2024-05-10 10:51:07 -04:00
esplora_client : bdk . EsploraClient = bdk . EsploraClient ( url = SIGNET_ESPLORA_URL )
2024-05-05 20:08:33 -04:00
full_scan_request : bdk . FullScanRequest = wallet . start_full_scan ( )
update = esplora_client . full_scan (
full_scan_request = full_scan_request ,
stop_gap = 10 ,
parallel_requests = 1
2024-02-08 16:02:15 -05:00
)
wallet . apply_update ( update )
2024-05-08 11:33:35 -04:00
wallet . commit ( )
2024-02-08 16:02:15 -05:00
2024-05-10 12:45:56 -04:00
self . assertGreater (
2024-05-11 14:30:15 -05:00
wallet . get_balance ( ) . total . to_sat ( ) ,
2024-05-10 12:45:56 -04:00
0 ,
f " Wallet balance must be greater than 0! Please send funds to { wallet . reveal_next_address ( bdk . KeychainKind . EXTERNAL ) . address . as_string ( ) } and try again. "
)
2024-02-08 16:02:15 -05:00
print ( f " Transactions count: { len ( wallet . transactions ( ) ) } " )
transactions = wallet . transactions ( ) [ : 3 ]
for tx in transactions :
2024-04-06 21:25:43 -05:00
sent_and_received = wallet . sent_and_received ( tx . transaction )
print ( f " Transaction: { tx . transaction . txid ( ) } " )
2024-02-08 16:02:15 -05:00
print ( f " Sent { sent_and_received . sent } " )
print ( f " Received { sent_and_received . received } " )
2023-12-07 13:24:39 -06:00
2023-11-17 13:25:33 -05:00
def test_broadcast_transaction ( self ) :
descriptor : bdk . Descriptor = bdk . Descriptor (
" wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*) " ,
2024-05-10 10:51:07 -04:00
bdk . Network . SIGNET
2023-11-17 13:25:33 -05:00
)
2024-02-08 16:02:15 -05:00
wallet : bdk . Wallet = bdk . Wallet (
descriptor ,
None ,
" ./bdk_persistence.db " ,
2024-05-10 10:51:07 -04:00
bdk . Network . SIGNET
2024-02-08 16:02:15 -05:00
)
2024-05-10 10:51:07 -04:00
esplora_client : bdk . EsploraClient = bdk . EsploraClient ( url = SIGNET_ESPLORA_URL )
2024-05-05 20:08:33 -04:00
full_scan_request : bdk . FullScanRequest = wallet . start_full_scan ( )
update = esplora_client . full_scan (
full_scan_request = full_scan_request ,
stop_gap = 10 ,
parallel_requests = 1
2024-02-08 16:02:15 -05:00
)
wallet . apply_update ( update )
2024-05-08 11:33:35 -04:00
wallet . commit ( )
2024-02-08 16:02:15 -05:00
2024-05-10 12:45:56 -04:00
self . assertGreater (
2024-05-11 14:30:15 -05:00
wallet . get_balance ( ) . total . to_sat ( ) ,
2024-05-10 12:45:56 -04:00
0 ,
f " Wallet balance must be greater than 0! Please send funds to { wallet . reveal_next_address ( bdk . KeychainKind . EXTERNAL ) . address . as_string ( ) } and try again. "
)
2024-02-08 16:02:15 -05:00
recipient = bdk . Address (
2024-05-05 20:08:33 -04:00
address = " tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989 " ,
2024-05-10 10:51:07 -04:00
network = bdk . Network . SIGNET
2024-02-08 16:02:15 -05:00
)
2024-04-17 11:34:25 -04:00
psbt : bdk . Psbt = bdk . TxBuilder ( ) . add_recipient ( script = recipient . script_pubkey ( ) , amount = 4200 ) . fee_rate ( fee_rate = bdk . FeeRate . from_sat_per_vb ( 2 ) ) . finish ( wallet )
2024-02-08 16:02:15 -05:00
# print(psbt.serialize())
self . assertTrue ( psbt . serialize ( ) . startswith ( " cHNi " ) , " The PSBT should start with cHNi " )
walletDidSign = wallet . sign ( psbt )
self . assertTrue ( walletDidSign )
tx = psbt . extract_tx ( )
print ( f " Transaction Id: { tx . txid ( ) } " )
fee = wallet . calculate_fee ( tx )
print ( f " Transaction Fee: { fee } " )
fee_rate = wallet . calculate_fee_rate ( tx )
2024-04-01 14:56:19 -04:00
print ( f " Transaction Fee Rate: { fee_rate . to_sat_per_vb_ceil ( ) } sat/vB " )
2024-02-08 16:02:15 -05:00
2024-05-05 20:08:33 -04:00
esplora_client . broadcast ( tx )
2023-11-17 13:25:33 -05:00
2023-10-27 14:13:44 -04:00
if __name__ == ' __main__ ' :
unittest . main ( )