feat (xtz): add node reqs to tezos api

This commit is contained in:
detherminal 2023-08-27 08:23:02 -06:00 committed by julian
parent 7c24e9e840
commit 30dbec866c
2 changed files with 65 additions and 34 deletions

View file

@ -53,7 +53,7 @@ class TezosAPI {
return txs; return txs;
} catch (e) { } catch (e) {
Logging.instance.log( Logging.instance.log(
"Error occured while getting transactions for $address: $e", "Error occured in tezos_api.dart while getting transactions for $address: $e",
level: LogLevel.Error); level: LogLevel.Error);
} }
return null; return null;
@ -67,9 +67,47 @@ 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 while getting fee estimation: $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

@ -490,17 +490,14 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateBalance() async { Future<void> updateBalance() async {
try { try {
String balanceCall = "https://api.mainnet.tzkt.io/v1/accounts/" NodeModel currentNode = getCurrentNode();
"${await currentReceivingAddress}/balance"; BigInt? balance = await tezosAPI.getBalance(
var response = jsonDecode(await client currentNode.host, currentNode.port, await currentReceivingAddress);
.get( if (balance == null) {
url: Uri.parse(balanceCall), return;
proxyInfo: }
_prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null, Amount balanceInAmount =
) Amount(rawValue: balance, fractionDigits: coin.decimals);
.then((value) => value.body));
Amount balanceInAmount = Amount(
rawValue: BigInt.parse(balance), fractionDigits: coin.decimals);
_balance = Balance( _balance = Balance(
total: balanceInAmount, total: balanceInAmount,
spendable: balanceInAmount, spendable: balanceInAmount,
@ -511,8 +508,9 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
); );
await updateCachedBalance(_balance!); await updateCachedBalance(_balance!);
} catch (e, s) { } catch (e, s) {
Logging.instance Logging.instance.log(
.log("ERROR GETTING BALANCE ${e.toString()}", level: LogLevel.Error); "Error getting balance in tezos_wallet.dart: ${e.toString()}",
level: LogLevel.Error);
} }
} }
@ -586,16 +584,19 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateChainHeight() async { Future<void> updateChainHeight() async {
try { try {
var api = NodeModel currentNode = getCurrentNode();
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"; int? intHeight =
var jsonParsedResponse = await tezosAPI.getChainHeight(currentNode.host, currentNode.port);
jsonDecode(await get(Uri.parse(api)).then((value) => value.body)); if (intHeight == null) {
final int intHeight = int.parse(jsonParsedResponse["level"].toString()); return;
Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info); }
Logging.instance
.log("Chain height for tezos: $intHeight", level: LogLevel.Info);
await updateCachedChainHeight(intHeight); await updateCachedChainHeight(intHeight);
} catch (e, s) { } catch (e, s) {
Logging.instance Logging.instance.log(
.log("GET CHAIN HEIGHT ERROR ${e.toString()}", level: LogLevel.Error); "Error occured in tezos_wallet.dart while getting chain height for tezos: ${e.toString()}",
level: LogLevel.Error);
} }
} }
@ -668,17 +669,9 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override @override
Future<bool> testNetworkConnection() async { Future<bool> testNetworkConnection() async {
try { NodeModel currentNode = getCurrentNode();
await client.get( return await tezosAPI.testNetworkConnection(
url: Uri.parse( currentNode.host, currentNode.port);
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"),
proxyInfo:
_prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null,
);
return true;
} catch (e) {
return false;
}
} }
@override @override