1+ import 'dart:convert' ;
12import 'dart:typed_data' ;
23
34import 'package:stellar_client/models/transaction.dart' ;
45import 'package:stellar_client/models/vesting_account.dart' ;
56import 'package:stellar_client/stellar_client.dart' ;
67import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart' ;
78import 'package:threebotlogin/helpers/logger.dart' ;
9+ import 'package:http/http.dart' as http;
810
911bool isValidStellarSecret (String seed) {
1012 try {
@@ -37,7 +39,10 @@ Future<String> getBalanceByClient(Client client) async {
3739 }
3840 } catch (e) {
3941 logger.i ("Couldn't load the account balance due to $e " );
42+ // -2 means that the account not activated on stellar
43+ return '-2' ;
4044 }
45+ // -1 means that account activated but no TFT trustline
4146 return '-1' ;
4247}
4348
@@ -77,9 +82,9 @@ Future<void> transfer(String secret, String dest, String amount,
7782 );
7883}
7984
80- Future <void > initialize (String secret) async {
85+ Future <bool > initialize (String secret) async {
8186 final client = Client (NetworkType .PUBLIC , secret);
82- await client.activateThroughThreefoldService ();
87+ return await client.activateThroughThreefoldService ();
8388}
8489
8590Future <String > getBalanceByAccountId (String accountId) async {
@@ -94,6 +99,54 @@ Future<String> getBalanceByAccountId(String accountId) async {
9499 }
95100 } catch (e) {
96101 logger.i ("Couldn't load the account balance due to $e " );
102+ return '-2' ;
97103 }
98104 return '-1' ;
99105}
106+
107+ Future <int > getTFTPriceFromXLM () async {
108+ const String baseUrl = 'https://horizon.stellar.org' ;
109+ const String counterAssetCode = 'TFT' ;
110+ const String counterAssetIssuer =
111+ 'GBOVQKJYHXRR3DX6NOX2RRYFRCUMSADGDESTDNBDS6CDVLGVESRTAC47' ;
112+ final String requestUrl = '$baseUrl /trades?base_asset_type=native'
113+ '&counter_asset_type=credit_alphanum4'
114+ '&counter_asset_code=$counterAssetCode '
115+ '&counter_asset_issuer=$counterAssetIssuer '
116+ '&order=desc&limit=1' ;
117+
118+ try {
119+ final response = await http.get (Uri .parse (requestUrl));
120+
121+ if (response.statusCode == 200 ) {
122+ final data = jsonDecode (response.body);
123+ final List <dynamic > trades = data['_embedded' ]? ['records' ] ?? [];
124+
125+ if (trades.isNotEmpty) {
126+ final trade = trades[0 ];
127+ final double baseAmount = double .parse (trade['base_amount' ]);
128+ final double counterAmount = double .parse (trade['counter_amount' ]);
129+
130+ final double pricePerTFT = counterAmount / baseAmount;
131+ logger.i ('Last traded price for 1 XLM in TFT: $pricePerTFT ' );
132+ final int roundedPrice = pricePerTFT.ceil ();
133+
134+ return roundedPrice;
135+ } else {
136+ logger.i ('No recent trades found.' );
137+ return 0 ;
138+ }
139+ } else {
140+ logger.e ('Error fetching last traded price: ${response .statusCode }' );
141+ throw Exception ('Error getting price' );
142+ }
143+ } catch (e) {
144+ logger.e ('Error: $e ' );
145+ throw Exception ('Error getting price' );
146+ }
147+ }
148+
149+ Future <bool > addTFTTrustline (String secret, String assetCode) async {
150+ final client = Client (NetworkType .PUBLIC , secret);
151+ return await client.addTrustLineThroughThreefoldService (assetCode);
152+ }
0 commit comments