From af6ceb01558af43e22c0f54ee3554b1cfd26ff7e Mon Sep 17 00:00:00 2001 From: kumulynja Date: Thu, 30 May 2024 19:36:07 +0200 Subject: [PATCH] add default config methods for Blockchain --- example/lib/bdk_library.dart | 43 ++++++++++++++++++++-------------- example/lib/simple_wallet.dart | 19 ++++++++++----- lib/src/root.dart | 24 +++++++++++++++++++ 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/example/lib/bdk_library.dart b/example/lib/bdk_library.dart index dbe57859..89faa7b9 100644 --- a/example/lib/bdk_library.dart +++ b/example/lib/bdk_library.dart @@ -19,24 +19,33 @@ class BdkLibrary { return descriptor; } - Future initializeBlockchain(bool isElectrumBlockchain) async { - if (isElectrumBlockchain) { - final blockchain = await Blockchain.create( - config: const BlockchainConfig.esplora( - config: EsploraConfig( - baseUrl: 'https://blockstream.info/testnet/api', - stopGap: 10))); - return blockchain; + Future initializeBlockchain({ + bool isElectrumBlockchain = false, + bool useTestnetDefaults = false, + }) async { + if (useTestnetDefaults) { + return await Blockchain.createWithTestnetDefaults(); + } else if (isElectrumBlockchain) { + return await Blockchain.create( + config: const BlockchainConfig.electrum( + config: ElectrumConfig( + stopGap: 10, + timeout: 5, + retry: 5, + url: "ssl://electrum.blockstream.info:60002", + validateDomain: true, + ), + ), + ); } else { - final blockchain = await Blockchain.create( - config: const BlockchainConfig.electrum( - config: ElectrumConfig( - stopGap: 10, - timeout: 5, - retry: 5, - url: "ssl://electrum.blockstream.info:60002", - validateDomain: true))); - return blockchain; + return await Blockchain.create( + config: const BlockchainConfig.esplora( + config: EsploraConfig( + baseUrl: 'https://blockstream.info/testnet/api', + stopGap: 10, + ), + ), + ); } } diff --git a/example/lib/simple_wallet.dart b/example/lib/simple_wallet.dart index 02f1d0fd..e26859f5 100644 --- a/example/lib/simple_wallet.dart +++ b/example/lib/simple_wallet.dart @@ -43,13 +43,20 @@ class _SimpleWalletState extends State { }); } - initBlockchain(bool isElectrumBlockchain) async { - blockchain = await lib.initializeBlockchain(isElectrumBlockchain); + initBlockchain({ + bool isElectrumBlockchain = false, + bool useTestnetDefaults = false, + }) async { + blockchain = await lib.initializeBlockchain( + isElectrumBlockchain: isElectrumBlockchain, + useTestnetDefaults: useTestnetDefaults, + ); } sync() async { if (blockchain == null) { - await initBlockchain(false); + // Initialize blockchain with default testnet values and esplora server + await initBlockchain(useTestnetDefaults: true); } await lib.sync(blockchain!, aliceWallet); } @@ -57,9 +64,9 @@ class _SimpleWalletState extends State { getNewAddress() async { final res = (await lib.getAddress(aliceWallet)); debugPrint(await res.address.asString()); - setState(() async { - displayText = - "Address: ${await res.address.asString()} \n Index: ${res.index}"; + final address = await res.address.asString(); + setState(() { + displayText = "Address: $address \n Index: ${res.index}"; }); } diff --git a/lib/src/root.dart b/lib/src/root.dart index 93e55c0a..10e20b12 100644 --- a/lib/src/root.dart +++ b/lib/src/root.dart @@ -61,6 +61,30 @@ class Blockchain extends BdkBlockchain { } } + static Future createWithMutinynetDefaults({ + int stopGap = 20, + }) async { + final config = BlockchainConfig.esplora( + config: EsploraConfig( + baseUrl: 'https://mutinynet.ltbl.io/api', + stopGap: stopGap, + ), + ); + return create(config: config); + } + + static Future createWithTestnetDefaults({ + int stopGap = 20, + }) async { + final config = BlockchainConfig.esplora( + config: EsploraConfig( + baseUrl: 'https://testnet.ltbl.io/api', + stopGap: stopGap, + ), + ); + return create(config: config); + } + ///Estimate the fee rate required to confirm a transaction in a given target of blocks @override Future estimateFee({required int target, hint}) async {