List of usage examples for com.google.common.util.concurrent Futures addCallback
public static <V> void addCallback(ListenableFuture<V> future, FutureCallback<? super V> callback)
From source file:com.google.vertcoin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();//from ww w . j a v a2s . c o m if (args.length < 2) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.google.zetacoin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();/*from w w w. j ava 2 s . c o m*/ if (args.length < 2) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see zetacoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.google.betacoin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();/*from ww w. jav a2s. co m*/ if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; //if (args[1].equals("testnet")) { // params = TestNet3Params.get(); // filePrefix = "forwarding-service-testnet"; //} else if (args[1].equals("regtest")) { // params = RegTestParams.get(); // filePrefix = "forwarding-service-regtest"; //} else { params = MainNetParams.get(); filePrefix = "forwarding-service"; //} // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see betacoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.google.worldcoin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();//from ww w .j ava2s . c o m if (args.length < 2) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see worldcoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.worldcoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.tribesman.kobocoinj.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();//from ww w . j a v a 2 s .c om if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "kobocoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see kobocoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.kobocoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.google.sha1coin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();//from w ww . java 2 s. c o m if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAsync(); kit.awaitRunning(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). Coin value = tx.getValueSentToMe(w); System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().currentReceiveKey().toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.dogecoin.dogecoinj.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();//from w w w . j a v a 2 s .c o m if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAsync(); kit.awaitRunning(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see dogecoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). Coin value = tx.getValueSentToMe(w); System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() { @Override public void onSuccess(TransactionConfidence result) { forwardCoins(tx); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().currentReceiveKey().toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.google.spartancoin.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();/*from ww w . j a va 2 s. c o m*/ if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.woollysammoth.nubitj.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();/*from w ww. j ava2 s . c om*/ if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "nubitd -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAndWait(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) { // Runs in the dedicated "user thread" (see nubitj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). BigInteger value = tx.getValueSentToMe(w); System.out.println("Received tx for " + Utils.nubitValueToFriendlyString(value) + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }
From source file:com.matthewmitchell.nubitsj.examples.ForwardingService.java
public static void main(String[] args) throws Exception { // This line makes the log output more compact and easily read, especially when using the JDK log adapter. BriefLogFormatter.init();/*from w w w .j a va2s . c om*/ if (args.length < 1) { System.err.println("Usage: address-to-send-back-to [regtest|testnet]"); return; } // Figure out which network we should connect to. Each one gets its own set of files. NetworkParameters params; String filePrefix; if (args.length > 1 && args[1].equals("testnet")) { params = TestNet3Params.get(); filePrefix = "forwarding-service-testnet"; } else if (args.length > 1 && args[1].equals("regtest")) { params = RegTestParams.get(); filePrefix = "forwarding-service-regtest"; } else { params = MainNetParams.get(); filePrefix = "forwarding-service"; } // Parse the address given as the first parameter. forwardingAddress = new Address(params, args[0]); // Start up a basic app using a class that automates some boilerplate. kit = new WalletAppKit(params, new File("."), filePrefix); if (params == RegTestParams.get()) { // Regression test mode is designed for testing and development only, so there's no public network for it. // If you pick this mode, you're expected to be running a local "nubitsd -regtest" instance. kit.connectToLocalHost(); } // Download the block chain and wait until it's done. kit.startAsync(); kit.awaitRunning(); // We want to know when we receive money. kit.wallet().addEventListener(new AbstractWalletEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see nubitsj docs for more info on this). // // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast). Coin value = tx.getValueSentToMe(w); System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx); System.out.println("Transaction will be forwarded after it confirms."); // Wait until it's made it into the block chain (may run immediately if it's already there). // // For this dummy app of course, we could just forward the unconfirmed transaction. If it were // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common // case of waiting for a block. Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() { @Override public void onSuccess(Transaction result) { // "result" here is the same as "tx" above, but we use it anyway for clarity. forwardCoins(result); } @Override public void onFailure(Throwable t) { // This kind of future can't fail, just rethrow in case something weird happens. throw new RuntimeException(t); } }); } }); Address sendToAddress = kit.wallet().currentReceiveKey().toAddress(params); System.out.println("Send coins to: " + sendToAddress); System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit."); try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) { } }