3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 03:35:52 +00:00

ForwardingService: Fix hardcoded fee by simply emptying the entire wallet to the forwarding address. This will make sure the library is calculating the correct fee.

This commit is contained in:
Andreas Schildbach 2016-11-26 18:18:29 +01:00
parent 8ee5e48171
commit 7e9c45ad55

View File

@ -30,6 +30,7 @@ import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.RegTestParams; import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params; import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.utils.BriefLogFormatter; import org.bitcoinj.utils.BriefLogFormatter;
import org.bitcoinj.wallet.SendRequest;
import org.bitcoinj.wallet.Wallet; import org.bitcoinj.wallet.Wallet;
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener; import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;
@ -73,6 +74,9 @@ public class ForwardingService {
// Parse the address given as the first parameter. // Parse the address given as the first parameter.
forwardingAddress = LegacyAddress.fromBase58(params, args[0]); 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. // Start up a basic app using a class that automates some boilerplate.
kit = new WalletAppKit(params, new File("."), filePrefix); kit = new WalletAppKit(params, new File("."), filePrefix);
@ -105,6 +109,7 @@ public class ForwardingService {
Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() { Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
@Override @Override
public void onSuccess(TransactionConfidence result) { public void onSuccess(TransactionConfidence result) {
System.out.println("Confirmation received.");
forwardCoins(tx); forwardCoins(tx);
} }
@ -128,11 +133,9 @@ public class ForwardingService {
private static void forwardCoins(Transaction tx) { private static void forwardCoins(Transaction tx) {
try { try {
Coin value = tx.getValueSentToMe(kit.wallet()); // Now send the coins onwards.
System.out.println("Forwarding " + value.toFriendlyString()); SendRequest sendRequest = SendRequest.emptyWallet(forwardingAddress);
// Now send the coins back! Send with a small fee attached to ensure rapid confirmation. Wallet.SendResult sendResult = kit.wallet().sendCoins(sendRequest);
final Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
checkNotNull(sendResult); // We should never try to send more coins than we have! checkNotNull(sendResult); // We should never try to send more coins than we have!
System.out.println("Sending ..."); System.out.println("Sending ...");
// Register a callback that is invoked when the transaction has propagated across the network. // Register a callback that is invoked when the transaction has propagated across the network.