Handle case where funds are sent to and from the same bitcoiny deterministic wallet.

This commit is contained in:
CalDescent 2022-02-19 17:45:24 +00:00
parent 6d0db7cc5e
commit 5d419dd4ec

View File

@ -476,6 +476,7 @@ public abstract class Bitcoiny implements ForeignBlockchain {
List<SimpleTransaction.Output> outputs = new ArrayList<>(); List<SimpleTransaction.Output> outputs = new ArrayList<>();
boolean anyOutputAddressInWallet = false; boolean anyOutputAddressInWallet = false;
boolean transactionInvolvesExternalWallet = false;
for (BitcoinyTransaction.Input input : t.inputs) { for (BitcoinyTransaction.Input input : t.inputs) {
try { try {
@ -490,6 +491,9 @@ public abstract class Bitcoiny implements ForeignBlockchain {
total += inputAmount; total += inputAmount;
addressInWallet = true; addressInWallet = true;
} }
else {
transactionInvolvesExternalWallet = true;
}
inputs.add(new SimpleTransaction.Input(sender, inputAmount, addressInWallet)); inputs.add(new SimpleTransaction.Input(sender, inputAmount, addressInWallet));
} }
} }
@ -503,14 +507,17 @@ public abstract class Bitcoiny implements ForeignBlockchain {
for (String address : output.addresses) { for (String address : output.addresses) {
boolean addressInWallet = false; boolean addressInWallet = false;
if (keySet.contains(address)) { if (keySet.contains(address)) {
if (total > 0L) { if (total > 0L) { // Change returned from sent amount
amount -= (total - output.value); amount -= (total - output.value);
} else { } else { // Amount received
amount += output.value; amount += output.value;
} }
addressInWallet = true; addressInWallet = true;
anyOutputAddressInWallet = true; anyOutputAddressInWallet = true;
} }
else {
transactionInvolvesExternalWallet = true;
}
outputs.add(new SimpleTransaction.Output(address, output.value, addressInWallet)); outputs.add(new SimpleTransaction.Output(address, output.value, addressInWallet));
} }
} }
@ -525,6 +532,10 @@ public abstract class Bitcoiny implements ForeignBlockchain {
amount = total * -1; amount = total * -1;
} }
} }
else if (!transactionInvolvesExternalWallet) {
// All inputs and outputs relate to this wallet, so the balance should be unaffected
amount = 0;
}
return new SimpleTransaction(t.txHash, t.timestamp, amount, fee, inputs, outputs); return new SimpleTransaction(t.txHash, t.timestamp, amount, fee, inputs, outputs);
} }