Skip to content

Commit

Permalink
daemon.getFeeEstimate() returns MoneroFeeEstimate w/ fee, fees, qmask
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Oct 27, 2022
1 parent 091cc6a commit e64b057
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MoneroTx> txsInPool = daemon.getTxPool(); // get transactions in the pool

// open wallet on monero-wallet-rpc
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/monero/daemon/MoneroDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/monero/daemon/MoneroDaemonDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +84,7 @@ public List<String> getTxHexes(Collection<String> txHashes) {
}

@Override
public BigInteger getFeeEstimate() {
public MoneroFeeEstimate getFeeEstimate() {
return getFeeEstimate(null);
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/monero/daemon/MoneroDaemonRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> resp = rpc.sendJsonRequest("get_fee_estimate");
Map<String, Object> result = (Map<String, Object>) resp.get("result");
checkResponseStatus(result);
return (BigInteger) result.get("fee");
List<BigInteger> fees = new ArrayList<BigInteger>();
for (BigInteger fee : (List<BigInteger>) 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
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/monero/daemon/model/MoneroFeeEstimate.java
Original file line number Diff line number Diff line change
@@ -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<BigInteger> fees;
private BigInteger quantizationMask;

public MoneroFeeEstimate() {
// nothing to construct
}

public MoneroFeeEstimate(BigInteger fee, List<BigInteger> fees, BigInteger quantizationMask) {
this.fee = fee;
this.fees = fees;
this.quantizationMask = quantizationMask;
}

public MoneroFeeEstimate(MoneroFeeEstimate feeEstimate) {
this.fee = feeEstimate.fee;
this.fees = new ArrayList<BigInteger>(feeEstimate.fees);
this.quantizationMask = feeEstimate.quantizationMask;
}

public BigInteger getFee() {
return fee;
}

public MoneroFeeEstimate setFee(BigInteger fee) {
this.fee = fee;
return this;
}

public List<BigInteger> getFees() {
return fees;
}

public MoneroFeeEstimate setFees(List<BigInteger> 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
}
}
8 changes: 6 additions & 2 deletions src/test/java/test/TestMoneroDaemonRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/test/TestMoneroWalletCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/test/java/test/TestSampleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MoneroTx> txsInPool = daemon.getTxPool(); // get transactions in the pool

// open wallet on monero-wallet-rpc
Expand Down

0 comments on commit e64b057

Please sign in to comment.