Skip to content

Commit

Permalink
Merge pull request #134 from kumulynja/default-ltbl-esplora
Browse files Browse the repository at this point in the history
add default config methods for Blockchain
  • Loading branch information
BitcoinZavior authored Jul 16, 2024
2 parents 821fe90 + 57a34d5 commit 78c55c5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
13 changes: 7 additions & 6 deletions example/lib/bdk_library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class BdkLibrary {
}

Future<Blockchain> initializeBlockchain() async {
return await Blockchain.create(
config: const BlockchainConfig.esplora(
config: EsploraConfig(
baseUrl: 'https://mutinynet.com/api', stopGap: 10)));
return Blockchain.createMutinynet();
}

Future<Wallet> restoreWallet(Descriptor descriptor) async {
Expand Down Expand Up @@ -95,7 +92,11 @@ class BdkLibrary {
}

sendBitcoin(
Blockchain blockchain, Wallet aliceWallet, String addressStr) async {
Blockchain blockchain,
Wallet aliceWallet,
String addressStr,
int amountSat,
) async {
try {
final txBuilder = TxBuilder();
final address = await Address.fromString(
Expand All @@ -104,7 +105,7 @@ class BdkLibrary {
final script = await address.scriptPubkey();
final feeRate = await estimateFeeRate(25, blockchain);
final (psbt, _) = await txBuilder
.addRecipient(script, 750)
.addRecipient(script, amountSat)
.feeRate(feeRate.satPerVb)
.finish(aliceWallet);
final isFinalized = await aliceWallet.sign(psbt: psbt);
Expand Down
25 changes: 16 additions & 9 deletions example/lib/simple_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class _SimpleWalletState extends State<SimpleWallet> {

generateMnemonicKeys() async {
final res = await lib.createMnemonic();
final mnemonic = await res.asString();
setState(() {
displayText = res.toString();
displayText = mnemonic;
});
if (kDebugMode) {
print(await res.asString());
print(mnemonic);
}
}

Expand All @@ -49,11 +50,13 @@ class _SimpleWalletState extends State<SimpleWallet> {
}

getNewAddress() async {
final res = (await (await lib.getAddress(aliceWallet)).address.asString());
debugPrint(res);
final addressInfo = await lib.getAddress(aliceWallet);
final address = await addressInfo.address.asString();

debugPrint(address);

setState(() {
displayText = "Address: $res";
displayText = "Address: $address \n Index: ${addressInfo.index}";
});
}

Expand Down Expand Up @@ -152,9 +155,13 @@ class _SimpleWalletState extends State<SimpleWallet> {
}
}

sendBit() async {
sendBit(int amountSat) async {
await lib.sendBitcoin(
blockchain!, aliceWallet, "tb1qyhssajdx5vfxuatt082m9tsfmxrxludgqwe52f");
blockchain!,
aliceWallet,
"tb1qyhssajdx5vfxuatt082m9tsfmxrxludgqwe52f",
amountSat,
);
}

@override
Expand Down Expand Up @@ -286,9 +293,9 @@ class _SimpleWalletState extends State<SimpleWallet> {
fontWeight: FontWeight.w800),
)),
TextButton(
onPressed: () => sendBit(),
onPressed: () => sendBit(100000),
child: const Text(
'Press to send 1200 satoshi',
'Press to send 100k sats',
style: TextStyle(
color: Colors.indigoAccent,
fontSize: 12,
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> createMutinynet({
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> createTestnet({
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 78c55c5

Please sign in to comment.