Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support getting balance by account id #116

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions packages/stellar_client/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,23 +417,13 @@ class Client {
}

Future<List<BalanceInfo>> getBalance() async {
List<BalanceInfo> balancesList = [];
AccountResponse account = await _sdk.accounts.account(accountId);

for (Balance balance in account.balances) {
BalanceData balanceData;
switch (balance.assetType) {
case Asset.TYPE_NATIVE:
balanceData = BalanceData(assetCode: 'XLM', balance: balance.balance);
break;
default:
balanceData = BalanceData(
assetCode: balance.assetCode!, balance: balance.balance);
}
balancesList.add(balanceData);
try {
List<BalanceInfo> balancesList =
await getBalanceByAccountID(network: _network, accountId: accountId);
return balancesList;
} catch (error) {
throw Exception('Could not get balance due to $error');
}

return balancesList;
}

Future<List<VestingAccount>?> getVestingAccounts() async {
Expand Down
35 changes: 35 additions & 0 deletions packages/stellar_client/lib/src/helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:stellar_client/models/balance.dart';
import 'package:stellar_client/stellar_client.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';

Future<List<BalanceInfo>> getBalanceByAccountID(
{required NetworkType network, required String accountId}) async {
late StellarSDK _sdk;

switch (network) {
case NetworkType.TESTNET:
_sdk = StellarSDK.TESTNET;
break;
case NetworkType.PUBLIC:
_sdk = StellarSDK.PUBLIC;
break;
}

List<BalanceInfo> balancesList = [];
AccountResponse account = await _sdk.accounts.account(accountId);

for (Balance balance in account.balances) {
BalanceData balanceData;
switch (balance.assetType) {
case Asset.TYPE_NATIVE:
balanceData = BalanceData(assetCode: 'XLM', balance: balance.balance);
break;
default:
balanceData = BalanceData(
assetCode: balance.assetCode!, balance: balance.balance);
}
balancesList.add(balanceData);
}

return balancesList;
}
1 change: 1 addition & 0 deletions packages/stellar_client/lib/stellar_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:stellar_client/models/currency.dart' as currency;
import 'package:stellar_client/models/transaction_data.dart';
import 'package:stellar_client/models/vesting_account.dart';
import 'package:stellar_client/models/transaction.dart';
import 'package:stellar_client/src/helpers.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
import 'package:http/http.dart' as http;
import 'package:convert/convert.dart';
Expand Down
Loading