mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-27 02:26:44 +00:00
wallet auto bitcoin unit
This commit is contained in:
parent
4e7f0611c4
commit
49a4b548b4
2 changed files with 32 additions and 1 deletions
|
@ -3,12 +3,24 @@ package com.sparrowwallet.drongo;
|
||||||
import com.sparrowwallet.drongo.protocol.Transaction;
|
import com.sparrowwallet.drongo.protocol.Transaction;
|
||||||
|
|
||||||
public enum BitcoinUnit {
|
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") {
|
BTC("BTC") {
|
||||||
@Override
|
@Override
|
||||||
public long getSatsValue(double unitValue) {
|
public long getSatsValue(double unitValue) {
|
||||||
return (long)(unitValue * Transaction.SATOSHIS_PER_BITCOIN);
|
return (long)(unitValue * Transaction.SATOSHIS_PER_BITCOIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public double getValue(long satsValue) {
|
public double getValue(long satsValue) {
|
||||||
return (double)satsValue / Transaction.SATOSHIS_PER_BITCOIN;
|
return (double)satsValue / Transaction.SATOSHIS_PER_BITCOIN;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +31,7 @@ public enum BitcoinUnit {
|
||||||
return (long)unitValue;
|
return (long)unitValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public double getValue(long satsValue) {
|
public double getValue(long satsValue) {
|
||||||
return (double)satsValue;
|
return (double)satsValue;
|
||||||
}
|
}
|
||||||
|
@ -43,6 +56,10 @@ public enum BitcoinUnit {
|
||||||
return getValue(satsValue);
|
return getValue(satsValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static long getAutoThreshold() {
|
||||||
|
return 100000000L;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return label;
|
return label;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.sparrowwallet.drongo.wallet;
|
package com.sparrowwallet.drongo.wallet;
|
||||||
|
|
||||||
|
import com.sparrowwallet.drongo.BitcoinUnit;
|
||||||
import com.sparrowwallet.drongo.KeyPurpose;
|
import com.sparrowwallet.drongo.KeyPurpose;
|
||||||
import com.sparrowwallet.drongo.address.Address;
|
import com.sparrowwallet.drongo.address.Address;
|
||||||
import com.sparrowwallet.drongo.crypto.DeterministicKey;
|
|
||||||
import com.sparrowwallet.drongo.crypto.ECKey;
|
import com.sparrowwallet.drongo.crypto.ECKey;
|
||||||
import com.sparrowwallet.drongo.crypto.Key;
|
import com.sparrowwallet.drongo.crypto.Key;
|
||||||
import com.sparrowwallet.drongo.policy.Policy;
|
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);
|
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() {
|
public void clearNodes() {
|
||||||
purposeNodes.clear();
|
purposeNodes.clear();
|
||||||
transactions.clear();
|
transactions.clear();
|
||||||
|
|
Loading…
Reference in a new issue