2023-08-24 17:51:48 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../../networking/http.dart';
|
|
|
|
import '../../../services/tor_service.dart';
|
|
|
|
import '../../../utilities/logger.dart';
|
|
|
|
import '../../../utilities/prefs.dart';
|
|
|
|
import 'tezos_account.dart';
|
|
|
|
import 'tezos_transaction.dart';
|
2023-08-24 17:51:48 +00:00
|
|
|
|
2023-11-20 15:55:28 +00:00
|
|
|
abstract final class TezosAPI {
|
|
|
|
static final HTTP _client = HTTP();
|
2023-11-20 22:16:07 +00:00
|
|
|
static const String _baseURL = 'https://api.tzkt.io';
|
2023-08-24 17:51:48 +00:00
|
|
|
|
2023-11-21 19:44:21 +00:00
|
|
|
static Future<int> getCounter(String address) async {
|
|
|
|
try {
|
|
|
|
final uriString = "$_baseURL/v1/accounts/$address/counter";
|
|
|
|
final response = await _client.get(
|
|
|
|
url: Uri.parse(uriString),
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
proxyInfo: Prefs.instance.useTor
|
|
|
|
? TorService.sharedInstance.getProxyInfo()
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
final result = jsonDecode(response.body);
|
|
|
|
return result as int;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error occurred in TezosAPI while getting counter for $address: $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
2023-11-22 18:30:09 +00:00
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:56:22 +00:00
|
|
|
static Future<TezosAccount> getAccount(
|
|
|
|
String address, {
|
|
|
|
String type = "user",
|
|
|
|
}) async {
|
2023-11-22 18:30:09 +00:00
|
|
|
try {
|
|
|
|
final uriString = "$_baseURL/v1/accounts/$address?legacy=false";
|
|
|
|
final response = await _client.get(
|
|
|
|
url: Uri.parse(uriString),
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
proxyInfo: Prefs.instance.useTor
|
|
|
|
? TorService.sharedInstance.getProxyInfo()
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
final result = jsonDecode(response.body) as Map;
|
|
|
|
|
|
|
|
final account = TezosAccount.fromMap(Map<String, dynamic>.from(result));
|
|
|
|
|
|
|
|
print("Get account =================== $account");
|
|
|
|
|
|
|
|
return account;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error occurred in TezosAPI while getting account for $address: $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
2023-11-21 19:44:21 +00:00
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<List<TezosTransaction>> getTransactions(String address) async {
|
2023-08-24 17:51:48 +00:00
|
|
|
try {
|
2023-11-20 22:16:07 +00:00
|
|
|
final transactionsCall =
|
|
|
|
"$_baseURL/v1/accounts/$address/operations?type=transaction";
|
2023-11-20 15:55:28 +00:00
|
|
|
|
|
|
|
final response = await _client.get(
|
|
|
|
url: Uri.parse(transactionsCall),
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
proxyInfo: Prefs.instance.useTor
|
|
|
|
? TorService.sharedInstance.getProxyInfo()
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
final result = jsonDecode(response.body) as List;
|
|
|
|
|
2024-05-27 23:56:22 +00:00
|
|
|
final List<TezosTransaction> txs = [];
|
|
|
|
for (final tx in result) {
|
2023-08-24 17:51:48 +00:00
|
|
|
if (tx["type"] == "transaction") {
|
2023-11-20 15:55:28 +00:00
|
|
|
final theTx = TezosTransaction(
|
2023-08-27 14:43:12 +00:00
|
|
|
id: tx["id"] as int,
|
|
|
|
hash: tx["hash"] as String,
|
|
|
|
type: tx["type"] as String,
|
2023-11-20 22:16:07 +00:00
|
|
|
height: tx["level"] as int,
|
|
|
|
timestamp: DateTime.parse(tx["timestamp"].toString())
|
2023-08-27 14:43:12 +00:00
|
|
|
.toUtc()
|
|
|
|
.millisecondsSinceEpoch ~/
|
|
|
|
1000,
|
2023-11-20 22:16:07 +00:00
|
|
|
cycle: tx["cycle"] as int?,
|
2023-08-27 14:43:12 +00:00
|
|
|
counter: tx["counter"] as int,
|
2023-11-20 22:16:07 +00:00
|
|
|
opN: tx["op_n"] as int?,
|
|
|
|
opP: tx["op_p"] as int?,
|
2023-08-27 14:43:12 +00:00
|
|
|
status: tx["status"] as String,
|
2023-11-20 22:16:07 +00:00
|
|
|
gasLimit: tx["gasLimit"] as int,
|
|
|
|
gasUsed: tx["gasUsed"] as int,
|
|
|
|
storageLimit: tx["storageLimit"] as int?,
|
|
|
|
amountInMicroTez: tx["amount"] as int,
|
|
|
|
feeInMicroTez: (tx["bakerFee"] as int? ?? 0) +
|
|
|
|
(tx["storageFee"] as int? ?? 0) +
|
|
|
|
(tx["allocationFee"] as int? ?? 0),
|
|
|
|
burnedAmountInMicroTez: tx["burned"] as int?,
|
|
|
|
senderAddress: tx["sender"]["address"] as String,
|
|
|
|
receiverAddress: tx["target"]["address"] as String,
|
2023-08-24 17:51:48 +00:00
|
|
|
);
|
|
|
|
txs.add(theTx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txs;
|
2023-11-20 22:16:07 +00:00
|
|
|
} catch (e, s) {
|
2023-08-24 17:51:48 +00:00
|
|
|
Logging.instance.log(
|
2023-11-21 19:44:21 +00:00
|
|
|
"Error occurred in TezosAPI while getting transactions for $address: $e\n$s",
|
2023-11-20 15:55:28 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
2023-11-21 19:44:21 +00:00
|
|
|
rethrow;
|
2023-08-27 14:23:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 17:51:48 +00:00
|
|
|
}
|