Refactor Update transactions call

This commit is contained in:
likho 2023-08-15 16:49:28 +02:00
parent 520ceabf79
commit 0a77dad7ec

View file

@ -198,7 +198,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Logging.instance.log(secretKey, level: LogLevel.Info); Logging.instance.log(secretKey, level: LogLevel.Info);
final sourceKeyStore = Keystore.fromSecretKey(secretKey); final sourceKeyStore = Keystore.fromSecretKey(secretKey);
final client = TezartClient(getCurrentNode().host); final client = TezartClient(getCurrentNode().host);
//TODO - Update gas Limit
final operation = await client.transferOperation( final operation = await client.transferOperation(
source: sourceKeyStore, source: sourceKeyStore,
destination: destinationAddress, destination: destinationAddress,
@ -431,50 +431,45 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
Future<void> updateTransactions() async { Future<void> updateTransactions() async {
// TODO: Use node RPC instead of tzstats API // TODO: Use node RPC instead of tzstats API
var api = String transactionsCall = "https://api.mainnet.tzkt.io/v1/accounts/${await currentReceivingAddress}/operations";
"https://api.tzstats.com/tables/op?address=${await currentReceivingAddress}"; var response = jsonDecode(await get(Uri.parse(transactionsCall)).then((value) => value.body));
var jsonResponse =
jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
List<Tuple2<Transaction, Address>> txs = []; List<Tuple2<Transaction, Address>> txs = [];
for (var tx in response as List) {
for (var tx in jsonResponse as List) { if (tx["type"] == "transaction") {
if (tx[1] == "transaction") {
var txApi =
"https://api.tzstats.com/explorer/op/${tx[0]}"; //Get transactions by Unique Id, this way we will only get txs
var txJsonResponse =
jsonDecode(await get(Uri.parse(txApi)).then((value) => value.body));
// Check if list is larger than 1 (if it is, it's a batch transaction)
if (!((txJsonResponse as List).length > 1)) {
for (var (opJson as Map) in txJsonResponse) {
if (opJson.containsKey("volume")) {
// This is to check if transaction is a token transfer
TransactionType txType; TransactionType txType;
if (opJson["sender"] == (await currentReceivingAddress)) { if (tx["sender"]["address"] == (await currentReceivingAddress)) {
txType = TransactionType.outgoing; txType = TransactionType.outgoing;
} else { } else {
txType = TransactionType.incoming; txType = TransactionType.incoming;
} }
final amount = tx["amount"] as int;
final fee = tx["bakerFee"] as int;
final amountInMicroTez =
amount / 1000000;
final feeInMicroTez =
fee / 1000000;
var theTx = Transaction( var theTx = Transaction(
walletId: walletId, walletId: walletId,
txid: opJson["hash"].toString(), txid: tx["hash"].toString(),
timestamp: DateTime.parse(opJson["time"].toString()) timestamp: DateTime.parse(tx["timestamp"].toString())
.toUtc() .toUtc()
.millisecondsSinceEpoch ~/ .millisecondsSinceEpoch ~/
1000, 1000,
type: txType, type: txType,
subType: TransactionSubType.none, subType: TransactionSubType.none,
amount: (float.parse(opJson["volume"].toString()) * 1000000) amount: tx["amount"] as int,
.toInt(),
amountString: Amount( amountString: Amount(
rawValue: BigInt.parse( rawValue: BigInt.parse(
(float.parse(opJson["volume"].toString()) * 1000000) (tx["amount"] as int)
.toInt() .toInt()
.toString()), .toString()),
fractionDigits: coin.decimals) fractionDigits: coin.decimals)
.toJsonString(), .toJsonString(),
fee: (float.parse(opJson["fee"].toString()) * 1000000).toInt(), fee: tx["bakerFee"] as int,
height: int.parse(opJson["height"].toString()), height: int.parse(tx["level"].toString()),
isCancelled: false, isCancelled: false,
isLelantus: false, isLelantus: false,
slateId: "", slateId: "",
@ -486,7 +481,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
); );
var theAddress = Address( var theAddress = Address(
walletId: walletId, walletId: walletId,
value: opJson["receiver"].toString(), value: tx["target"]["address"].toString(),
publicKey: [], // TODO: Add public key publicKey: [], // TODO: Add public key
derivationIndex: 0, derivationIndex: 0,
derivationPath: null, derivationPath: null,
@ -496,16 +491,12 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
txs.add(Tuple2(theTx, theAddress)); txs.add(Tuple2(theTx, theAddress));
} }
} }
}
}
}
Logging.instance.log("Transactions: $txs", level: LogLevel.Info); Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
await db.addNewTransactionData(txs, walletId); await db.addNewTransactionData(txs, walletId);
} }
Future<void> updateChainHeight() async { Future<void> updateChainHeight() async {
try { try {
final client = TezartClient(getCurrentNode().host);
var api = "${getCurrentNode().host}/chains/main/blocks/head/header/shell"; var api = "${getCurrentNode().host}/chains/main/blocks/head/header/shell";
var jsonParsedResponse = var jsonParsedResponse =
jsonDecode(await get(Uri.parse(api)).then((value) => value.body)); jsonDecode(await get(Uri.parse(api)).then((value) => value.body));