Skip to content

Commit

Permalink
Fixes raw amount parser when an amount like 0.00000001 is valid when …
Browse files Browse the repository at this point in the history
…max decimals is 8.

SLP txs now go to actual change addresses
  • Loading branch information
pokkst committed May 16, 2020
1 parent 3831ae2 commit 1c016a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
16 changes: 5 additions & 11 deletions core/src/main/java/org/bitcoinj/core/slp/SlpTxBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class SlpTxBuilder {

// Send our token change back to our SLP address
if (it.quantities.size == 2) {
req.tx.addOutput(slpAppKit.wallet.params.minNonDustOutput, CashAddress.fromCashAddress(slpAppKit.wallet.params, slpAppKit.freshSlpReceiveAddress().toCashAddress()))
req.tx.addOutput(slpAppKit.wallet.params.minNonDustOutput, CashAddress.fromCashAddress(slpAppKit.wallet.params, slpAppKit.freshSlpChangeAddress().toCashAddress()))
}

// Send our BCH change back to our BCH address
if (it.changeSatoshi >= DUST_LIMIT) {
req.tx.addOutput(Coin.valueOf(it.changeSatoshi), slpAppKit.wallet.freshReceiveAddress())
req.tx.addOutput(Coin.valueOf(it.changeSatoshi), slpAppKit.wallet.freshChangeAddress())
}

it.selectedUtxos.forEach { req.tx.addInput(it) }
Expand Down Expand Up @@ -122,15 +122,9 @@ class SlpTxBuilder {

fun toRawAmount(amount: BigDecimal, slpToken: SlpToken): ULong {
var amt = amount
if (amt > maxRawAmount) {
throw IllegalArgumentException("amount larger than 8 unsigned bytes")
} else if (amt.scale() > slpToken.decimals) {
if(slpToken.decimals == 0) {
amt = amount.toInt().toBigDecimal()
} else {
throw IllegalArgumentException("${slpToken.ticker} supports maximum ${slpToken.decimals} decimals but amount is $amount")
}
}
if (amt > maxRawAmount) { throw IllegalArgumentException("amount larger than 8 unsigned bytes") }
else if(slpToken.decimals == 0) { amt = amount.toInt().toBigDecimal() }
else if (amt.scale() - 1 > slpToken.decimals) { throw IllegalArgumentException("${slpToken.ticker} supports maximum ${slpToken.decimals} decimals but amount is $amount") }
return amt.scaleByPowerOfTen(slpToken.decimals).toLong().toULong()
}

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/bitcoinj/kits/SlpAppKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,18 @@ public SlpAddress currentSlpReceiveAddress() {
return SlpAddress.fromCashAddr(this.wallet.getParams(), this.wallet.currentReceiveAddress().toString());
}

public SlpAddress currentSlpChangeAddress() {
return SlpAddress.fromCashAddr(this.wallet.getParams(), this.wallet.currentChangeAddress().toString());
}

public SlpAddress freshSlpReceiveAddress() {
return SlpAddress.fromCashAddr(this.wallet.getParams(), this.wallet.freshReceiveAddress().toString());
}

public SlpAddress freshSlpChangeAddress() {
return SlpAddress.fromCashAddr(this.wallet.getParams(), this.wallet.freshChangeAddress().toString());
}

public void setDiscovery(@Nullable PeerDiscovery discovery) {
this.discovery = discovery;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/bitcoinj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ public Address currentChangeAddress() {
return currentAddress(KeyChain.KeyPurpose.CHANGE);
}

public CashAddress freshChangeAddress() {
return freshAddress(KeyChain.KeyPurpose.CHANGE);
}

/**
* <p>Imports the given ECKey to the wallet.</p>
*
Expand Down

0 comments on commit 1c016a5

Please sign in to comment.