fix (xtz): add the reqs to rpc api

This commit is contained in:
detherminal 2023-08-27 08:43:12 -06:00 committed by julian
parent 30dbec866c
commit 88a98953e5
4 changed files with 121 additions and 90 deletions

View file

@ -11,7 +11,8 @@ class TezosAPI {
Future<List<TezosOperation>?> getTransactions(String address) async { Future<List<TezosOperation>?> getTransactions(String address) async {
try { try {
String transactionsCall = "$_baseURL/explorer/account/$address/operations"; String transactionsCall =
"$_baseURL/explorer/account/$address/operations";
var response = jsonDecode( var response = jsonDecode(
await get(Uri.parse(transactionsCall)).then((value) => value.body)); await get(Uri.parse(transactionsCall)).then((value) => value.body));
List<TezosOperation> txs = []; List<TezosOperation> txs = [];
@ -20,32 +21,41 @@ class TezosAPI {
int? burnedAmountInMicroTez; int? burnedAmountInMicroTez;
int? storage_limit; int? storage_limit;
if (tx["burned"] != null) { if (tx["burned"] != null) {
burnedAmountInMicroTez = double.parse((tx["burned"] * pow(10, Coin.tezos.decimals)).toString()).toInt(); burnedAmountInMicroTez = double.parse(
(tx["burned"] * pow(10, Coin.tezos.decimals)).toString())
.toInt();
} }
if (tx["storage_limit"] != null) { if (tx["storage_limit"] != null) {
storage_limit = tx["storage_limit"] as int; storage_limit = tx["storage_limit"] as int;
} }
final theTx = TezosOperation( final theTx = TezosOperation(
id: tx["id"] as int, id: tx["id"] as int,
hash: tx["hash"] as String, hash: tx["hash"] as String,
type: tx["type"] as String, type: tx["type"] as String,
height: tx["height"] as int, height: tx["height"] as int,
timestamp: DateTime.parse(tx["time"].toString()).toUtc().millisecondsSinceEpoch ~/ 1000, timestamp: DateTime.parse(tx["time"].toString())
cycle: tx["cycle"] as int, .toUtc()
counter: tx["counter"] as int, .millisecondsSinceEpoch ~/
op_n: tx["op_n"] as int, 1000,
op_p: tx["op_p"] as int, cycle: tx["cycle"] as int,
status: tx["status"] as String, counter: tx["counter"] as int,
is_success: tx["is_success"] as bool, op_n: tx["op_n"] as int,
gas_limit: tx["gas_limit"] as int, op_p: tx["op_p"] as int,
gas_used: tx["gas_used"] as int, status: tx["status"] as String,
storage_limit: storage_limit, is_success: tx["is_success"] as bool,
amountInMicroTez: double.parse((tx["volume"] * pow(10, Coin.tezos.decimals)).toString()).toInt(), gas_limit: tx["gas_limit"] as int,
feeInMicroTez: double.parse((tx["fee"] * pow(10, Coin.tezos.decimals)).toString()).toInt(), gas_used: tx["gas_used"] as int,
burnedAmountInMicroTez: burnedAmountInMicroTez, storage_limit: storage_limit,
senderAddress: tx["sender"] as String, amountInMicroTez: double.parse(
receiverAddress: tx["receiver"] as String, (tx["volume"] * pow(10, Coin.tezos.decimals)).toString())
confirmations: tx["confirmations"] as int, .toInt(),
feeInMicroTez: double.parse(
(tx["fee"] * pow(10, Coin.tezos.decimals)).toString())
.toInt(),
burnedAmountInMicroTez: burnedAmountInMicroTez,
senderAddress: tx["sender"] as String,
receiverAddress: tx["receiver"] as String,
confirmations: tx["confirmations"] as int,
); );
txs.add(theTx); txs.add(theTx);
} }
@ -67,47 +77,10 @@ class TezosAPI {
int totalTxs = response[0][8] as int; int totalTxs = response[0][8] as int;
return ((totalFees / totalTxs * Coin.tezos.decimals).floor()); return ((totalFees / totalTxs * Coin.tezos.decimals).floor());
} catch (e) { } catch (e) {
Logging.instance.log("Error occured in tezos_api.dart while getting fee estimation for tezos: $e", Logging.instance.log(
"Error occured in tezos_api.dart while getting fee estimation for tezos: $e",
level: LogLevel.Error); level: LogLevel.Error);
} }
return null; return null;
} }
Future<BigInt?> getBalance(String host, int port, String address) async {
try {
String balanceCall =
"$host:$port/chains/main/blocks/head/context/contracts/$address/balance";
var response =
await get(Uri.parse(balanceCall)).then((value) => value.body);
var balance = BigInt.parse(response.substring(1, response.length - 2));
return balance;
} catch (e) {
Logging.instance.log("Error occured in tezos_api.dart while getting balance for $address: $e",
level: LogLevel.Error);
}
return null;
}
Future<int?> getChainHeight(String host, int port) async {
try {
var api =
"$host:$port/chains/main/blocks/head/header/shell";
var jsonParsedResponse = jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
return int.parse(jsonParsedResponse["level"].toString());
} catch (e) {
Logging.instance.log("Error occured in tezos_api.dart while getting chain height for tezos: $e",
level: LogLevel.Error);
}
return null;
}
Future<bool> testNetworkConnection(String host, int port) async {
try {
await get(Uri.parse(
"$host:$port/chains/main/blocks/head/header/shell"));
return true;
} catch (e) {
return false;
}
}
} }

View file

@ -0,0 +1,52 @@
import 'dart:convert';
import 'package:http/http.dart';
import 'package:stackwallet/utilities/logger.dart';
class TezosRpcAPI {
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";
var response =
await get(Uri.parse(balanceCall)).then((value) => value.body);
var balance = BigInt.parse(response.substring(1, response.length - 2));
return balance;
} catch (e) {
Logging.instance.log(
"Error occured in tezos_rpc_api.dart while getting balance for $address: $e",
level: LogLevel.Error);
}
return null;
}
Future<int?> getChainHeight(
{required ({String host, int port}) nodeInfo}) async {
try {
var api =
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header/shell";
var jsonParsedResponse =
jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
return int.parse(jsonParsedResponse["level"].toString());
} catch (e) {
Logging.instance.log(
"Error occured in tezos_rpc_api.dart while getting chain height for tezos: $e",
level: LogLevel.Error);
}
return null;
}
Future<bool> testNetworkConnection(
{required ({String host, int port}) nodeInfo}) async {
try {
await get(Uri.parse(
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header/shell"));
return true;
} catch (e) {
return false;
}
}
}

View file

@ -20,26 +20,25 @@ class TezosOperation {
String receiverAddress; String receiverAddress;
int? confirmations; int? confirmations;
TezosOperation({ TezosOperation(
this.id, {this.id,
required this.hash, required this.hash,
this.type, this.type,
required this.height, required this.height,
required this.timestamp, required this.timestamp,
this.cycle, this.cycle,
this.counter, this.counter,
this.op_n, this.op_n,
this.op_p, this.op_p,
this.status, this.status,
this.is_success, this.is_success,
this.gas_limit, this.gas_limit,
this.gas_used, this.gas_used,
this.storage_limit, this.storage_limit,
required this.amountInMicroTez, required this.amountInMicroTez,
required this.feeInMicroTez, required this.feeInMicroTez,
this.burnedAmountInMicroTez, this.burnedAmountInMicroTez,
required this.senderAddress, required this.senderAddress,
required this.receiverAddress, required this.receiverAddress,
this.confirmations this.confirmations});
});
} }

View file

@ -34,6 +34,8 @@ import 'package:stackwallet/utilities/prefs.dart';
import 'package:tezart/tezart.dart'; import 'package:tezart/tezart.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import 'api/tezos_rpc_api.dart';
const int MINIMUM_CONFIRMATIONS = 1; const int MINIMUM_CONFIRMATIONS = 1;
const int _gasLimit = 10200; const int _gasLimit = 10200;
@ -58,6 +60,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
NodeModel? _xtzNode; NodeModel? _xtzNode;
TezosAPI tezosAPI = TezosAPI(); TezosAPI tezosAPI = TezosAPI();
TezosRpcAPI tezosRpcAPI = TezosRpcAPI();
NodeModel getCurrentNode() { NodeModel getCurrentNode() {
return _xtzNode ?? return _xtzNode ??
@ -491,11 +494,15 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateBalance() async { Future<void> updateBalance() async {
try { try {
NodeModel currentNode = getCurrentNode(); NodeModel currentNode = getCurrentNode();
BigInt? balance = await tezosAPI.getBalance( BigInt? balance = await tezosRpcAPI.getBalance(
currentNode.host, currentNode.port, await currentReceivingAddress); nodeInfo: (host: currentNode.host, port: currentNode.port),
address: await currentReceivingAddress);
if (balance == null) { if (balance == null) {
return; return;
} }
Logging.instance.log(
"Balance for ${await currentReceivingAddress}: $balance",
level: LogLevel.Info);
Amount balanceInAmount = Amount balanceInAmount =
Amount(rawValue: balance, fractionDigits: coin.decimals); Amount(rawValue: balance, fractionDigits: coin.decimals);
_balance = Balance( _balance = Balance(
@ -585,8 +592,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateChainHeight() async { Future<void> updateChainHeight() async {
try { try {
NodeModel currentNode = getCurrentNode(); NodeModel currentNode = getCurrentNode();
int? intHeight = int? intHeight = await tezosRpcAPI.getChainHeight(
await tezosAPI.getChainHeight(currentNode.host, currentNode.port); nodeInfo: (host: currentNode.host, port: currentNode.port));
if (intHeight == null) { if (intHeight == null) {
return; return;
} }
@ -670,8 +677,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override @override
Future<bool> testNetworkConnection() async { Future<bool> testNetworkConnection() async {
NodeModel currentNode = getCurrentNode(); NodeModel currentNode = getCurrentNode();
return await tezosAPI.testNetworkConnection( return await tezosRpcAPI.testNetworkConnection(
currentNode.host, currentNode.port); nodeInfo: (host: currentNode.host, port: currentNode.port));
} }
@override @override