Skip to content

Commit

Permalink
wallets: Remove dependency to Bisq 2 Coin Class
Browse files Browse the repository at this point in the history
We can't depend on Bisq 2's Coin class to be able to reuse the wallets
library in Bisq 1.
  • Loading branch information
alvasw committed Sep 10, 2024
1 parent 31c1b3e commit 1a213c0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public WalletTransactionListItem(Transaction transaction) {
dateString = DateFormatter.formatDate(date);
timeString = DateFormatter.formatTime(date);
txId = transaction.getTxId();
amount = transaction.getAmount();
amount = Coin.asBtcFromValue(transaction.getAmount());
amountAsString = AmountFormatter.formatAmount(amount);
confirmations = transaction.getConfirmations();
confirmationsAsString = String.valueOf(confirmations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ public void sendBtcAndListTxs() throws MalformedURLException, InterruptedExcepti
BitcoindListTransactionsResponse.Entry firstTx = txs.getFirst();
assertEquals(firstTxReceiverAddress, firstTx.getAddress());
assertEquals("receive", firstTx.getCategory());
assertEquals(100000000, firstTx.getAmount().getValue());
assertEquals(100000000, firstTx.getAmount());
assertEquals(1, firstTx.getConfirmations());
assertEquals(0, firstTx.getWalletconflicts().length);
assertEquals("no", firstTx.getBip125Replaceable());

BitcoindListTransactionsResponse.Entry secondTx = txs.get(1);
assertEquals(secondTxReceiverAddress, secondTx.getAddress());
assertEquals("receive", secondTx.getCategory());
assertEquals(100000000, secondTx.getAmount().getValue());
assertEquals(100000000, secondTx.getAmount());
assertEquals(1, secondTx.getConfirmations());
assertEquals(0, secondTx.getWalletconflicts().length);
assertEquals("no", secondTx.getBip125Replaceable());

BitcoindListTransactionsResponse.Entry thirdTx = txs.get(2);
assertEquals(thirdTxReceiverAddress, thirdTx.getAddress());
assertEquals("receive", thirdTx.getCategory());
assertEquals(100000000, thirdTx.getAmount().getValue());
assertEquals(100000000, thirdTx.getAmount());
assertEquals(1, thirdTx.getConfirmations());
assertEquals(0, thirdTx.getWalletconflicts().length);
assertEquals("no", thirdTx.getBip125Replaceable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package bisq.wallets.bitcoind.rpc.responses;

import bisq.common.monetary.Coin;
import bisq.wallets.core.model.TransactionInfo;
import bisq.wallets.json_rpc.JsonRpcResponse;
import com.squareup.moshi.Json;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -59,13 +59,21 @@ public static class Entry implements TransactionInfo {
private boolean abandoned;

@Override
public Coin getAmount() {
return Coin.asBtcFromFaceValue(amount);
public long getAmount() {
return doubleValueToLong(amount, 8);
}

@Override
public Optional<Date> getDate() {
return Optional.of(new Date(time * 1000L));
}
}

private static long doubleValueToLong(double value, int precision) {
double max = BigDecimal.valueOf(Long.MAX_VALUE).movePointLeft(precision).doubleValue();
if (value > max) {
throw new ArithmeticException("Provided value would lead to an overflow");
}
return BigDecimal.valueOf(value).movePointRight(precision).longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.wallets.core.model;

import bisq.common.monetary.Coin;
import lombok.EqualsAndHashCode;
import lombok.Getter;

Expand All @@ -35,7 +34,7 @@ public class Transaction {
private final int height;
private final Optional<Date> date;
private final int confirmations;
private final Coin amount;
private final long amount;
private final boolean incoming;

public Transaction(String txId,
Expand All @@ -45,7 +44,7 @@ public Transaction(String txId,
int height,
Optional<Date> date,
int confirmations,
Coin amount,
long amount,
boolean incoming) {

this.txId = txId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

package bisq.wallets.core.model;

import bisq.common.monetary.Coin;

import java.util.Date;
import java.util.Optional;

public interface TransactionInfo {
String getTxId();

Coin getAmount();
long getAmount();

int getConfirmations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.wallets.electrum.rpc.responses;

import bisq.common.monetary.Coin;
import bisq.wallets.core.model.TransactionInfo;
import com.squareup.moshi.Json;
import lombok.Getter;
Expand Down Expand Up @@ -56,8 +55,8 @@ public class ElectrumOnChainTransactionResponse implements TransactionInfo {
private Integer txPosInBlock;

@Override
public Coin getAmount() {
return Coin.parseBtc(bcValue);
public long getAmount() {
return Long.parseLong(bcBalance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void sendLBtcAndListTxs() throws InterruptedException {
ElementsdListTransactionsResponse.Entry firstTx = txs.getFirst();
assertEquals(firstTxId, firstTx.getTxId());
assertEquals("receive", firstTx.getCategory());
assertEquals(100000000, firstTx.getAmount().getValue());
assertEquals(100000000, firstTx.getAmount());
assertEquals(1, firstTx.getConfirmations());
assertEquals(3, firstTx.getBlockheight());
assertEquals(0, firstTx.getWalletconflicts().length);
Expand All @@ -59,7 +59,7 @@ public void sendLBtcAndListTxs() throws InterruptedException {
ElementsdListTransactionsResponse.Entry secondTx = txs.get(1);
assertEquals(secondTxId, secondTx.getTxId());
assertEquals("receive", secondTx.getCategory());
assertEquals(100000000, secondTx.getAmount().getValue());
assertEquals(100000000, secondTx.getAmount());
assertEquals(1, secondTx.getConfirmations());
assertEquals(3, secondTx.getBlockheight());
assertEquals(0, secondTx.getWalletconflicts().length);
Expand All @@ -68,7 +68,7 @@ public void sendLBtcAndListTxs() throws InterruptedException {
ElementsdListTransactionsResponse.Entry thirdTx = txs.get(2);
assertEquals(thirdTxId, thirdTx.getTxId());
assertEquals("receive", thirdTx.getCategory());
assertEquals(100000000, thirdTx.getAmount().getValue());
assertEquals(100000000, thirdTx.getAmount());
assertEquals(1, thirdTx.getConfirmations());
assertEquals(3, thirdTx.getBlockheight());
assertEquals(0, thirdTx.getWalletconflicts().length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.wallets.bitcoind;

import bisq.common.monetary.Coin;
import bisq.common.observable.collection.ObservableSet;
import bisq.wallets.bitcoind.rpc.BitcoindDaemon;
import bisq.wallets.bitcoind.rpc.BitcoindWallet;
Expand Down Expand Up @@ -100,7 +99,6 @@ public List<Transaction> getTransactions() {
List<TransactionInput> inputs = new ArrayList<>();
List<TransactionOutput> outputs = new ArrayList<>();
int lockTime = 0;
Coin amount = Coin.asBtcFromValue(0);
boolean incoming = true;
return new Transaction(tx.getTxId(),
inputs,
Expand All @@ -109,7 +107,7 @@ public List<Transaction> getTransactions() {
tx.getBlockheight(),
Optional.of(new Date(tx.getTime() * 1000L)),
tx.getConfirmations(),
amount,
tx.getAmount(),
incoming);
})
.collect(Collectors.toList());
Expand Down

0 comments on commit 1a213c0

Please sign in to comment.