Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitcoinjRegtestSetup: Implement wallet funding #7319

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions core/src/integrationTest/java/bisq/core/BitcoinjRegtestSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package bisq.core;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.wallet.Wallet;
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;



import bisq.wallets.regtest.bitcoind.BitcoindRegtestSetup;

public class BitcoinjRegtestSetup {
private final BitcoindRegtestSetup bitcoindRegtestSetup;

public BitcoinjRegtestSetup(BitcoindRegtestSetup bitcoindRegtestSetup) {
this.bitcoindRegtestSetup = bitcoindRegtestSetup;
}

public void fundWallet(Wallet wallet, double amount) throws InterruptedException {
var walletReceivedLatch = new CountDownLatch(1);
WalletCoinsReceivedEventListener coinsReceivedEventListener =
(affectedWallet, tx, prevBalance, newBalance) -> {
walletReceivedLatch.countDown();
};

wallet.addCoinsReceivedEventListener(coinsReceivedEventListener);
Coin previousBalance = wallet.getBalance();

Address currentReceiveAddress = wallet.currentReceiveAddress();
String address = currentReceiveAddress.toString();
bitcoindRegtestSetup.fundAddress(address, amount);
bitcoindRegtestSetup.mineOneBlock();

boolean isSuccess = walletReceivedLatch.await(30, TimeUnit.SECONDS);
wallet.removeCoinsReceivedEventListener(coinsReceivedEventListener);
if (!isSuccess) {
throw new IllegalStateException("Wallet not funded after 30 seconds.");
}

Coin balance = wallet.getBalance();
Coin receivedAmount = balance.minus(previousBalance);

long receivedAmountAsLong = receivedAmount.value;
long fundedAmount = (long) amount * 100_000_000;
if (receivedAmount.value != fundedAmount) {
throw new IllegalStateException("Wallet balance is " + receivedAmountAsLong +
" but should be " + fundedAmount + ".");
}
}
}
Loading