reaster / cardano_wallet_sdk

Targeting Flutter apps, the Cardano Wallet SDK is a high-level Dart library for managing cryptocurrency accounts & executing transactions on the blockchain.
Apache License 2.0
38 stars 18 forks source link

Building a wallet not async #14

Open rok5ek opened 2 years ago

rok5ek commented 2 years ago

Hi when I build the wallet the whole app UI freezes. Can you fix the wallet build method so it will be async?

reaster commented 2 years ago

There are two ways to update a wallet. You can build the wallet and sync it with the blockchain in one call:

Result<Wallet, String> result = await walletBuilder.buildAndSync();
if (result.isOk()) {
    var wallet = result.unwrap();
    print("${wallet.walletName}: ${wallet.balance}");
}   

or you can do it in two steps. First call build() which returns an empty wallet immediately. Then call update() to sync with the blockchain which is an async call:

Result<Wallet, String> result = walletBuilder.build();
Wallet wallet = result.unwrap();
Coin oldBalance = wallet.balance;
var result2 = await wallet.update();
result2.when(
    ok: (_) => print("old:$oldBalance lovelace, new: ${wallet.balance} lovelace"),
    err: (message) => print("Error: $message"),
);  

For better performance, you could wrap the update() call in an isolate following the pattern outlined here: https://github.com/dart-lang/samples/blob/master/isolates/bin/send_and_receive.dart