tweak spark used coins update on successful send

This commit is contained in:
julian 2024-02-28 14:42:32 +07:00
parent a90071f6eb
commit 4c98ee0db3
2 changed files with 33 additions and 37 deletions

View file

@ -5,6 +5,7 @@ import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/models/paynym/paynym_account_lite.dart'; import 'package:stackwallet/models/paynym/paynym_account_lite.dart';
import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart'; import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
import 'package:stackwallet/wallets/isar/models/spark_coin.dart';
import 'package:tezart/tezart.dart' as tezart; import 'package:tezart/tezart.dart' as tezart;
import 'package:web3dart/web3dart.dart' as web3dart; import 'package:web3dart/web3dart.dart' as web3dart;
@ -69,13 +70,7 @@ class TxData {
bool isChange, bool isChange,
})>? sparkRecipients; })>? sparkRecipients;
final List<TxData>? sparkMints; final List<TxData>? sparkMints;
final List< final List<SparkCoin>? usedSparkCoins;
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>? usedCoins;
final TransactionV2? tempTx; final TransactionV2? tempTx;
@ -112,7 +107,7 @@ class TxData {
this.tezosOperationsList, this.tezosOperationsList,
this.sparkRecipients, this.sparkRecipients,
this.sparkMints, this.sparkMints,
this.usedCoins, this.usedSparkCoins,
this.tempTx, this.tempTx,
}); });
@ -195,14 +190,7 @@ class TxData {
})>? })>?
sparkRecipients, sparkRecipients,
List<TxData>? sparkMints, List<TxData>? sparkMints,
List< List<SparkCoin>? usedSparkCoins,
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>?
usedCoins,
TransactionV2? tempTx, TransactionV2? tempTx,
}) { }) {
return TxData( return TxData(
@ -240,7 +228,7 @@ class TxData {
tezosOperationsList: tezosOperationsList ?? this.tezosOperationsList, tezosOperationsList: tezosOperationsList ?? this.tezosOperationsList,
sparkRecipients: sparkRecipients ?? this.sparkRecipients, sparkRecipients: sparkRecipients ?? this.sparkRecipients,
sparkMints: sparkMints ?? this.sparkMints, sparkMints: sparkMints ?? this.sparkMints,
usedCoins: usedCoins ?? this.usedCoins, usedSparkCoins: usedSparkCoins ?? this.usedSparkCoins,
tempTx: tempTx ?? this.tempTx, tempTx: tempTx ?? this.tempTx,
); );
} }
@ -279,7 +267,7 @@ class TxData {
'tezosOperationsList: $tezosOperationsList, ' 'tezosOperationsList: $tezosOperationsList, '
'sparkRecipients: $sparkRecipients, ' 'sparkRecipients: $sparkRecipients, '
'sparkMints: $sparkMints, ' 'sparkMints: $sparkMints, '
'usedCoins: $usedCoins, ' 'usedSparkCoins: $usedSparkCoins, '
'tempTx: $tempTx, ' 'tempTx: $tempTx, '
'}'; '}';
} }

View file

@ -499,6 +499,27 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
), ),
); );
final List<SparkCoin> usedSparkCoins = [];
for (final usedCoin in spend.usedCoins) {
try {
usedSparkCoins.add(coins
.firstWhere((e) =>
usedCoin.height == e.height &&
usedCoin.groupId == e.groupId &&
base64Decode(e.serializedCoinB64!)
.toHex
.startsWith(base64Decode(usedCoin.serializedCoin).toHex))
.copyWith(
isUsed: true,
));
} catch (_) {
throw Exception(
"Unexpectedly did not find used spark coin. This should never happen.",
);
}
}
return txData.copyWith( return txData.copyWith(
raw: rawTxHex, raw: rawTxHex,
vSize: extractedTx.virtualSize(), vSize: extractedTx.virtualSize(),
@ -523,7 +544,7 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
height: null, height: null,
version: 3, version: 3,
), ),
usedCoins: spend.usedCoins, usedSparkCoins: usedSparkCoins,
); );
} }
@ -545,26 +566,13 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
txid: txHash, txid: txHash,
); );
// Update coins as used in database. // Update used spark coins as used in database. They should already have
final List<SparkCoin> usedCoins = []; // been marked as isUsed.
for (final usedCoin in txData.usedCoins!) { // TODO: [prio=med] Could (probably should) throw an exception here if txData.usedSparkCoins is null or empty
// Find the SparkCoin that matches the usedCoin. if (txData.usedSparkCoins != null && txData.usedSparkCoins!.isNotEmpty) {
final sparkCoin = await mainDB.isar.sparkCoins await _addOrUpdateSparkCoins(txData.usedSparkCoins!);
.where()
.walletIdEqualToAnyLTagHash(walletId)
.filter()
.serializedCoinB64EqualTo(usedCoin.serializedCoin)
.findFirst();
// Add the SparkCoin to usedCoins if it exists.
if (sparkCoin != null) {
usedCoins.add(sparkCoin.copyWith(isUsed: true));
}
} }
// Update the SparkCoins in the database.
await _addOrUpdateSparkCoins(usedCoins);
return await updateSentCachedTxData(txData: txData); return await updateSentCachedTxData(txData: txData);
} catch (e, s) { } catch (e, s) {
Logging.instance.log("Exception rethrown from confirmSend(): $e\n$s", Logging.instance.log("Exception rethrown from confirmSend(): $e\n$s",