Skip to content

Commit

Permalink
Merge branch 'master' into upstream_http-api
Browse files Browse the repository at this point in the history
  • Loading branch information
ManfredKarrer committed Jan 16, 2019
2 parents be9d08f + 6f6f504 commit 0feed88
Show file tree
Hide file tree
Showing 23 changed files with 470 additions and 215 deletions.
36 changes: 36 additions & 0 deletions assets/src/main/java/bisq/asset/coins/Fujicoin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.asset.coins;

import bisq.asset.Base58BitcoinAddressValidator;
import bisq.asset.Coin;
import bisq.asset.NetworkParametersAdapter;

public class Fujicoin extends Coin {
public Fujicoin() {
super("Fujicoin", "FJC", new Base58BitcoinAddressValidator(new FujicoinParams()));
}
public static class FujicoinParams extends NetworkParametersAdapter {

public FujicoinParams() {
addressHeader = 36;
p2shHeader = 16;
acceptableAddressCodes = new int[]{addressHeader, p2shHeader};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bisq.asset.coins.Dragonglass
bisq.asset.coins.Ether
bisq.asset.coins.EtherClassic
bisq.asset.coins.FourtyTwo
bisq.asset.coins.Fujicoin
bisq.asset.coins.GambleCoin
bisq.asset.coins.Gridcoin
bisq.asset.coins.Grin
Expand Down
44 changes: 44 additions & 0 deletions assets/src/test/java/bisq/asset/coins/FujicoinTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.asset.coins;

import bisq.asset.AbstractAssetTest;

import org.junit.Test;

public class FujicoinTest extends AbstractAssetTest {

public FujicoinTest() {
super(new Fujicoin());
}

@Test
public void testValidAddresses() {
assertValidAddress("FpEbvwxhmer2zSvqh61JtLiffu8Tk2abdo");
assertValidAddress("7gcLWi78MFJ9akMzTAiug3uArvPch5LB6q");
assertValidAddress("FrjN1LLWJj1DWVooBCdybBvmaEAqxMuuq8");
}

@Test
public void testInvalidAddresses() {
assertInvalidAddress("17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem");
assertInvalidAddress("FpEbvwxhmer2zSvqh61JtLiffu8Tk2abda");
assertInvalidAddress("7gcLWi78MFJ9akMzTAiug3uArvPch5LB6a");
assertInvalidAddress("fc1q3s2fc2xqgush29urtfdj0vhcj96h8424zyl6wa");
}
}
31 changes: 6 additions & 25 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import bisq.core.alert.PrivateNotificationPayload;
import bisq.core.arbitration.ArbitratorManager;
import bisq.core.arbitration.DisputeManager;
import bisq.core.btc.listeners.BalanceListener;
import bisq.core.btc.Balances;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.model.BalanceModel;
import bisq.core.btc.setup.WalletsSetup;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.WalletsManager;
Expand All @@ -41,14 +40,12 @@
import bisq.core.notifications.alerts.TradeEvents;
import bisq.core.notifications.alerts.market.MarketAlerts;
import bisq.core.notifications.alerts.price.PriceAlert;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.AccountAgeWitnessService;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.provider.fee.FeeService;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeManager;
import bisq.core.trade.statistics.AssetTradeActivityCheck;
import bisq.core.trade.statistics.TradeStatisticsManager;
Expand All @@ -73,7 +70,6 @@
import bisq.common.util.Utilities;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;

import javax.inject.Inject;

Expand Down Expand Up @@ -125,7 +121,7 @@ public interface BisqSetupCompleteListener {
private final WalletsManager walletsManager;
private final WalletsSetup walletsSetup;
private final BtcWalletService btcWalletService;
private final BalanceModel balanceModel;
private final Balances balances;
private final PriceFeedService priceFeedService;
private final ArbitratorManager arbitratorManager;
private final P2PService p2PService;
Expand Down Expand Up @@ -201,7 +197,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
WalletsManager walletsManager,
WalletsSetup walletsSetup,
BtcWalletService btcWalletService,
BalanceModel balanceModel,
Balances balances,
PriceFeedService priceFeedService,
ArbitratorManager arbitratorManager,
P2PService p2PService,
Expand Down Expand Up @@ -239,7 +235,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
this.walletsManager = walletsManager;
this.walletsSetup = walletsSetup;
this.btcWalletService = btcWalletService;
this.balanceModel = balanceModel;
this.balances = balances;
this.priceFeedService = priceFeedService;
this.arbitratorManager = arbitratorManager;
this.p2PService = p2PService;
Expand Down Expand Up @@ -588,29 +584,14 @@ private void initDomainServices() {
disputeManager.onAllServicesInitialized();

tradeManager.onAllServicesInitialized();
tradeManager.getTradableList().addListener((ListChangeListener<Trade>) change -> balanceModel.updateBalance());
tradeManager.getAddressEntriesForAvailableBalanceStream()
.filter(addressEntry -> addressEntry.getOfferId() != null)
.forEach(addressEntry -> {
log.warn("Swapping pending OFFER_FUNDING entries at startup. offerId={}", addressEntry.getOfferId());
btcWalletService.swapTradeEntryToAvailableEntry(addressEntry.getOfferId(), AddressEntry.Context.OFFER_FUNDING);
});

btcWalletService.addBalanceListener(new BalanceListener() {
@Override
public void onBalanceChanged(Coin balance, Transaction tx) {
balanceModel.updateBalance();
}
});

if (walletsSetup.downloadPercentageProperty().get() == 1)
checkForLockedUpFunds();

balanceModel.updateBalance();

openOfferManager.getObservableList().addListener((ListChangeListener<OpenOffer>) c -> balanceModel.updateBalance());
openOfferManager.onAllServicesInitialized();

balances.onAllServicesInitialized();

arbitratorManager.onAllServicesInitialized();

alertManager.alertMessageProperty().addListener((observable, oldValue, newValue) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,38 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.btc.model;
package bisq.core.btc;

import bisq.core.btc.listeners.BalanceListener;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.OpenOfferManager;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeManager;
import bisq.core.trade.closed.ClosedTradableManager;
import bisq.core.trade.failed.FailedTradesManager;

import org.bitcoinj.core.Address;
import bisq.common.UserThread;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;

import javax.inject.Inject;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

import javafx.collections.ListChangeListener;

import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

public class BalanceModel {
@Slf4j
public class Balances {
private final TradeManager tradeManager;
private final BtcWalletService btcWalletService;
private final OpenOfferManager openOfferManager;
Expand All @@ -53,64 +61,65 @@ public class BalanceModel {
private final ObjectProperty<Coin> lockedBalance = new SimpleObjectProperty<>();

@Inject
public BalanceModel(TradeManager tradeManager, BtcWalletService btcWalletService, OpenOfferManager openOfferManager,
ClosedTradableManager closedTradableManager, FailedTradesManager failedTradesManager) {
public Balances(TradeManager tradeManager,
BtcWalletService btcWalletService,
OpenOfferManager openOfferManager,
ClosedTradableManager closedTradableManager,
FailedTradesManager failedTradesManager) {
this.tradeManager = tradeManager;
this.btcWalletService = btcWalletService;
this.openOfferManager = openOfferManager;
this.closedTradableManager = closedTradableManager;
this.failedTradesManager = failedTradesManager;
}

public void updateBalance() {
//TODO check if still needed
/* // Without delaying to the next cycle it does not update.
// Seems order of events we are listening on causes that...
public void onAllServicesInitialized() {
openOfferManager.getObservableList().addListener((ListChangeListener<OpenOffer>) c -> updateBalance());
tradeManager.getTradableList().addListener((ListChangeListener<Trade>) change -> updateBalance());
btcWalletService.addBalanceListener(new BalanceListener() {
@Override
public void onBalanceChanged(Coin balance, Transaction tx) {
updateBalance();
}
});

updateBalance();
}

private void updateBalance() {
// Need to delay a bit to get the balances correct
UserThread.execute(() -> {
updateAvailableBalance();
updateReservedBalance();
updateLockedBalance();
});*/
updateAvailableBalance();
updateReservedBalance();
updateLockedBalance();
// TODO add lockingBalance
});
}

private void updateAvailableBalance() {
Coin totalAvailableBalance = Coin.valueOf(tradeManager.getAddressEntriesForAvailableBalanceStream()
.mapToLong(addressEntry -> btcWalletService.getBalanceForAddress(addressEntry.getAddress()).getValue())
.sum());
availableBalance.set(totalAvailableBalance);
long sum = tradeManager.getAddressEntriesForAvailableBalanceStream()
.mapToLong(addressEntry -> btcWalletService.getBalanceForAddress(addressEntry.getAddress()).value)
.sum();
availableBalance.set(Coin.valueOf(sum));
}

private void updateReservedBalance() {
Coin sum = Coin.valueOf(openOfferManager.getObservableList().stream()
.map(openOffer -> {
final Optional<AddressEntry> addressEntryOptional = btcWalletService.getAddressEntry(openOffer.getId(), AddressEntry.Context.RESERVED_FOR_TRADE);
if (addressEntryOptional.isPresent()) {
Address address = addressEntryOptional.get().getAddress();
return btcWalletService.getBalanceForAddress(address);
} else {
return null;
}
})
long sum = openOfferManager.getObservableList().stream()
.map(openOffer -> btcWalletService.getAddressEntry(openOffer.getId(), AddressEntry.Context.RESERVED_FOR_TRADE)
.orElse(null))
.filter(Objects::nonNull)
.mapToLong(Coin::getValue)
.sum());

reservedBalance.set(sum);
.mapToLong(addressEntry -> btcWalletService.getBalanceForAddress(addressEntry.getAddress()).value)
.sum();
reservedBalance.set(Coin.valueOf(sum));
}

private void updateLockedBalance() {
Stream<Trade> lockedTrades = Stream.concat(closedTradableManager.getLockedTradesStream(), failedTradesManager.getLockedTradesStream());
lockedTrades = Stream.concat(lockedTrades, tradeManager.getLockedTradesStream());
Coin sum = Coin.valueOf(lockedTrades
.mapToLong(trade -> {
final Optional<AddressEntry> addressEntryOptional = btcWalletService.getAddressEntry(trade.getId(), AddressEntry.Context.MULTI_SIG);
return addressEntryOptional.map(addressEntry -> addressEntry.getCoinLockedInMultiSig().getValue()).orElse(0L);
})
.sum());
lockedBalance.set(sum);
long sum = lockedTrades.map(trade -> btcWalletService.getAddressEntry(trade.getId(), AddressEntry.Context.MULTI_SIG)
.orElse(null))
.filter(Objects::nonNull)
.mapToLong(addressEntry -> addressEntry.getCoinLockedInMultiSig().getValue())
.sum();
lockedBalance.set(Coin.valueOf(sum));
}
}
3 changes: 1 addition & 2 deletions core/src/main/java/bisq/core/btc/BitcoinModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import bisq.core.app.AppOptionKeys;
import bisq.core.btc.model.AddressEntryList;
import bisq.core.btc.model.BalanceModel;
import bisq.core.btc.nodes.BtcNodes;
import bisq.core.btc.setup.RegTestHost;
import bisq.core.btc.setup.WalletsSetup;
Expand Down Expand Up @@ -78,7 +77,7 @@ protected void configure() {
bind(BsqCoinSelector.class).in(Singleton.class);
bind(NonBsqCoinSelector.class).in(Singleton.class);
bind(BtcNodes.class).in(Singleton.class);
bind(BalanceModel.class).in(Singleton.class);
bind(Balances.class).in(Singleton.class);

bind(PriceNodeHttpClient.class).in(Singleton.class);

Expand Down
Loading

0 comments on commit 0feed88

Please sign in to comment.