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/utilities/amount/amount.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:web3dart/web3dart.dart' as web3dart;
@ -69,13 +70,7 @@ class TxData {
bool isChange,
})>? sparkRecipients;
final List<TxData>? sparkMints;
final List<
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>? usedCoins;
final List<SparkCoin>? usedSparkCoins;
final TransactionV2? tempTx;
@ -112,7 +107,7 @@ class TxData {
this.tezosOperationsList,
this.sparkRecipients,
this.sparkMints,
this.usedCoins,
this.usedSparkCoins,
this.tempTx,
});
@ -195,14 +190,7 @@ class TxData {
})>?
sparkRecipients,
List<TxData>? sparkMints,
List<
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>?
usedCoins,
List<SparkCoin>? usedSparkCoins,
TransactionV2? tempTx,
}) {
return TxData(
@ -240,7 +228,7 @@ class TxData {
tezosOperationsList: tezosOperationsList ?? this.tezosOperationsList,
sparkRecipients: sparkRecipients ?? this.sparkRecipients,
sparkMints: sparkMints ?? this.sparkMints,
usedCoins: usedCoins ?? this.usedCoins,
usedSparkCoins: usedSparkCoins ?? this.usedSparkCoins,
tempTx: tempTx ?? this.tempTx,
);
}
@ -279,7 +267,7 @@ class TxData {
'tezosOperationsList: $tezosOperationsList, '
'sparkRecipients: $sparkRecipients, '
'sparkMints: $sparkMints, '
'usedCoins: $usedCoins, '
'usedSparkCoins: $usedSparkCoins, '
'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(
raw: rawTxHex,
vSize: extractedTx.virtualSize(),
@ -523,7 +544,7 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
height: null,
version: 3,
),
usedCoins: spend.usedCoins,
usedSparkCoins: usedSparkCoins,
);
}
@ -545,26 +566,13 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
txid: txHash,
);
// Update coins as used in database.
final List<SparkCoin> usedCoins = [];
for (final usedCoin in txData.usedCoins!) {
// Find the SparkCoin that matches the usedCoin.
final sparkCoin = await mainDB.isar.sparkCoins
.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 used spark coins as used in database. They should already have
// been marked as isUsed.
// TODO: [prio=med] Could (probably should) throw an exception here if txData.usedSparkCoins is null or empty
if (txData.usedSparkCoins != null && txData.usedSparkCoins!.isNotEmpty) {
await _addOrUpdateSparkCoins(txData.usedSparkCoins!);
}
// Update the SparkCoins in the database.
await _addOrUpdateSparkCoins(usedCoins);
return await updateSentCachedTxData(txData: txData);
} catch (e, s) {
Logging.instance.log("Exception rethrown from confirmSend(): $e\n$s",