balance method

Future<CustomUnitBalance> balance(
  1. XCBAddress account, {
  2. String? unit,
  3. BlockNum? atBlock,
})

Reads canonical and calculated balances at the same block reference.

Implementation

Future<CustomUnitBalance> balance(
  XCBAddress account, {
  String? unit,
  BlockNum? atBlock,
}) async {
  final resolvedUnit =
      unit?.trim().isNotEmpty == true
          ? unit!.trim()
          : await preferredUnit(atBlock: atBlock);
  if (resolvedUnit.isEmpty) {
    throw StateError('The token contract did not provide a preferred unit');
  }
  final values = await Future.wait<BigInt>([
    canonicalBalance(account, atBlock: atBlock),
    balanceOfUnit(account, resolvedUnit, atBlock: atBlock),
  ]);
  return CustomUnitBalance(
    canonicalAmount: values[0],
    unitAmount: values[1],
    unit: resolvedUnit,
  );
}