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,72 +431,64 @@ 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") { TransactionType txType;
var txApi = if (tx["sender"]["address"] == (await currentReceivingAddress)) {
"https://api.tzstats.com/explorer/op/${tx[0]}"; //Get transactions by Unique Id, this way we will only get txs txType = TransactionType.outgoing;
var txJsonResponse = } else {
jsonDecode(await get(Uri.parse(txApi)).then((value) => value.body)); txType = TransactionType.incoming;
// 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;
if (opJson["sender"] == (await currentReceivingAddress)) {
txType = TransactionType.outgoing;
} else {
txType = TransactionType.incoming;
}
var theTx = Transaction(
walletId: walletId,
txid: opJson["hash"].toString(),
timestamp: DateTime.parse(opJson["time"].toString())
.toUtc()
.millisecondsSinceEpoch ~/
1000,
type: txType,
subType: TransactionSubType.none,
amount: (float.parse(opJson["volume"].toString()) * 1000000)
.toInt(),
amountString: Amount(
rawValue: BigInt.parse(
(float.parse(opJson["volume"].toString()) * 1000000)
.toInt()
.toString()),
fractionDigits: coin.decimals)
.toJsonString(),
fee: (float.parse(opJson["fee"].toString()) * 1000000).toInt(),
height: int.parse(opJson["height"].toString()),
isCancelled: false,
isLelantus: false,
slateId: "",
otherData: "",
inputs: [],
outputs: [],
nonce: 0,
numberOfMessages: null,
);
var theAddress = Address(
walletId: walletId,
value: opJson["receiver"].toString(),
publicKey: [], // TODO: Add public key
derivationIndex: 0,
derivationPath: null,
type: AddressType.unknown,
subType: AddressSubType.unknown,
);
txs.add(Tuple2(theTx, theAddress));
}
}
} }
final amount = tx["amount"] as int;
final fee = tx["bakerFee"] as int;
final amountInMicroTez =
amount / 1000000;
final feeInMicroTez =
fee / 1000000;
var theTx = Transaction(
walletId: walletId,
txid: tx["hash"].toString(),
timestamp: DateTime.parse(tx["timestamp"].toString())
.toUtc()
.millisecondsSinceEpoch ~/
1000,
type: txType,
subType: TransactionSubType.none,
amount: tx["amount"] as int,
amountString: Amount(
rawValue: BigInt.parse(
(tx["amount"] as int)
.toInt()
.toString()),
fractionDigits: coin.decimals)
.toJsonString(),
fee: tx["bakerFee"] as int,
height: int.parse(tx["level"].toString()),
isCancelled: false,
isLelantus: false,
slateId: "",
otherData: "",
inputs: [],
outputs: [],
nonce: 0,
numberOfMessages: null,
);
var theAddress = Address(
walletId: walletId,
value: tx["target"]["address"].toString(),
publicKey: [], // TODO: Add public key
derivationIndex: 0,
derivationPath: null,
type: AddressType.unknown,
subType: AddressSubType.unknown,
);
txs.add(Tuple2(theTx, theAddress));
} }
} }
Logging.instance.log("Transactions: $txs", level: LogLevel.Info); Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
@ -505,7 +497,6 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
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));