Skip to content

Commit

Permalink
add default config methods for Blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
kumulynja committed May 30, 2024
1 parent 3ecadf0 commit af6ceb0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
43 changes: 26 additions & 17 deletions example/lib/bdk_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,33 @@ class BdkLibrary {
return descriptor;
}

Future<Blockchain> 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<Blockchain> 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,
),
),
);
}
}

Expand Down
19 changes: 13 additions & 6 deletions example/lib/simple_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,30 @@ class _SimpleWalletState extends State<SimpleWallet> {
});
}

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);
}

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}";
});
}

Expand Down
24 changes: 24 additions & 0 deletions lib/src/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ class Blockchain extends BdkBlockchain {
}
}

static Future<Blockchain> 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<Blockchain> 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<FeeRate> estimateFee({required int target, hint}) async {
Expand Down

0 comments on commit af6ceb0

Please sign in to comment.