mirror of
https://github.com/Qortal/qortal.git
synced 2025-02-12 18:25:49 +00:00
Calculate wallet balances from the transactions (ElectrumX) rather than using bitcoinj.
Hopeful fix for incorrect balances in wallets with large numbers of transactions. At the very least, this gives us control of the code that calculates the balance.
This commit is contained in:
parent
077165b807
commit
892612c084
@ -67,11 +67,16 @@ public class CrossChainBitcoinResource {
|
|||||||
if (!bitcoin.isValidDeterministicKey(key58))
|
if (!bitcoin.isValidDeterministicKey(key58))
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
||||||
|
|
||||||
Long balance = bitcoin.getWalletBalance(key58);
|
try {
|
||||||
if (balance == null)
|
Long balance = bitcoin.getWalletBalanceFromTransactions(key58);
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
if (balance == null)
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
|
||||||
return balance.toString();
|
return balance.toString();
|
||||||
|
|
||||||
|
} catch (ForeignBlockchainException e) {
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
@ -65,11 +65,16 @@ public class CrossChainDogecoinResource {
|
|||||||
if (!dogecoin.isValidDeterministicKey(key58))
|
if (!dogecoin.isValidDeterministicKey(key58))
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
||||||
|
|
||||||
Long balance = dogecoin.getWalletBalance(key58);
|
try {
|
||||||
if (balance == null)
|
Long balance = dogecoin.getWalletBalanceFromTransactions(key58);
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
if (balance == null)
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
|
||||||
return balance.toString();
|
return balance.toString();
|
||||||
|
|
||||||
|
} catch (ForeignBlockchainException e) {
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
@ -67,11 +67,16 @@ public class CrossChainLitecoinResource {
|
|||||||
if (!litecoin.isValidDeterministicKey(key58))
|
if (!litecoin.isValidDeterministicKey(key58))
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_PRIVATE_KEY);
|
||||||
|
|
||||||
Long balance = litecoin.getWalletBalance(key58);
|
try {
|
||||||
if (balance == null)
|
Long balance = litecoin.getWalletBalanceFromTransactions(key58);
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
if (balance == null)
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
|
||||||
return balance.toString();
|
return balance.toString();
|
||||||
|
|
||||||
|
} catch (ForeignBlockchainException e) {
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
@ -337,6 +337,21 @@ public abstract class Bitcoiny implements ForeignBlockchain {
|
|||||||
return balance.value;
|
return balance.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getWalletBalanceFromTransactions(String key58) throws ForeignBlockchainException {
|
||||||
|
long balance = 0;
|
||||||
|
Comparator<SimpleTransaction> oldestTimestampFirstComparator = Comparator.comparingInt(SimpleTransaction::getTimestamp);
|
||||||
|
List<SimpleTransaction> transactions = getWalletTransactions(key58).stream().sorted(oldestTimestampFirstComparator).collect(Collectors.toList());
|
||||||
|
for (SimpleTransaction transaction : transactions) {
|
||||||
|
balance += transaction.getTotalAmount();
|
||||||
|
|
||||||
|
if (transaction.getTotalAmount() < 0) {
|
||||||
|
// Outgoing transaction - so this wallet paid the fee
|
||||||
|
balance -= transaction.getFeeAmount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
public List<SimpleTransaction> getWalletTransactions(String key58) throws ForeignBlockchainException {
|
public List<SimpleTransaction> getWalletTransactions(String key58) throws ForeignBlockchainException {
|
||||||
Context.propagate(bitcoinjContext);
|
Context.propagate(bitcoinjContext);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user