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

Wallets: Include Unconfirmed Balance in getBalance method #279

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public BitcoindGetAddressInfoResponse getAddressInfo(String address) {
}

public double getBalance() {
var rpcCall = new BitcoindGetBalanceRpcCall();
return rpcClient.invokeAndValidate(rpcCall);
var rpcCall = new BitcoindGetBalancesRpcCall();
BitcoindGetBalancesResponse response = rpcClient.invokeAndValidate(rpcCall);
BitcoindGetMineBalancesResponse mineBalancesResponse = response.getMine();
return mineBalancesResponse.getTrusted() + mineBalancesResponse.getUntrustedPending();
}

public String getNewAddress(AddressType addressType, String label) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@

package bisq.wallets.bitcoind.rpc.calls;

import bisq.wallets.bitcoind.rpc.responses.BitcoindGetBalancesResponse;
import bisq.wallets.rpc.call.WalletRpcCall;

public class BitcoindGetBalanceRpcCall extends WalletRpcCall<Void, Double> {
public BitcoindGetBalanceRpcCall() {
public class BitcoindGetBalancesRpcCall extends WalletRpcCall<Void, BitcoindGetBalancesResponse> {
public BitcoindGetBalancesRpcCall() {
super(null);
}

@Override
public String getRpcMethodName() {
return "getbalance";
return "getbalances";
}

@Override
public boolean isResponseValid(Double response) {
public boolean isResponseValid(BitcoindGetBalancesResponse response) {
return true;
}

@Override
public Class<Double> getRpcResponseClass() {
return Double.class;
public Class<BitcoindGetBalancesResponse> getRpcResponseClass() {
return BitcoindGetBalancesResponse.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.bitcoind.rpc.responses;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BitcoindGetBalancesResponse {
private BitcoindGetMineBalancesResponse mine;
@JsonProperty("watchonly")
private BitcoindGetMineBalancesResponse watchOnly;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.bitcoind.rpc.responses;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BitcoindGetMineBalancesResponse {
private double trusted;
@JsonProperty("untrusted_pending")
private double untrustedPending;
private double immature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ public double getLBtcBalance() {
}

public double getAssetBalance(String assetLabel) {
var request = new ElementsdGetBalanceRpcCall.Request(assetLabel);
var rpcCall = new ElementsdGetBalanceRpcCall(request);
return rpcClient.invokeAndValidate(rpcCall);
var rpcCall = new ElementsdGetBalancesRpcCall();
ElementsdGetBalancesResponse response = rpcClient.invokeAndValidate(rpcCall);
ElementsdGetMineBalancesResponse mineBalancesResponse = response.getMine();

double trustedBalance = mineBalancesResponse.getTrusted().getOrDefault(assetLabel, 0.);
double pendingBalance = mineBalancesResponse.getUntrustedPending().getOrDefault(assetLabel, 0.);
return trustedBalance + pendingBalance;
}

public ElementsdGetAddressInfoResponse getAddressInfo(String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,29 @@

package bisq.wallets.elementsd.rpc.calls;

import bisq.wallets.elementsd.rpc.responses.ElementsdGetBalancesResponse;
import bisq.wallets.rpc.call.WalletRpcCall;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

public class ElementsdGetBalanceRpcCall extends WalletRpcCall<ElementsdGetBalanceRpcCall.Request, Double> {
public class ElementsdGetBalancesRpcCall extends WalletRpcCall<Void, ElementsdGetBalancesResponse> {

@Getter
public static class Request {
@JsonProperty("assetlabel")
private final String assetLabel;

public Request(String assetLabel) {
this.assetLabel = assetLabel;
}
}

public ElementsdGetBalanceRpcCall(Request request) {
super(request);
public ElementsdGetBalancesRpcCall() {
super(null);
}

@Override
public String getRpcMethodName() {
return "getbalance";
return "getbalances";
}

@Override
public boolean isResponseValid(Double response) {
public boolean isResponseValid(ElementsdGetBalancesResponse response) {
return true;
}

@Override
public Class<Double> getRpcResponseClass() {
return Double.class;
public Class<ElementsdGetBalancesResponse> getRpcResponseClass() {
return ElementsdGetBalancesResponse.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.elementsd.rpc.responses;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ElementsdGetBalancesResponse {
private ElementsdGetMineBalancesResponse mine;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.elementsd.rpc.responses;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class ElementsdGetMineBalancesResponse {
private Map<String, Double> trusted;
@JsonProperty("untrusted_pending")
private Map<String, Double> untrustedPending;
private Map<String, Double> immature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,28 @@

import bisq.wallets.AddressType;
import bisq.wallets.bitcoind.rpc.BitcoindWallet;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.MalformedURLException;

import static org.assertj.core.api.Assertions.assertThat;

public class BitcoindSendIntegrationTests extends SharedBitcoindInstanceTests {
@Test
public void sendOneBtcToAddress() throws MalformedURLException {

private BitcoindWallet receiverBackend;

@Override
@BeforeAll
public void start() throws IOException, InterruptedException {
super.start();
regtestSetup.mineInitialRegtestBlocks();
BitcoindWallet minerWallet = regtestSetup.getMinerWallet();
var receiverBackend = regtestSetup.createNewWallet("receiver_wallet");
receiverBackend = regtestSetup.createNewWallet("receiver_wallet");
}

@Test
public void sendOneBtcToAddress() {
String receiverAddress = receiverBackend.getNewAddress(AddressType.BECH32, "");
minerWallet.sendToAddress(receiverAddress, 1);
regtestSetup.mineOneBlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.bitcoind;

import bisq.wallets.AddressType;
import bisq.wallets.bitcoind.rpc.BitcoindWallet;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

public class BitcoindSendUnconfirmedTxIntegrationTests extends SharedBitcoindInstanceTests {

private BitcoindWallet receiverBackend;

@Override
@BeforeAll
public void start() throws IOException, InterruptedException {
super.start();
regtestSetup.mineInitialRegtestBlocks();
receiverBackend = regtestSetup.createNewWallet("receiver_wallet");
}

@Test
public void sendBtcAndCheckIfUnconfirmedBalanceIncluded() {
String receiverAddress = receiverBackend.getNewAddress(AddressType.BECH32, "");
minerWallet.sendToAddress(receiverAddress, 1);

assertThat(receiverBackend.getBalance())
.isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.wallets.elementsd;

import bisq.wallets.AddressType;
import org.junit.jupiter.api.Test;

import java.net.MalformedURLException;

import static org.assertj.core.api.Assertions.assertThat;

public class ElementsdSendUnconfirmedTxIntegrationTests extends SharedElementsdInstanceTests {
@Test
public void sendOneLBtcToAddress() throws MalformedURLException {
peginBtc(20);
var receiverBackend = elementsdRegtestSetup.createNewWallet("receiver_wallet");

String receiverAddress = receiverBackend.getNewAddress(AddressType.BECH32, "");
elementsdMinerWallet.sendLBtcToAddress(receiverAddress, 1);

assertThat(receiverBackend.getLBtcBalance())
.isEqualTo(1);
}
}