Skip to content

Commit

Permalink
Merge pull request #8 from sqrrm/dao
Browse files Browse the repository at this point in the history
Dao
  • Loading branch information
ManfredKarrer authored Jul 13, 2018
2 parents 83fac41 + 069802b commit 333133a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ private LockupView(BsqWalletService bsqWalletService,

@Override
public void initialize() {
// TODO: Show balance locked up in bonds
gridRow = bsqBalanceUtil.addGroup(root, gridRow);

addTitledGroupBg(root, ++gridRow, 4, Res.get("dao.bonding.lock.lockBSQ"), Layout.GROUP_DISTANCE);
Expand All @@ -129,7 +128,7 @@ public void initialize() {
bsqWalletService.getAvailableNonBsqBalance(),
bsqWalletService.getUnverifiedBalance(),
bsqWalletService.getLockedForVotingBalance(),
bsqWalletService.getLockedInBondsBalance(),
bsqWalletService.getLockupBondsBalance(),
bsqWalletService.getUnlockingBondsBalance());
};

Expand Down Expand Up @@ -189,7 +188,7 @@ protected void activate() {
bsqWalletService.getAvailableNonBsqBalance(),
bsqWalletService.getUnverifiedBalance(),
bsqWalletService.getLockedForVotingBalance(),
bsqWalletService.getLockedInBondsBalance(),
bsqWalletService.getLockupBondsBalance(),
bsqWalletService.getUnlockingBondsBalance());
}

Expand All @@ -205,7 +204,7 @@ public void onUpdateBalances(Coin confirmedBalance,
Coin availableNonBsqBalance,
Coin pendingBalance,
Coin lockedForVotingBalance,
Coin lockedInBondsBalance,
Coin lockupBondsBalance,
Coin unlockingBondsBalance) {
bsqValidator.setAvailableBalance(confirmedBalance);
boolean isValid = bsqValidator.validate(amountInputTextField.getText()).isValid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

@Data
class LockedTxListItem {
class LockupTxListItem {
private final Transaction transaction;
private final BsqWalletService bsqWalletService;
private final BtcWalletService btcWalletService;
Expand All @@ -64,7 +64,7 @@ class LockedTxListItem {
private TxConfidenceListener txConfidenceListener;
private boolean issuanceTx;

LockedTxListItem(Transaction transaction,
LockupTxListItem(Transaction transaction,
BsqWalletService bsqWalletService,
BtcWalletService btcWalletService,
DaoFacade daoFacade,
Expand All @@ -89,8 +89,7 @@ class LockedTxListItem {
stateService.getLockupTxOutput(transaction.getHashAsString()).ifPresent(
out -> amount = Coin.valueOf(out.getValue()));

//TODO SQ: use DaoFacade instead of direct access to stateService
Optional<Integer> opLockTime = stateService.getLockTime(transaction.getHashAsString());
Optional<Integer> opLockTime = daoFacade.getLockTime(transaction.getHashAsString());
lockTime = opLockTime.orElse(-1);

button = new AutoTooltipButton();
Expand Down Expand Up @@ -125,17 +124,16 @@ private void updateConfidence(TransactionConfidence confidence, Tooltip tooltip)
}
}

public boolean isLockedAndUnspent() {
public boolean isLockupAndUnspent() {
return !isSpent() && getTxType() == TxType.LOCKUP;
}

public void cleanup() {
bsqWalletService.removeTxConfidenceListener(txConfidenceListener);
}

// TODO SQ use daoFacade
public boolean isSpent() {
Optional<TxOutput> optionalTxOutput = stateService.getLockupTxOutput(txId);
Optional<TxOutput> optionalTxOutput = daoFacade.getLockupTxOutput(txId);
if (!optionalTxOutput.isPresent())
return true;

Expand Down
99 changes: 48 additions & 51 deletions src/main/java/bisq/desktop/main/dao/bonding/unlock/UnlockView.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

@FxmlView
public class UnlockView extends ActivatableView<GridPane, Void> implements BsqBalanceListener, BlockListener {
private TableView<LockedTxListItem> tableView;
private TableView<LockupTxListItem> tableView;

private final BsqWalletService bsqWalletService;
private final BtcWalletService btcWalletService;
Expand All @@ -98,10 +98,10 @@ public class UnlockView extends ActivatableView<GridPane, Void> implements BsqBa

private int gridRow = 0;
private boolean synced;
private LockedTxListItem selectedItem;
private LockupTxListItem selectedItem;

private final ObservableList<LockedTxListItem> observableList = FXCollections.observableArrayList();
private final FilteredList<LockedTxListItem> lockedTxs = new FilteredList<>(observableList);
private final ObservableList<LockupTxListItem> observableList = FXCollections.observableArrayList();
private final FilteredList<LockupTxListItem> lockupTxs = new FilteredList<>(observableList);

private ListChangeListener<Transaction> walletBsqTransactionsListener;
private ChangeListener<Number> walletChainHeightListener;
Expand Down Expand Up @@ -138,7 +138,6 @@ private UnlockView(BsqWalletService bsqWalletService,

@Override
public void initialize() {
// TODO: Show balance locked up in bonds
gridRow = bsqBalanceUtil.addGroup(root, gridRow);

tableView = new TableView<>();
Expand All @@ -149,7 +148,7 @@ public void initialize() {
addLockTimeColumn();
addUnlockColumn();

lockedTxs.setPredicate(LockedTxListItem::isLockedAndUnspent);
lockupTxs.setPredicate(LockupTxListItem::isLockupAndUnspent);
walletBsqTransactionsListener = change -> updateList();
walletChainHeightListener = (observable, oldValue, newValue) -> onUpdateAnyChainHeight();

Expand All @@ -164,22 +163,22 @@ public void initialize() {
}

private void addTxIdColumn() {
TableColumn<LockedTxListItem, LockedTxListItem> column = new AutoTooltipTableColumn<>(Res.get("shared.txId"));
TableColumn<LockupTxListItem, LockupTxListItem> column = new AutoTooltipTableColumn<>(Res.get("shared.txId"));

column.setCellValueFactory(item -> new ReadOnlyObjectWrapper<>(item.getValue()));
column.setMinWidth(60);
column.setCellFactory(
new Callback<TableColumn<LockedTxListItem, LockedTxListItem>, TableCell<LockedTxListItem,
LockedTxListItem>>() {
new Callback<TableColumn<LockupTxListItem, LockupTxListItem>, TableCell<LockupTxListItem,
LockupTxListItem>>() {

@Override
public TableCell<LockedTxListItem, LockedTxListItem> call(TableColumn<LockedTxListItem,
LockedTxListItem> column) {
return new TableCell<LockedTxListItem, LockedTxListItem>() {
public TableCell<LockupTxListItem, LockupTxListItem> call(TableColumn<LockupTxListItem,
LockupTxListItem> column) {
return new TableCell<LockupTxListItem, LockupTxListItem>() {
private HyperlinkWithIcon hyperlinkWithIcon;

@Override
public void updateItem(final LockedTxListItem item, boolean empty) {
public void updateItem(final LockupTxListItem item, boolean empty) {
super.updateItem(item, empty);

if (item != null && !empty) {
Expand All @@ -201,22 +200,22 @@ public void updateItem(final LockedTxListItem item, boolean empty) {
}

private void addAmountColumn() {
TableColumn<LockedTxListItem, LockedTxListItem> column =
TableColumn<LockupTxListItem, LockupTxListItem> column =
new AutoTooltipTableColumn<>(Res.get("shared.amountWithCur", "BSQ"));
column.setMinWidth(120);
column.setMaxWidth(column.getMinWidth());

column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue()));
column.setCellFactory(new Callback<TableColumn<LockedTxListItem, LockedTxListItem>,
TableCell<LockedTxListItem, LockedTxListItem>>() {
column.setCellFactory(new Callback<TableColumn<LockupTxListItem, LockupTxListItem>,
TableCell<LockupTxListItem, LockupTxListItem>>() {

@Override
public TableCell<LockedTxListItem, LockedTxListItem> call(TableColumn<LockedTxListItem,
LockedTxListItem> column) {
return new TableCell<LockedTxListItem, LockedTxListItem>() {
public TableCell<LockupTxListItem, LockupTxListItem> call(TableColumn<LockupTxListItem,
LockupTxListItem> column) {
return new TableCell<LockupTxListItem, LockupTxListItem>() {

@Override
public void updateItem(final LockedTxListItem item, boolean empty) {
public void updateItem(final LockupTxListItem item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty) {
TxType txType = item.getTxType();
Expand All @@ -233,22 +232,22 @@ public void updateItem(final LockedTxListItem item, boolean empty) {
}

private void addLockTimeColumn() {
TableColumn<LockedTxListItem, LockedTxListItem> column =
TableColumn<LockupTxListItem, LockupTxListItem> column =
new AutoTooltipTableColumn<>(Res.get("dao.bonding.unlock.time"));
column.setMinWidth(120);
column.setMaxWidth(column.getMinWidth());

column.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue()));
column.setCellFactory(new Callback<TableColumn<LockedTxListItem, LockedTxListItem>,
TableCell<LockedTxListItem, LockedTxListItem>>() {
column.setCellFactory(new Callback<TableColumn<LockupTxListItem, LockupTxListItem>,
TableCell<LockupTxListItem, LockupTxListItem>>() {

@Override
public TableCell<LockedTxListItem, LockedTxListItem> call(TableColumn<LockedTxListItem,
LockedTxListItem> column) {
return new TableCell<LockedTxListItem, LockedTxListItem>() {
public TableCell<LockupTxListItem, LockupTxListItem> call(TableColumn<LockupTxListItem,
LockupTxListItem> column) {
return new TableCell<LockupTxListItem, LockupTxListItem>() {

@Override
public void updateItem(final LockedTxListItem item, boolean empty) {
public void updateItem(final LockupTxListItem item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty) {
TxType txType = item.getTxType();
Expand All @@ -265,23 +264,23 @@ public void updateItem(final LockedTxListItem item, boolean empty) {
}

private void addUnlockColumn() {
TableColumn<LockedTxListItem, LockedTxListItem> unlockColumn = new TableColumn<>();
TableColumn<LockupTxListItem, LockupTxListItem> unlockColumn = new TableColumn<>();
unlockColumn.setMinWidth(130);
unlockColumn.setMaxWidth(unlockColumn.getMinWidth());

unlockColumn.setCellValueFactory((item) -> new ReadOnlyObjectWrapper<>(item.getValue()));

unlockColumn.setCellFactory(new Callback<TableColumn<LockedTxListItem, LockedTxListItem>,
TableCell<LockedTxListItem, LockedTxListItem>>() {
unlockColumn.setCellFactory(new Callback<TableColumn<LockupTxListItem, LockupTxListItem>,
TableCell<LockupTxListItem, LockupTxListItem>>() {

@Override
public TableCell<LockedTxListItem, LockedTxListItem> call(TableColumn<LockedTxListItem,
LockedTxListItem> column) {
return new TableCell<LockedTxListItem, LockedTxListItem>() {
public TableCell<LockupTxListItem, LockupTxListItem> call(TableColumn<LockupTxListItem,
LockupTxListItem> column) {
return new TableCell<LockupTxListItem, LockupTxListItem>() {
Button button;

@Override
public void updateItem(final LockedTxListItem item, boolean empty) {
public void updateItem(final LockupTxListItem item, boolean empty) {
super.updateItem(item, empty);

if (item != null && !empty) {
Expand All @@ -304,22 +303,20 @@ public void updateItem(final LockedTxListItem item, boolean empty) {
};
}
});
unlockColumn.setComparator(Comparator.comparing(LockedTxListItem::getConfirmations));
unlockColumn.setComparator(Comparator.comparing(LockupTxListItem::getConfirmations));
tableView.getColumns().add(unlockColumn);
}

private void onButtonClick() {
if (GUIUtil.isReadyForTxBroadcast(p2PService, walletsSetup)) {
// TODO SQ use daoFacade
Optional<TxOutput> lockedTxOutput = stateService.getLockupTxOutput(selectedItem.getTxId());
if (!lockedTxOutput.isPresent()) {
log.warn("Locked output not found, txId = ", selectedItem.getTxId());
Optional<TxOutput> lockupTxOutput = daoFacade.getLockupTxOutput(selectedItem.getTxId());
if (!lockupTxOutput.isPresent()) {
log.warn("Lockup output not found, txId = ", selectedItem.getTxId());
return;
}

Coin unlockAmount = Coin.valueOf(lockedTxOutput.get().getValue());
//TODO SQ: use DaoFacade instead of direct access to stateService
Optional<Integer> opLockTime = stateService.getLockTime(selectedItem.getTxId());
Coin unlockAmount = Coin.valueOf(lockupTxOutput.get().getValue());
Optional<Integer> opLockTime = daoFacade.getLockTime(selectedItem.getTxId());
int lockTime = opLockTime.isPresent() ? opLockTime.get() : -1;

try {
Expand Down Expand Up @@ -360,7 +357,7 @@ private void onButtonClick() {
log.info("unlock tx: {}", selectedItem.getTxId());
}

private void openTxInBlockExplorer(LockedTxListItem item) {
private void openTxInBlockExplorer(LockupTxListItem item) {
if (item.getTxId() != null)
GUIUtil.openWebPage(preferences.getBsqBlockChainExplorer().txUrl + item.getTxId());
}
Expand All @@ -373,14 +370,14 @@ protected void activate() {
bsqWalletService.getAvailableNonBsqBalance(),
bsqWalletService.getUnverifiedBalance(),
bsqWalletService.getLockedForVotingBalance(),
bsqWalletService.getLockedInBondsBalance(),
bsqWalletService.getLockupBondsBalance(),
bsqWalletService.getUnlockingBondsBalance());

bsqWalletService.getWalletTransactions().addListener(walletBsqTransactionsListener);
bsqWalletService.addBsqBalanceListener(this);
btcWalletService.getChainHeightProperty().addListener(walletChainHeightListener);

tableView.setItems(lockedTxs);
tableView.setItems(lockupTxs);

daoFacade.addBlockListener(this);

Expand All @@ -403,13 +400,13 @@ private void onUpdateAnyChainHeight() {
}

private void updateList() {
observableList.forEach(LockedTxListItem::cleanup);
observableList.forEach(LockupTxListItem::cleanup);

// copy list to avoid ConcurrentModificationException
final List<Transaction> walletTransactions = new ArrayList<>(bsqWalletService.getWalletTransactions());
List<LockedTxListItem> items = walletTransactions.stream()
List<LockupTxListItem> items = walletTransactions.stream()
.map(transaction -> {
return new LockedTxListItem(transaction,
return new LockupTxListItem(transaction,
bsqWalletService,
btcWalletService,
daoFacade,
Expand All @@ -426,21 +423,21 @@ protected void deactivate() {
bsqBalanceUtil.deactivate();
bsqWalletService.removeBsqBalanceListener(this);

lockedTxs.predicateProperty().unbind();
lockupTxs.predicateProperty().unbind();
bsqWalletService.getWalletTransactions().removeListener(walletBsqTransactionsListener);
bsqWalletService.removeBsqBalanceListener(this);
btcWalletService.getChainHeightProperty().removeListener(walletChainHeightListener);
daoFacade.removeBlockListener(this);

observableList.forEach(LockedTxListItem::cleanup);
observableList.forEach(LockupTxListItem::cleanup);
}

@Override
public void onUpdateBalances(Coin confirmedBalance,
Coin availableNonBsqBalance,
Coin pendingBalance,
Coin lockedForVotingBalance,
Coin lockedInBondsBalance,
Coin lockupBondsBalance,
Coin unlockingBondsBalance) {
bsqValidator.setAvailableBalance(confirmedBalance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected void activate() {
bsqWalletService.getAvailableNonBsqBalance(),
bsqWalletService.getUnverifiedBalance(),
bsqWalletService.getLockedForVotingBalance(),
bsqWalletService.getLockedInBondsBalance(),
bsqWalletService.getLockupBondsBalance(),
bsqWalletService.getUnlockingBondsBalance());

voteButton.setOnAction(e -> onVote());
Expand Down Expand Up @@ -144,7 +144,7 @@ public void onUpdateBalances(Coin confirmedBalance,
Coin availableNonBsqBalance,
Coin pendingBalance,
Coin lockedForVotingBalance,
Coin lockedInBondsBalance,
Coin lockupBondsBalance,
Coin unlockingBondsBalance) {
stakeInputTextField.setPromptText(Res.get("dao.proposal.myVote.stake.prompt",
bsqFormatter.formatCoinWithCode(confirmedBalance)));
Expand Down
Loading

0 comments on commit 333133a

Please sign in to comment.