From 1ecfc9c2d32ca80250275dbbca9705c525898d9a Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 17 Oct 2024 16:50:01 +0300 Subject: [PATCH] Add logger to stellar --- packages/stellar_client/lib/src/client.dart | 62 ++++++++++--------- .../stellar_client/lib/stellar_client.dart | 1 + packages/stellar_client/pubspec.lock | 16 +++-- packages/stellar_client/pubspec.yaml | 1 + 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/packages/stellar_client/lib/src/client.dart b/packages/stellar_client/lib/src/client.dart index 49a75e5f..180223c8 100644 --- a/packages/stellar_client/lib/src/client.dart +++ b/packages/stellar_client/lib/src/client.dart @@ -12,6 +12,10 @@ class Client { String get secretSeed => _keyPair.secretSeed; Uint8List? get privateKey => _keyPair.privateKey; + var logger = Logger( + printer: PrettyPrinter(), + ); + Client(this._network, String secretSeed) { _keyPair = KeyPair.fromSecretSeed(secretSeed); _initialize(); @@ -70,8 +74,8 @@ class Client { if (transaction != null) { transaction.sign(_keyPair, _stellarNetwork); await _sdk.submitTransaction(transaction); - print("Account Activated Successfully."); - print("TFT Asset was added Successfully "); + logger.i("Account Activated Successfully."); + logger.i("TFT Asset was added Successfully "); return true; } else { throw Exception("Failed to retrieve activation transaction."); @@ -109,13 +113,13 @@ class Client { try { bool funded = await FriendBot.fundTestAccount(accountId); if (funded) { - print("Account funded successfully"); + logger.i("Account funded successfully"); } else { - print("Failed to fund account"); + logger.e("Failed to fund account"); } return funded; } catch (error) { - print("Error while funding account: $error"); + logger.e("Error while funding account: $error"); return false; } } @@ -143,15 +147,15 @@ class Client { await _sdk.submitTransaction(transaction); if (!response.success) { - print("Failed to add trustline for $currencyCode"); + logger.e("Failed to add trustline for $currencyCode"); return false; } else { - print("trustline for $currencyCode was added successfully"); + logger.i("trustline for $currencyCode was added successfully"); return true; } } - print("No trustlines were processed"); + logger.i("No trustlines were processed"); return false; } @@ -169,10 +173,10 @@ class Client { transaction!.sign(_keyPair, _stellarNetwork); await _sdk.submitTransaction(transaction); - print("Transaction successful."); + logger.i("Transaction successful."); return true; } catch (error) { - print("Failed to send transaction: $error"); + logger.e("Failed to send transaction: $error"); return false; } } @@ -194,7 +198,7 @@ class Client { await _sdk.submitTransaction(transaction); return true; } catch (error) { - print("Failed to create account $error"); + logger.e("Failed to create account $error"); return false; } } @@ -232,10 +236,10 @@ class Client { await _sdk.submitTransaction(transaction); if (response2.success) { - print("Trustline for $asset_code added successfully."); + logger.i("Trustline for $asset_code added successfully."); return true; } else { - print("Failed to add trustline for $asset_code."); + logger.e("Failed to add trustline for $asset_code."); return false; } } catch (error) { @@ -263,10 +267,10 @@ class Client { if (details != null) { TransactionData transactionData = TransactionData.fromJson(details); - print(transactionData); + logger.i(transactionData); return transactionData; } else { - print('Failed to details for asset: '); + logger.e('Failed to details for asset: '); } return null; @@ -292,17 +296,17 @@ class Client { required String currency, String? memoText, required bool funded}) async { - // check if I have enough balance final accountBalances = await this.getBalance(); - accountBalances.firstWhere((b) => b.assetCode == currency && double.parse(b.balance) > double.parse(amount), - orElse: () { - if (accountBalances.contains(currency)) { - throw Exception('Balance is not enough.'); - } - throw Exception('Balance with asset code ${currency} not found.'); + accountBalances.firstWhere( + (b) => + b.assetCode == currency && + double.parse(b.balance) > double.parse(amount), orElse: () { + if (accountBalances.contains(currency)) { + throw Exception('Balance is not enough.'); } - ); + throw Exception('Balance with asset code ${currency} not found.'); + }); // check that receiver account exists final receiver = await _sdk.accounts.account(accountId); // check that asset exists @@ -358,8 +362,8 @@ class Client { fundedTransaction!.sign(_keyPair, _stellarNetwork); - print('Sending to'); - print( + logger.i('Sending to'); + logger.i( '${_serviceUrls[_network.toString()]}/transactionfunding_service/fund_transaction'); try { @@ -371,7 +375,7 @@ class Client { {'transaction': fundedTransaction.toEnvelopeXdrBase64()}), ); - print(response.body); + logger.i(response.body); } catch (error) { throw Exception('Something went wrong! $error'); } @@ -407,11 +411,11 @@ class Client { transactionDetails.add(details); } } else { - print("Unhandled operation type: ${response.runtimeType}"); + logger.i("Unhandled operation type: ${response.runtimeType}"); } } } else { - print("No payment records found."); + logger.i("No payment records found."); } return transactionDetails; } @@ -448,7 +452,7 @@ class Client { final body = jsonDecode(response.body); if (body['vesting_accounts'] is List && body['vesting_accounts'].isEmpty) { - print("no vesting account found"); + logger.i("no vesting account found"); return []; } else { List accountsList = []; diff --git a/packages/stellar_client/lib/stellar_client.dart b/packages/stellar_client/lib/stellar_client.dart index ab471557..0004740a 100644 --- a/packages/stellar_client/lib/stellar_client.dart +++ b/packages/stellar_client/lib/stellar_client.dart @@ -11,6 +11,7 @@ import 'package:stellar_client/models/transaction.dart'; import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart'; import 'package:http/http.dart' as http; import 'package:convert/convert.dart'; +import 'package:logger/logger.dart'; part 'src/client.dart'; part 'src/network_types.dart'; diff --git a/packages/stellar_client/pubspec.lock b/packages/stellar_client/pubspec.lock index c3375831..4268f691 100644 --- a/packages/stellar_client/pubspec.lock +++ b/packages/stellar_client/pubspec.lock @@ -190,6 +190,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.0" + logger: + dependency: "direct main" + description: + name: logger + sha256: "697d067c60c20999686a0add96cf6aba723b3aa1f83ecf806a8097231529ec32" + url: "https://pub.dev" + source: hosted + version: "2.4.0" logging: dependency: transitive description: @@ -210,18 +218,18 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.11.0" mime: dependency: transitive description: diff --git a/packages/stellar_client/pubspec.yaml b/packages/stellar_client/pubspec.yaml index a6a04c1f..d1736867 100644 --- a/packages/stellar_client/pubspec.yaml +++ b/packages/stellar_client/pubspec.yaml @@ -8,6 +8,7 @@ environment: # Add regular dependencies here. dependencies: http: 1.2.0 + logger: ^2.4.0 stellar_flutter_sdk: ^1.7.5 dev_dependencies: