RootSoft / algorand-dart

Unofficial community SDK to interact with the Algorand network, for Dart & Flutter
MIT License
36 stars 16 forks source link

How to Get Account Balances, Asset Balances #25

Closed AteqEjaz closed 2 years ago

AteqEjaz commented 2 years ago

Hi,

Thanks so much for this very helpful Dart Plugin 100/100 :) - Actually i'm using this plugin in my Flutter app and it is working great.

I went through the Account Documentation but couldt found any way to get Account Balance and Asset balance.

Can you please help me in this regard how we can get Account Total balance and Asset balance ?

Thanks.

2i2i commented 2 years ago
Future<List<AssetHolding>> getAssetHoldings(
      {required String address, required AlgorandNet net}) async {
    final balanceALGOFuture = algorandLib.client[net]!.getBalance(address);

    final accountInfoFuture =
        algorandLib.client[net]!.getAccountByAddress(address);

    final futureResults =
        await Future.wait([balanceALGOFuture, accountInfoFuture]);

    final balanceALGO = futureResults[0];
    final assetHoldings = (futureResults[1] as AccountInformation).assets;

    final algoAssetHolding = AssetHolding(
        amount: balanceALGO, assetId: 0, creator: '', isFrozen: false);

    return [algoAssetHolding, ...assetHoldings];
  }

with this

class AlgorandLib {
  static const Map<AlgorandNet, String> API_URL = {
    AlgorandNet.mainnet: AlgoExplorer.MAINNET_ALGOD_API_URL,
    AlgorandNet.testnet: AlgoExplorer.TESTNET_ALGOD_API_URL,
    AlgorandNet.betanet: AlgoExplorer.BETANET_ALGOD_API_URL,
  };
  static const Map<AlgorandNet, String> INDEXER_URL = {
    AlgorandNet.mainnet: AlgoExplorer.MAINNET_INDEXER_API_URL,
    AlgorandNet.testnet: AlgoExplorer.TESTNET_INDEXER_API_URL,
    AlgorandNet.betanet: AlgoExplorer.BETANET_INDEXER_API_URL,
  };
  static const Map<AlgorandNet, String> API_KEY = {
    AlgorandNet.mainnet: '',
    AlgorandNet.testnet: '',
    AlgorandNet.betanet: '',
  };
  // static const Map<AlgorandNet, String> API_KEY = {
  //   AlgorandNet.mainnet: '',
  //   AlgorandNet.testnet: '',
  //   AlgorandNet.betanet: '',
  // };
  AlgorandLib() {
    for (final net in [AlgorandNet.mainnet, AlgorandNet.testnet]) {
      client[net] = Algorand(
          algodClient:
              AlgodClient(apiUrl: API_URL[net]!, apiKey: API_KEY[net]!),
          indexerClient: IndexerClient(apiUrl: INDEXER_URL[net]!));
    }
  }
  final Map<AlgorandNet, Algorand> client = {};
}