From 2ee65b584ea21d59f44d01884afb2cf9fe296068 Mon Sep 17 00:00:00 2001 From: detherminal <76167420+detherminal@users.noreply.github.com> Date: Thu, 27 Jul 2023 23:15:49 +0300 Subject: [PATCH 1/2] fix: fetch from nodes instead of tzkt --- lib/services/coins/tezos/tezos_wallet.dart | 13 +++++++------ lib/utilities/default_nodes.dart | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/services/coins/tezos/tezos_wallet.dart b/lib/services/coins/tezos/tezos_wallet.dart index e5576741c..38631a071 100644 --- a/lib/services/coins/tezos/tezos_wallet.dart +++ b/lib/services/coins/tezos/tezos_wallet.dart @@ -292,8 +292,8 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { } Future updateBalance() async { - var api = "https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/balance"; // TODO: Can we use current node instead of this? - var theBalance = await get(Uri.parse(api)).then((value) => value.body); + var api = "${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/context/contracts/${await currentReceivingAddress}/balance"; + var theBalance = (await get(Uri.parse(api)).then((value) => value.body)).substring(1, (await get(Uri.parse(api)).then((value) => value.body)).length - 2); Logging.instance.log("Balance for ${await currentReceivingAddress}: $theBalance", level: LogLevel.Info); var balanceInAmount = Amount(rawValue: BigInt.parse(theBalance.toString()), fractionDigits: 6); _balance = Balance( @@ -355,9 +355,10 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { } Future updateChainHeight() async { - var api = "https://api.tzkt.io/v1/blocks/count"; // TODO: Can we use current node instead of this? - var returnedHeight = await get(Uri.parse(api)).then((value) => value.body); - final int intHeight = int.parse(returnedHeight.toString()); + 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); await updateCachedChainHeight(intHeight); } @@ -375,7 +376,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { @override Future testNetworkConnection() async{ try { - await get(Uri.parse("https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/balance")); + await get(Uri.parse("${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell")); return true; } catch (e) { return false; diff --git a/lib/utilities/default_nodes.dart b/lib/utilities/default_nodes.dart index ac2edb482..22f274d62 100644 --- a/lib/utilities/default_nodes.dart +++ b/lib/utilities/default_nodes.dart @@ -182,8 +182,8 @@ abstract class DefaultNodes { isDown: false ); - static NodeModel get tezos => NodeModel( // TODO: Change this to original one - host: "mainnet.api.tez.ie", + static NodeModel get tezos => NodeModel( // TODO: Change this to stack wallet one + host: "https://mainnet.api.tez.ie", port: 443, name: defaultName, id: _nodeId(Coin.tezos), From 3077465e81e017c524b61b1ea9afcda3c1bb01fc Mon Sep 17 00:00:00 2001 From: detherminal <76167420+detherminal@users.noreply.github.com> Date: Fri, 28 Jul 2023 13:39:07 +0300 Subject: [PATCH 2/2] xtz: update api and add price --- lib/services/coins/tezos/tezos_wallet.dart | 87 +++++++++++----------- lib/services/price.dart | 2 +- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/services/coins/tezos/tezos_wallet.dart b/lib/services/coins/tezos/tezos_wallet.dart index 38631a071..8faa15427 100644 --- a/lib/services/coins/tezos/tezos_wallet.dart +++ b/lib/services/coins/tezos/tezos_wallet.dart @@ -306,52 +306,53 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB { } Future updateTransactions() async { - var api = "https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/balance_history"; // TODO: Can we use current node instead of this? - var returnedTxs = await get(Uri.parse(api)).then((value) => value.body); - Logging.instance.log( - "Transactions for ${await currentReceivingAddress}: $returnedTxs", - level: LogLevel.Info); + // TODO: Use node RPC instead of tzstats API + var api = "https://api.tzstats.com/tables/op?address=${await currentReceivingAddress}"; + var jsonResponse = jsonDecode(await get(Uri.parse(api)).then((value) => value.body)); List> txs = []; - Object? jsonTxs = jsonDecode(returnedTxs); - if (jsonTxs == null) { - await db.addNewTransactionData(txs, walletId); - } else { - for (var tx in jsonTxs as List) { - var theTx = Transaction( - walletId: walletId, - txid: "", - timestamp: DateTime.parse(tx["timestamp"].toString()).toUtc().millisecondsSinceEpoch ~/ 1000, - type: TransactionType.unknown, - subType: TransactionSubType.none, - amount: int.parse(tx["balance"].toString()), - amountString: Amount( - rawValue: BigInt.parse(tx["balance"].toString()), - fractionDigits: 6 - ).toJsonString(), - fee: 0, - height: int.parse(tx["level"].toString()), - isCancelled: false, - isLelantus: false, - slateId: "", - otherData: "", - inputs: [], - outputs: [], - nonce: 0, - numberOfMessages: null, - ); - var theAddress = Address( - walletId: walletId, - value: await currentReceivingAddress, - publicKey: [], // TODO: Add public key - derivationIndex: 0, - derivationPath: null, - type: AddressType.unknown, - subType: AddressSubType.unknown, - ); - txs.add(Tuple2(theTx, theAddress)); + for (var tx in jsonResponse as List) { + var txApi = "https://api.tzstats.com/explorer/op/${tx["hash"]}"; + var txJsonResponse = jsonDecode(await get(Uri.parse(txApi)).then((value) => value.body))[0]; + TransactionType txType; + if (txJsonResponse["sender"] == (await currentReceivingAddress)) { + txType = TransactionType.outgoing; + } else { + txType = TransactionType.incoming; } - await db.addNewTransactionData(txs, walletId); + var theTx = Transaction( + walletId: walletId, + txid: txJsonResponse["hash"].toString(), + timestamp: DateTime.parse(txJsonResponse["time"].toString()).toUtc().millisecondsSinceEpoch ~/ 1000, + type: txType, + subType: TransactionSubType.none, + amount: (float.parse(txJsonResponse["volume"].toString()) * 1000000).toInt(), + amountString: Amount( + rawValue: BigInt.parse((float.parse(txJsonResponse["volume"].toString()) * 1000000).toString()), + fractionDigits: 6 + ).toJsonString(), + fee: (float.parse(txJsonResponse["fee"].toString()) * 1000000).toInt(), + height: int.parse(txJsonResponse["height"].toString()), + isCancelled: false, + isLelantus: false, + slateId: "", + otherData: "", + inputs: [], + outputs: [], + nonce: 0, + numberOfMessages: null, + ); + var theAddress = Address( + walletId: walletId, + value: txJsonResponse["receiver"].toString(), + publicKey: [], // TODO: Add public key + derivationIndex: 0, + derivationPath: null, + type: AddressType.unknown, + subType: AddressSubType.unknown, + ); + txs.add(Tuple2(theTx, theAddress)); } + await db.addNewTransactionData(txs, walletId); } Future updateChainHeight() async { diff --git a/lib/services/price.dart b/lib/services/price.dart index 24d929062..e9cbacc96 100644 --- a/lib/services/price.dart +++ b/lib/services/price.dart @@ -100,7 +100,7 @@ class PriceAPI { Uri.parse("https://api.coingecko.com/api/v3/coins/markets?vs_currency" "=${baseCurrency.toLowerCase()}" "&ids=monero,bitcoin,litecoin,ecash,epic-cash,zcoin,dogecoin," - "bitcoin-cash,namecoin,wownero,ethereum,particl,nano,banano" + "bitcoin-cash,namecoin,wownero,ethereum,particl,nano,banano,tezos" "&order=market_cap_desc&per_page=50&page=1&sparkline=false"); final coinGeckoResponse = await client.get(