diff --git a/README.md b/README.md index 12d98cb2..6c2de0d3 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ A Java library for creating Monero applications using RPC and JNI bindings to [m // connect to daemon MoneroDaemon daemon = new MoneroDaemonRpc("http://localhost:38081", "superuser", "abctesting123"); long height = daemon.getHeight(); // 1523651 -BigInteger feeEstimate = daemon.getFeeEstimate(); // 1014313512 List txsInPool = daemon.getTxPool(); // get transactions in the pool // open wallet on monero-wallet-rpc diff --git a/src/main/java/monero/daemon/MoneroDaemon.java b/src/main/java/monero/daemon/MoneroDaemon.java index 8e89aed4..ef5d2199 100644 --- a/src/main/java/monero/daemon/MoneroDaemon.java +++ b/src/main/java/monero/daemon/MoneroDaemon.java @@ -37,6 +37,7 @@ import monero.daemon.model.MoneroDaemonSyncInfo; import monero.daemon.model.MoneroDaemonUpdateCheckResult; import monero.daemon.model.MoneroDaemonUpdateDownloadResult; +import monero.daemon.model.MoneroFeeEstimate; import monero.daemon.model.MoneroHardForkInfo; import monero.daemon.model.MoneroKeyImageSpentStatus; import monero.daemon.model.MoneroMinerTxSum; @@ -310,19 +311,19 @@ public interface MoneroDaemon { public MoneroMinerTxSum getMinerTxSum(long height, Long numBlocks); /** - * Get the fee estimate per kB. + * Get mining fee estimates per kB. * - * @return is the fee estimate per kB. + * @return mining fee estimates per kB */ - public BigInteger getFeeEstimate(); + public MoneroFeeEstimate getFeeEstimate(); /** - * Get the fee estimate per kB. + * Get mining fee estimates per kB. * * @param graceBlocks TODO - * @return is the fee estimate per kB. + * @return mining fee estimates per kB */ - public BigInteger getFeeEstimate(Integer graceBlocks); + public MoneroFeeEstimate getFeeEstimate(Integer graceBlocks); /** * Submits a transaction to the daemon's pool. diff --git a/src/main/java/monero/daemon/MoneroDaemonDefault.java b/src/main/java/monero/daemon/MoneroDaemonDefault.java index 8134b307..8eb83dbf 100644 --- a/src/main/java/monero/daemon/MoneroDaemonDefault.java +++ b/src/main/java/monero/daemon/MoneroDaemonDefault.java @@ -31,6 +31,7 @@ import monero.daemon.model.MoneroBlock; import monero.daemon.model.MoneroBlockTemplate; import monero.daemon.model.MoneroDaemonUpdateDownloadResult; +import monero.daemon.model.MoneroFeeEstimate; import monero.daemon.model.MoneroKeyImageSpentStatus; import monero.daemon.model.MoneroOutputDistributionEntry; import monero.daemon.model.MoneroSubmitTxResult; @@ -83,7 +84,7 @@ public List getTxHexes(Collection txHashes) { } @Override - public BigInteger getFeeEstimate() { + public MoneroFeeEstimate getFeeEstimate() { return getFeeEstimate(null); } diff --git a/src/main/java/monero/daemon/MoneroDaemonRpc.java b/src/main/java/monero/daemon/MoneroDaemonRpc.java index 58f8fed3..6bf0ddc6 100644 --- a/src/main/java/monero/daemon/MoneroDaemonRpc.java +++ b/src/main/java/monero/daemon/MoneroDaemonRpc.java @@ -56,6 +56,7 @@ import monero.daemon.model.MoneroDaemonSyncInfo; import monero.daemon.model.MoneroDaemonUpdateCheckResult; import monero.daemon.model.MoneroDaemonUpdateDownloadResult; +import monero.daemon.model.MoneroFeeEstimate; import monero.daemon.model.MoneroHardForkInfo; import monero.daemon.model.MoneroKeyImage; import monero.daemon.model.MoneroKeyImageSpentStatus; @@ -525,11 +526,15 @@ public MoneroMinerTxSum getMinerTxSum(long height, Long numBlocks) { @SuppressWarnings("unchecked") @Override - public BigInteger getFeeEstimate(Integer graceBlocks) { + public MoneroFeeEstimate getFeeEstimate(Integer graceBlocks) { Map resp = rpc.sendJsonRequest("get_fee_estimate"); Map result = (Map) resp.get("result"); checkResponseStatus(result); - return (BigInteger) result.get("fee"); + List fees = new ArrayList(); + for (BigInteger fee : (List) result.get("fees")) fees.add(fee); + BigInteger fee = (BigInteger) result.get("fee"); + BigInteger quantizationMask = (BigInteger) result.get("quantization_mask"); + return new MoneroFeeEstimate(fee, fees, quantizationMask); } @Override diff --git a/src/main/java/monero/daemon/model/MoneroFeeEstimate.java b/src/main/java/monero/daemon/model/MoneroFeeEstimate.java new file mode 100644 index 00000000..6e1ad6a3 --- /dev/null +++ b/src/main/java/monero/daemon/model/MoneroFeeEstimate.java @@ -0,0 +1,78 @@ +package monero.daemon.model; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +import common.utils.GenUtils; + +/** + * Models a Monero fee estimate. + */ +public class MoneroFeeEstimate { + + private BigInteger fee; + private List fees; + private BigInteger quantizationMask; + + public MoneroFeeEstimate() { + // nothing to construct + } + + public MoneroFeeEstimate(BigInteger fee, List fees, BigInteger quantizationMask) { + this.fee = fee; + this.fees = fees; + this.quantizationMask = quantizationMask; + } + + public MoneroFeeEstimate(MoneroFeeEstimate feeEstimate) { + this.fee = feeEstimate.fee; + this.fees = new ArrayList(feeEstimate.fees); + this.quantizationMask = feeEstimate.quantizationMask; + } + + public BigInteger getFee() { + return fee; + } + + public MoneroFeeEstimate setFee(BigInteger fee) { + this.fee = fee; + return this; + } + + public List getFees() { + return fees; + } + + public MoneroFeeEstimate setFees(List fees) { + this.fees = fees; + return this; + } + + public BigInteger getQuantizationMask() { + return quantizationMask; + } + + public MoneroFeeEstimate setQuantizationMask(BigInteger quantizationMask) { + this.quantizationMask = quantizationMask; + return this; + } + + public MoneroFeeEstimate copy() { + return new MoneroFeeEstimate(this); + } + + @Override + public String toString() { + return toString(0); + } + + public String toString(int indent) { + StringBuilder sb = new StringBuilder(); + sb.append(GenUtils.kvLine("Fee", getFee(), indent)); + sb.append(GenUtils.kvLine("Fees", getFees(), indent)); + sb.append(GenUtils.kvLine("Quantization mask", getQuantizationMask(), indent)); + String str = sb.toString(); + return str.substring(0, str.length() - 1); // strip newline + } +} diff --git a/src/test/java/test/TestMoneroDaemonRpc.java b/src/test/java/test/TestMoneroDaemonRpc.java index b5293da6..0967cf47 100644 --- a/src/test/java/test/TestMoneroDaemonRpc.java +++ b/src/test/java/test/TestMoneroDaemonRpc.java @@ -33,6 +33,7 @@ import monero.daemon.model.MoneroDaemonSyncInfo; import monero.daemon.model.MoneroDaemonUpdateCheckResult; import monero.daemon.model.MoneroDaemonUpdateDownloadResult; +import monero.daemon.model.MoneroFeeEstimate; import monero.daemon.model.MoneroHardForkInfo; import monero.daemon.model.MoneroKeyImage; import monero.daemon.model.MoneroKeyImageSpentStatus; @@ -600,8 +601,11 @@ public void testGetMinerTxSum() { @Test public void testGetFeeEstimate() { assumeTrue(TEST_NON_RELAYS); - BigInteger fee = daemon.getFeeEstimate(); - TestUtils.testUnsignedBigInteger(fee, true); + MoneroFeeEstimate feeEstimate = daemon.getFeeEstimate(); + TestUtils.testUnsignedBigInteger(feeEstimate.getFee(), true); + assertTrue(feeEstimate.getFees().size() == 4); // slow, normal, fast, fastest + for (int i = 0; i < 4; i++) TestUtils.testUnsignedBigInteger(feeEstimate.getFees().get(i), true); + TestUtils.testUnsignedBigInteger(feeEstimate.getQuantizationMask(), true); } // Can get all transactions in the transaction pool diff --git a/src/test/java/test/TestMoneroWalletCommon.java b/src/test/java/test/TestMoneroWalletCommon.java index 57275f29..430382f2 100644 --- a/src/test/java/test/TestMoneroWalletCommon.java +++ b/src/test/java/test/TestMoneroWalletCommon.java @@ -3450,7 +3450,7 @@ public void testSendToMultipleSplit() { @Test public void testSendDustToMultipleSplit() { assumeTrue(TEST_RELAYS); - BigInteger dustAmt = daemon.getFeeEstimate().divide(BigInteger.valueOf(2)); + BigInteger dustAmt = daemon.getFeeEstimate().getFee().divide(BigInteger.valueOf(2)); testSendToMultiple(5, 3, true, dustAmt); } diff --git a/src/test/java/test/TestSampleCode.java b/src/test/java/test/TestSampleCode.java index fcd2e1f4..e19aaf0a 100644 --- a/src/test/java/test/TestSampleCode.java +++ b/src/test/java/test/TestSampleCode.java @@ -55,7 +55,6 @@ public void testSampleCode() throws InterruptedException { // connect to daemon MoneroDaemon daemon = new MoneroDaemonRpc("http://localhost:28081", "", ""); long height = daemon.getHeight(); // 1523651 - BigInteger feeEstimate = daemon.getFeeEstimate(); // 1014313512 List txsInPool = daemon.getTxPool(); // get transactions in the pool // open wallet on monero-wallet-rpc