pull over tezos support files from the future

This commit is contained in:
julian 2023-12-04 14:54:55 -06:00
parent 9a9c9550ee
commit 01f2cdd117
4 changed files with 289 additions and 0 deletions

View file

@ -0,0 +1,58 @@
class TezosAccount {
final int id;
final String type;
final String address;
final String? publicKey;
final bool revealed;
final int balance;
final int counter;
TezosAccount({
required this.id,
required this.type,
required this.address,
required this.publicKey,
required this.revealed,
required this.balance,
required this.counter,
});
TezosAccount copyWith({
int? id,
String? type,
String? address,
String? publicKey,
bool? revealed,
int? balance,
int? counter,
}) {
return TezosAccount(
id: id ?? this.id,
type: type ?? this.type,
address: address ?? this.address,
publicKey: publicKey ?? this.publicKey,
revealed: revealed ?? this.revealed,
balance: balance ?? this.balance,
counter: counter ?? this.counter,
);
}
factory TezosAccount.fromMap(Map<String, dynamic> map) {
return TezosAccount(
id: map['id'] as int,
type: map['type'] as String,
address: map['address'] as String,
publicKey: map['publicKey'] as String?,
revealed: map['revealed'] as bool,
balance: map['balance'] as int,
counter: map['counter'] as int,
);
}
@override
String toString() {
return 'UserData{id: $id, type: $type, address: $address, '
'publicKey: $publicKey, revealed: $revealed,'
' balance: $balance, counter: $counter}';
}
}

View file

@ -0,0 +1,117 @@
import 'dart:convert';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/coins/tezos/api/tezos_account.dart';
import 'package:stackwallet/services/coins/tezos/api/tezos_transaction.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
abstract final class TezosAPI {
static final HTTP _client = HTTP();
static const String _baseURL = 'https://api.tzkt.io';
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,
);
rethrow;
}
}
static Future<TezosAccount> getAccount(String address,
{String type = "user"}) async {
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));
return account;
} catch (e, s) {
Logging.instance.log(
"Error occurred in TezosAPI while getting account for $address: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
static Future<List<TezosTransaction>> getTransactions(String address) async {
try {
final transactionsCall =
"$_baseURL/v1/accounts/$address/operations?type=transaction";
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;
List<TezosTransaction> txs = [];
for (var tx in result) {
if (tx["type"] == "transaction") {
final theTx = TezosTransaction(
id: tx["id"] as int,
hash: tx["hash"] as String,
type: tx["type"] as String,
height: tx["level"] as int,
timestamp: DateTime.parse(tx["timestamp"].toString())
.toUtc()
.millisecondsSinceEpoch ~/
1000,
cycle: tx["cycle"] as int?,
counter: tx["counter"] as int,
opN: tx["op_n"] as int?,
opP: tx["op_p"] as int?,
status: tx["status"] as String,
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,
);
txs.add(theTx);
}
}
return txs;
} catch (e, s) {
Logging.instance.log(
"Error occurred in TezosAPI while getting transactions for $address: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
}

View file

@ -0,0 +1,71 @@
import 'dart:convert';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
abstract final class TezosRpcAPI {
static final HTTP _client = HTTP();
static Future<BigInt?> getBalance({
required ({String host, int port}) nodeInfo,
required String address,
}) async {
try {
String balanceCall =
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/context/contracts/$address/balance";
final response = await _client.get(
url: Uri.parse(balanceCall),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final balance =
BigInt.parse(response.body.substring(1, response.body.length - 2));
return balance;
} catch (e) {
Logging.instance.log(
"Error occurred in tezos_rpc_api.dart while getting balance for $address: $e",
level: LogLevel.Error,
);
}
return null;
}
static Future<int?> getChainHeight({
required ({String host, int port}) nodeInfo,
}) async {
try {
final api =
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header/shell";
final response = await _client.get(
url: Uri.parse(api),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final jsonParsedResponse = jsonDecode(response.body);
return int.parse(jsonParsedResponse["level"].toString());
} catch (e) {
Logging.instance.log(
"Error occurred in tezos_rpc_api.dart while getting chain height for tezos: $e",
level: LogLevel.Error,
);
}
return null;
}
static Future<bool> testNetworkConnection({
required ({String host, int port}) nodeInfo,
}) async {
final result = await getChainHeight(nodeInfo: nodeInfo);
return result != null;
}
}

View file

@ -0,0 +1,43 @@
class TezosTransaction {
final int? id;
final String hash;
final String? type;
final int height;
final int timestamp;
final int? cycle;
final int? counter;
final int? opN;
final int? opP;
final String? status;
final bool? isSuccess;
final int? gasLimit;
final int? gasUsed;
final int? storageLimit;
final int amountInMicroTez;
final int feeInMicroTez;
final int? burnedAmountInMicroTez;
final String senderAddress;
final String receiverAddress;
TezosTransaction({
this.id,
required this.hash,
this.type,
required this.height,
required this.timestamp,
this.cycle,
this.counter,
this.opN,
this.opP,
this.status,
this.isSuccess,
this.gasLimit,
this.gasUsed,
this.storageLimit,
required this.amountInMicroTez,
required this.feeInMicroTez,
this.burnedAmountInMicroTez,
required this.senderAddress,
required this.receiverAddress,
});
}