|
| 1 | +import 'package:cw_bitcoin/bitcoin_address_record.dart'; |
| 2 | +import 'package:cw_bitcoin/bitcoin_unspent.dart'; |
| 3 | +import 'package:cw_bitcoin/octojoin_transaction_builder.dart'; |
| 4 | +import 'package:cw_core/unspent_coins_info.dart'; |
| 5 | +import 'package:cw_core/utils/print_verbose.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'package:bitcoin_base/bitcoin_base.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + group('OctojoinTransactionBuilder E2E Logic Test', () { |
| 11 | + test('decomposeAmount successfully chunks into standard denominations', () { |
| 12 | + final amount = 300000; |
| 13 | + final denominations = OctojoinTransactionBuilder.decomposeAmount(amount); |
| 14 | + |
| 15 | + printV('Decomposed Amount: $amount sats'); |
| 16 | + printV('Denominations Result: $denominations'); |
| 17 | + |
| 18 | + expect(denominations.length, 2); |
| 19 | + expect(denominations.contains(200000), true); |
| 20 | + expect(denominations.contains(100000), true); |
| 21 | + }); |
| 22 | + |
| 23 | + test('selectUTXOs correctly isolates octojoin notes from unspents', () { |
| 24 | + final walletId = 'test_wallet'; |
| 25 | + |
| 26 | + final addrRecord1 = BitcoinAddressRecord('addr1', index: 0, type: SegwitAddresType.p2wpkh, network: BitcoinNetwork.testnet); |
| 27 | + final addrRecord2 = BitcoinAddressRecord('addr2', index: 1, type: SegwitAddresType.p2wpkh, network: BitcoinNetwork.testnet); |
| 28 | + final addrRecord3 = BitcoinAddressRecord('addr3', index: 2, type: SegwitAddresType.p2wpkh, network: BitcoinNetwork.testnet); |
| 29 | + |
| 30 | + final unspentCoins = [ |
| 31 | + BitcoinUnspent(addrRecord1, 'hash1', 150000, 0), |
| 32 | + BitcoinUnspent(addrRecord2, 'hash2', 150000, 0), |
| 33 | + BitcoinUnspent(addrRecord3, 'hash3', 50000, 0), |
| 34 | + ]; |
| 35 | + |
| 36 | + final unspentCoinsInfoValues = [ |
| 37 | + UnspentCoinsInfo(walletId: walletId, hash: 'hash1', address: 'addr1', value: 150000, vout: 0, isFrozen: false, isSending: true, noteRaw: 'Octojoin 1'), |
| 38 | + UnspentCoinsInfo(walletId: walletId, hash: 'hash2', address: 'addr2', value: 150000, vout: 0, isFrozen: false, isSending: true, noteRaw: 'octojoin 2'), |
| 39 | + UnspentCoinsInfo(walletId: walletId, hash: 'hash3', address: 'addr3', value: 50000, vout: 0, isFrozen: false, isSending: true, noteRaw: 'Normal TX'), |
| 40 | + ]; |
| 41 | + |
| 42 | + final selection = OctojoinTransactionBuilder.selectUTXOs( |
| 43 | + unspentCoins, |
| 44 | + unspentCoinsInfoValues, |
| 45 | + 3, |
| 46 | + walletId, |
| 47 | + 300000 |
| 48 | + ); |
| 49 | + |
| 50 | + final swapped = selection['swapped'] as List<BitcoinUnspent>; |
| 51 | + final other = selection['other'] as List<BitcoinUnspent>; |
| 52 | + final all = selection['all'] as List<BitcoinUnspent>; |
| 53 | + |
| 54 | + printV('--- UTXO Selection Pipeline ---'); |
| 55 | + printV('Swapped UTXOs Found: ${swapped.length}'); |
| 56 | + printV('Total Value Selected: ${selection['totalValue']}'); |
| 57 | + |
| 58 | + expect(swapped.length, 2); |
| 59 | + expect(other.length, 1); |
| 60 | + expect(all.length, 3); |
| 61 | + expect(selection['totalValue'], 350000); |
| 62 | + }); |
| 63 | + |
| 64 | + test('distributeOutputs seamlessly maps exact denominations to destination addresses', () { |
| 65 | + final addrs = ['addr1', 'addr2']; |
| 66 | + final denominations = [200000, 100000, 1000]; |
| 67 | + |
| 68 | + final outputsMap = OctojoinTransactionBuilder.distributeOutputs(denominations, addrs); |
| 69 | + |
| 70 | + printV('--- Final Output Structure Map ---'); |
| 71 | + outputsMap.forEach((key, value) { |
| 72 | + printV('Address: $key -> Value: $value sats'); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(outputsMap['addr1'], 201000); |
| 76 | + expect(outputsMap['addr2'], 100000); |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
0 commit comments