Skip to content

Commit

Permalink
ForwardingService: Fix hardcoded fee by simply emptying the entire wa…
Browse files Browse the repository at this point in the history
…llet to the forwarding address. This will make sure the library is calculating the correct fee.
  • Loading branch information
Andreas Schildbach committed Mar 13, 2018
1 parent 8ee5e48 commit 7e9c45a
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.utils.BriefLogFormatter;
import org.bitcoinj.wallet.SendRequest;
import org.bitcoinj.wallet.Wallet;
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;

Expand Down Expand Up @@ -73,6 +74,9 @@ public static void main(String[] args) throws Exception {
// Parse the address given as the first parameter.
forwardingAddress = LegacyAddress.fromBase58(params, args[0]);

System.out.println("Network: " + params.getId());
System.out.println("Forwarding address: " + forwardingAddress);

// Start up a basic app using a class that automates some boilerplate.
kit = new WalletAppKit(params, new File("."), filePrefix);

Expand Down Expand Up @@ -105,6 +109,7 @@ public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin new
Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
@Override
public void onSuccess(TransactionConfidence result) {
System.out.println("Confirmation received.");
forwardCoins(tx);
}

Expand All @@ -128,11 +133,9 @@ public void onFailure(Throwable t) {

private static void forwardCoins(Transaction tx) {
try {
Coin value = tx.getValueSentToMe(kit.wallet());
System.out.println("Forwarding " + value.toFriendlyString());
// Now send the coins back! Send with a small fee attached to ensure rapid confirmation.
final Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
// Now send the coins onwards.
SendRequest sendRequest = SendRequest.emptyWallet(forwardingAddress);
Wallet.SendResult sendResult = kit.wallet().sendCoins(sendRequest);
checkNotNull(sendResult); // We should never try to send more coins than we have!
System.out.println("Sending ...");
// Register a callback that is invoked when the transaction has propagated across the network.
Expand Down

0 comments on commit 7e9c45a

Please sign in to comment.