getStrkBalance function

Future<double> getStrkBalance({
  1. required Provider provider,
  2. required Felt accountAddress,
})

Implementation

Future<double> getStrkBalance(
    {required sp.Provider provider, required s.Felt accountAddress}) async {
  final strkContractAddress = s.Felt.fromHexString(
      '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d');
  const strkDecimals = 18;
  final response = await provider.call(
    request: sp.FunctionCall(
      contractAddress: strkContractAddress,
      entryPointSelector: s.getSelectorByName('balanceOf'),
      calldata: [accountAddress],
    ),
    blockId: const sp.BlockId.blockTag("latest"),
  );
  return response.when<double>(
    error: (error) {
      throw Exception(error);
    },
    result: (result) {
      final strkBalance = s.Uint256.fromFeltList(result).toBigInt() /
          BigInt.from(10).pow(strkDecimals);
      return strkBalance;
    },
  );
}