Split wallet events into single method interfaces

This commit is contained in:
Ross Nicoll
2016-01-31 21:24:40 +00:00
committed by Andreas Schildbach
parent 8841371922
commit aca39ee9de
28 changed files with 753 additions and 244 deletions

View File

@@ -17,7 +17,6 @@
package org.bitcoinj.examples;
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
import org.bitcoinj.core.*;
import org.bitcoinj.crypto.KeyCrypterException;
import org.bitcoinj.kits.WalletAppKit;
@@ -32,6 +31,7 @@ import com.google.common.util.concurrent.MoreExecutors;
import java.io.File;
import static com.google.common.base.Preconditions.checkNotNull;
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
/**
* ForwardingService demonstrates basic usage of the library. It sits on the network and when it receives coins, simply
@@ -79,7 +79,7 @@ public class ForwardingService {
kit.awaitRunning();
// We want to know when we receive money.
kit.wallet().addEventListener(new AbstractWalletEventListener() {
kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
@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).

View File

@@ -14,7 +14,6 @@
package org.bitcoinj.examples;
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.TestNet3Params;
@@ -22,6 +21,11 @@ import org.bitcoinj.script.Script;
import java.io.File;
import java.util.List;
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
import org.bitcoinj.core.listeners.WalletCoinsSentEventListener;
import org.bitcoinj.core.listeners.ScriptsChangeEventListener;
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
import org.bitcoinj.wallet.KeyChainEventListener;
/**
* The following example shows how to use the by bitcoinj provided WalletAppKit.
@@ -57,9 +61,43 @@ public class Kit {
kit.startAsync();
kit.awaitRunning();
// To observe wallet events (like coins received) we implement a EventListener class that extends the AbstractWalletEventListener bitcoinj then calls the different functions from the EventListener class
WalletListener wListener = new WalletListener();
kit.wallet().addEventListener(wListener);
kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("-----> coins resceived: " + tx.getHashAsString());
System.out.println("received: " + tx.getValue(wallet));
}
});
kit.wallet().addCoinsSentEventListener(new WalletCoinsSentEventListener() {
@Override
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("coins sent");
}
});
kit.wallet().addKeyChainEventListener(new KeyChainEventListener() {
@Override
public void onKeysAdded(List<ECKey> keys) {
System.out.println("new key added");
}
});
kit.wallet().addScriptsChangeEventListener(new ScriptsChangeEventListener() {
@Override
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
System.out.println("new script added");
}
});
kit.wallet().addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
@Override
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
System.out.println("-----> confidence changed: " + tx.getHashAsString());
TransactionConfidence confidence = tx.getConfidence();
System.out.println("new block depth: " + confidence.getDepthInBlocks());
}
});
// Ready to run. The kit syncs the blockchain and our wallet event listener gets notified when something happens.
// To test everything we create and print a fresh receiving address. Send some coins to that address and see if everything works.
@@ -71,44 +109,4 @@ public class Kit {
//kit.awaitTerminated();
}
// The Wallet event listener its implementations get called on wallet changes.
static class WalletListener extends AbstractWalletEventListener {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("-----> coins resceived: " + tx.getHashAsString());
System.out.println("received: " + tx.getValue(wallet));
}
@Override
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
System.out.println("-----> confidence changed: " + tx.getHashAsString());
TransactionConfidence confidence = tx.getConfidence();
System.out.println("new block depth: " + confidence.getDepthInBlocks());
}
@Override
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("coins sent");
}
@Override
public void onReorganize(Wallet wallet) {
}
@Override
public void onWalletChanged(Wallet wallet) {
}
@Override
public void onKeysAdded(List<ECKey> keys) {
System.out.println("new key added");
}
@Override
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
System.out.println("new script added");
}
}
}

View File

@@ -17,13 +17,13 @@
package org.bitcoinj.examples;
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
import org.bitcoinj.core.*;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.MemoryBlockStore;
import java.io.File;
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
/**
* RefreshWallet loads a wallet, then processes the block chain to update the transaction pools within it.
@@ -42,7 +42,7 @@ public class RefreshWallet {
final PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.startAsync();
wallet.addEventListener(new AbstractWalletEventListener() {
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
@Override
public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
System.out.println("\nReceived tx " + tx.getHashAsString());