I need to know when my local wallet receives coins... but onCoinsReceived
is invoked as soon as a transaction on my wallet is visible. How do I get notified when coins have been received and are spendable?
1 Answer
Technically speaking, the coins are spendable immediately; they'll just take longer to spend and cost more in transaction fees. But to know when the coins are confirmed, in onCoinsRecieved:
If you want to know when such a transaction receives its first confirmation, register a TransactionConfidence event listener using the object retrieved via Transaction.getConfidence(). It's safe to modify the wallet in this callback, for example, by spending the transaction just received.
(Source.)
Use something like this:
public void onCoinsReceived(Wallet wallet,
Transaction tx,
Coin prevBalance,
Coin newBalance) {
TransactionConfidence confidence = Transaction.getConfidence();
confidence.addEventListener(new TransactionConfidence.Listener() {
public void onConfidenceChanged(Transaction tx,
TransactionConfidence.Listener.ChangeReason reason) {
DefaultCoinSelector.isSelectable(transaction) {
// do something here
}
}
});
}