Example 6


Rust adjustments for Example "Check Balance".


What adjustments do I need to make in Rust?

In summary, here are the steps you need to take to create the API function.

Cargo.toml

There is no need to add any crates. It's the same as in Example 5.

api.rs - Used Paths

There is no need to add any path. It's the same as in Example 5.

api.rs - Struct BaseCoinBalance

This struct bundles some information about total and available amounts. Through code generation of the Flutter-Rust-Bridge, the struct is translated into a Flutter class and becomes part of the bridge_definitions.dart file.

Therefore, it is not necessary to explicitly create it in Flutter! On the other hand, this means that the code generator needs to be executed once for the class to be seamlessly used in Flutter without any errors.

#[derive(Debug, Clone)]
pub struct BaseCoinBalance {
    /// Total amount
    pub total: u64,
    /// Balance that can currently be spent
    pub available: u64,
}

api.rs - Function check_balance()

#[allow(dead_code)]
pub fn check_balance(wallet_info: WalletInfo) -> Result<BaseCoinBalance> {
    let rt = Runtime::new().unwrap();
    rt.block_on(async {
        let stronghold_filepath = wallet_info.stronghold_filepath;
        env::set_current_dir(&stronghold_filepath).ok();

        // Create the account manager
        let manager = AccountManager::builder().finish().await?;

        // Get the account we generated with `create_wallet_account`
        let account = manager.get_account((&wallet_info.alias).to_string()).await?;

        // Sync and get the balance
        let _account_balance = account.sync(None).await?;
        // If already synced, just get the balance
        let account_balance = account.balance().await?;

        //let base_coin_balance = account_balance.base_coin;
        let base_coin_balance = BaseCoinBalance {
            total: account_balance.base_coin.total,
            available: account_balance.base_coin.available,
        };
        //let total = account_balance.base_coin.total;
        //println!("{:?}", account_balance);

        //Ok(total.to_string())
        //Ok(base_coin_balance)
        Ok(base_coin_balance)
    })
}

Checks using cargo build


All checks (-> except iOS Simulator) should work without any issue. Please also refer to the explanations of Example 3 and the corresponding video (2023-09-03: ToDo).


To examine the various targets, you should navigate from playground_app root directory to the rust directory:

cd rust

Then, within the rust directory, excute the following commands.

Android

If you haven't already, install the cargo-ndk command using:

cargo install cargo-ndk

I only check the ABI arm64-v8a.

a) If you've had NO problems with the 3rd party library libsodium, use the command:

cargo ndk -t arm64-v8a build

b) If you've HAD problems with the 3rd party library libsodium, use the command:

SODIUM_LIB_DIR="/path/to/libsodium" SODIUM_SHARED=1 cargo ndk -t arm64-v8a build

e.g.

SODIUM_LIB_DIR="/Users/yourname/playground_app/android/app/src/main/jniLibs/arm64-v8a" SODIUM_SHARED=1 cargo ndk -t arm64-v8a build

-> Why do you need SODIUM_LIB_DIR and SODIUM_SHARED here?

macOS

cargo build --target aarch64-apple-darwin

iOS Simulator

This check will fail, please refer to the explanations of Example 3 (Libsodium).

cargo build --target aarch64-apple-ios-sim

iOS Device

cargo build --target aarch64-apple-ios