From 49a4b548b4b93aa9b765b0b6f9520d0314b1395f Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 9 Jul 2020 14:34:13 +0200 Subject: [PATCH] wallet auto bitcoin unit --- .../com/sparrowwallet/drongo/BitcoinUnit.java | 17 +++++++++++++++++ .../com/sparrowwallet/drongo/wallet/Wallet.java | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java b/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java index 2e50d9c..b067c87 100644 --- a/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java +++ b/src/main/java/com/sparrowwallet/drongo/BitcoinUnit.java @@ -3,12 +3,24 @@ package com.sparrowwallet.drongo; import com.sparrowwallet.drongo.protocol.Transaction; public enum BitcoinUnit { + AUTO("Auto") { + @Override + public long getSatsValue(double unitValue) { + throw new UnsupportedOperationException("Auto unit cannot convert bitcoin values"); + } + + @Override + public double getValue(long satsValue) { + throw new UnsupportedOperationException("Auto unit cannot convert bitcoin values"); + } + }, BTC("BTC") { @Override public long getSatsValue(double unitValue) { return (long)(unitValue * Transaction.SATOSHIS_PER_BITCOIN); } + @Override public double getValue(long satsValue) { return (double)satsValue / Transaction.SATOSHIS_PER_BITCOIN; } @@ -19,6 +31,7 @@ public enum BitcoinUnit { return (long)unitValue; } + @Override public double getValue(long satsValue) { return (double)satsValue; } @@ -43,6 +56,10 @@ public enum BitcoinUnit { return getValue(satsValue); } + public static long getAutoThreshold() { + return 100000000L; + } + @Override public String toString() { return label; diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index d92a469..2c170cc 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -1,8 +1,8 @@ package com.sparrowwallet.drongo.wallet; +import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.address.Address; -import com.sparrowwallet.drongo.crypto.DeterministicKey; import com.sparrowwallet.drongo.crypto.ECKey; import com.sparrowwallet.drongo.crypto.Key; import com.sparrowwallet.drongo.policy.Policy; @@ -340,6 +340,20 @@ public class Wallet { throw new InsufficientFundsException("Not enough combined value in UTXOs for output value " + targetValue); } + public BitcoinUnit getAutoUnit() { + for(KeyPurpose keyPurpose : KeyPurpose.values()) { + for(WalletNode addressNode : getNode(keyPurpose).getChildren()) { + for(BlockTransactionHashIndex output : addressNode.getTransactionOutputs()) { + if(output.getValue() >= BitcoinUnit.getAutoThreshold()) { + return BitcoinUnit.BTC; + } + } + } + } + + return BitcoinUnit.SATOSHIS; + } + public void clearNodes() { purposeNodes.clear(); transactions.clear();