forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar_client.dart
More file actions
81 lines (73 loc) · 2.59 KB
/
Copy pathstellar_client.dart
File metadata and controls
81 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../echo_mirror.dart';
import 'stellar_models.dart';
import '../errors.dart';
class StellarClient {
final SDKConfig config;
StellarClient(this.config);
Map<String, String> get _headers => {
'x-api-key': config.apiKey,
'x-echomirror-network': config.network.name,
'content-type': 'application/json',
if (config.authToken != null)
'authorization': 'Bearer ${config.authToken}',
};
/// Get XLM and ECHO token balance for a Stellar public key.
///
/// ```dart
/// final balance = await EchoMirror.instance.stellar.getBalance(publicKey);
/// print('${balance.xlm} XLM • ${balance.echo} ECHO');
/// ```
Future<StellarBalance> getBalance(String publicKey) async {
final res = await config.httpClient.get(
Uri.parse('${config.baseUrl}/stellar/balance/$publicKey'),
headers: _headers,
);
_checkStatus(res);
return StellarBalance.fromJson(
jsonDecode(res.body) as Map<String, dynamic>);
}
/// Fund a testnet account using Stellar Friendbot.
/// Only works on testnet.
///
/// ```dart
/// await EchoMirror.instance.stellar.fundTestnetAccount(publicKey);
/// ```
Future<void> fundTestnetAccount(String publicKey) async {
if (config.network != StellarNetwork.testnet) {
throw const EchoMirrorError(
'fundTestnetAccount is only available on testnet');
}
final res = await config.httpClient.post(
Uri.parse('${config.baseUrl}/stellar/friendbot'),
headers: _headers,
body: jsonEncode({'public_key': publicKey}),
);
_checkStatus(res);
}
/// Get paginated transaction history for a public key.
Future<List<StellarTransaction>> getTransactionHistory(
String publicKey, {
int limit = 20,
String? cursor,
}) async {
var url =
'${config.baseUrl}/stellar/transactions?public_key=$publicKey&limit=$limit';
if (cursor != null) url += '&cursor=$cursor';
final res = await config.httpClient.get(Uri.parse(url), headers: _headers);
_checkStatus(res);
final body = jsonDecode(res.body) as Map<String, dynamic>;
return (body['transactions'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map(StellarTransaction.fromJson)
.toList();
}
void _checkStatus(http.Response res) {
if (res.statusCode == 401) throw const EchoMirrorAuthError();
if (res.statusCode == 429) throw const EchoMirrorRateLimitError();
if (res.statusCode < 200 || res.statusCode >= 300) {
throw EchoMirrorError('HTTP ${res.statusCode}: ${res.body}');
}
}
}