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;
} catch (e) {
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);
}
return null;
@ -67,9 +67,47 @@ class TezosAPI {
int totalTxs = response[0][8] as int;
return ((totalFees / totalTxs * Coin.tezos.decimals).floor());
} 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);
}
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 {
try {
String balanceCall = "https://api.mainnet.tzkt.io/v1/accounts/"
"${await currentReceivingAddress}/balance";
var response = jsonDecode(await client
.get(
url: Uri.parse(balanceCall),
proxyInfo:
_prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null,
)
.then((value) => value.body));
Amount balanceInAmount = Amount(
rawValue: BigInt.parse(balance), fractionDigits: coin.decimals);
NodeModel currentNode = getCurrentNode();
BigInt? balance = await tezosAPI.getBalance(
currentNode.host, currentNode.port, await currentReceivingAddress);
if (balance == null) {
return;
}
Amount balanceInAmount =
Amount(rawValue: balance, fractionDigits: coin.decimals);
_balance = Balance(
total: balanceInAmount,
spendable: balanceInAmount,
@ -511,8 +508,9 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
);
await updateCachedBalance(_balance!);
} catch (e, s) {
Logging.instance
.log("ERROR GETTING BALANCE ${e.toString()}", level: LogLevel.Error);
Logging.instance.log(
"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 {
try {
var api =
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell";
var jsonParsedResponse =
jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
final int intHeight = int.parse(jsonParsedResponse["level"].toString());
Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info);
NodeModel currentNode = getCurrentNode();
int? intHeight =
await tezosAPI.getChainHeight(currentNode.host, currentNode.port);
if (intHeight == null) {
return;
}
Logging.instance
.log("Chain height for tezos: $intHeight", level: LogLevel.Info);
await updateCachedChainHeight(intHeight);
} catch (e, s) {
Logging.instance
.log("GET CHAIN HEIGHT ERROR ${e.toString()}", level: LogLevel.Error);
Logging.instance.log(
"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
Future<bool> testNetworkConnection() async {
try {
await client.get(
url: Uri.parse(
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"),
proxyInfo:
_prefs.useTor ? TorService.sharedInstance.getProxyInfo() : null,
);
return true;
} catch (e) {
return false;
}
NodeModel currentNode = getCurrentNode();
return await tezosAPI.testNetworkConnection(
currentNode.host, currentNode.port);
}
@override