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

Show total fees and total DPT #6647

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 @@ -28,6 +28,9 @@
import bisq.core.dao.burningman.accounting.exceptions.BlockHashNotConnectingException;
import bisq.core.dao.burningman.accounting.exceptions.BlockHeightNotConnectingException;
import bisq.core.dao.burningman.accounting.storage.BurningManAccountingStoreService;
import bisq.core.dao.state.DaoStateListener;
import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.Block;
import bisq.core.monetary.Price;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
Expand All @@ -46,6 +49,7 @@
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -67,7 +71,7 @@
*/
@Slf4j
@Singleton
public class BurningManAccountingService implements DaoSetupService {
public class BurningManAccountingService implements DaoSetupService, DaoStateListener {
// now 763195 -> 107159 blocks takes about 14h
// First tx at BM address 656036 Sun Nov 08 19:02:18 EST 2020
// 2 months ago 754555
Expand All @@ -86,10 +90,14 @@ public class BurningManAccountingService implements DaoSetupService {
@Getter
private final Map<String, BalanceModel> balanceModelByBurningManName = new HashMap<>();
@Getter
private BooleanProperty isProcessing = new SimpleBooleanProperty();
private final BooleanProperty isProcessing = new SimpleBooleanProperty();

// cache
private final List<ReceivedBtcBalanceEntry> receivedBtcBalanceEntryListExcludingLegacyBM = new ArrayList<>();

@Inject
public BurningManAccountingService(BurningManAccountingStoreService burningManAccountingStoreService,
public BurningManAccountingService(DaoStateService daoStateService,
BurningManAccountingStoreService burningManAccountingStoreService,
BurningManPresentationService burningManPresentationService,
TradeStatisticsManager tradeStatisticsManager,
Preferences preferences) {
Expand All @@ -98,6 +106,8 @@ public BurningManAccountingService(BurningManAccountingStoreService burningManAc
this.tradeStatisticsManager = tradeStatisticsManager;
this.preferences = preferences;

daoStateService.addDaoStateListener(this);
daoStateService.getLastBlock().ifPresent(this::applyBlock);
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -124,6 +134,20 @@ public void start() {
}


///////////////////////////////////////////////////////////////////////////////////////////
// DaoStateListener
///////////////////////////////////////////////////////////////////////////////////////////

@Override
public void onParseBlockCompleteAfterBatchProcessing(Block block) {
applyBlock(block);
}

private void applyBlock(Block block) {
receivedBtcBalanceEntryListExcludingLegacyBM.clear();
}


///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -162,14 +186,72 @@ public Map<Date, Price> getAverageBsqPriceByMonth() {
}

public long getTotalAmountOfDistributedBtc() {
return getReceivedBtcBalanceEntryStreamExcludingLegacyBurningmen()
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.mapToLong(BaseBalanceEntry::getAmount)
.sum();
}

public long getTotalAmountOfDistributedBtcFees() {
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.filter(e -> e.getType() == BalanceEntry.Type.BTC_TRADE_FEE_TX)
.mapToLong(BaseBalanceEntry::getAmount)
.sum();
}

public long getTotalAmountOfDistributedBtcFeesAsBsq() {
Map<Date, Price> averageBsqPriceByMonth = getAverageBsqPriceByMonth();
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.filter(e -> e.getType() == BalanceEntry.Type.BTC_TRADE_FEE_TX)
.map(balanceEntry -> {
Date month = balanceEntry.getMonth();
Optional<Price> price = Optional.ofNullable(averageBsqPriceByMonth.get(month));
long receivedBtc = balanceEntry.getAmount();
Optional<Long> receivedBtcAsBsq;
if (price.isEmpty() || price.get().getValue() == 0) {
receivedBtcAsBsq = Optional.empty();
} else {
long volume = price.get().getVolumeByAmount(Coin.valueOf(receivedBtc)).getValue();
receivedBtcAsBsq = Optional.of(MathUtils.roundDoubleToLong(MathUtils.scaleDownByPowerOf10(volume, 6)));
}
return receivedBtcAsBsq;
})
.filter(Optional::isPresent)
.mapToLong(Optional::get)
.sum();
}

public long getTotalAmountOfDistributedDPT() {
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.filter(e -> e.getType() == BalanceEntry.Type.DPT_TX)
.mapToLong(BaseBalanceEntry::getAmount)
.sum();
}

public long getTotalAmountOfDistributedDPTAsBsq() {
Map<Date, Price> averageBsqPriceByMonth = getAverageBsqPriceByMonth();
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.filter(e -> e.getType() == BalanceEntry.Type.DPT_TX)
.map(balanceEntry -> {
Date month = balanceEntry.getMonth();
Optional<Price> price = Optional.ofNullable(averageBsqPriceByMonth.get(month));
long receivedBtc = balanceEntry.getAmount();
Optional<Long> receivedBtcAsBsq;
if (price.isEmpty() || price.get().getValue() == 0) {
receivedBtcAsBsq = Optional.empty();
} else {
long volume = price.get().getVolumeByAmount(Coin.valueOf(receivedBtc)).getValue();
receivedBtcAsBsq = Optional.of(MathUtils.roundDoubleToLong(MathUtils.scaleDownByPowerOf10(volume, 6)));
}
return receivedBtcAsBsq;
})
.filter(Optional::isPresent)
.mapToLong(Optional::get)
.sum();
}

public long getTotalAmountOfDistributedBsq() {
Map<Date, Price> averageBsqPriceByMonth = getAverageBsqPriceByMonth();
return getReceivedBtcBalanceEntryStreamExcludingLegacyBurningmen()
return getReceivedBtcBalanceEntryListExcludingLegacyBM().stream()
.map(balanceEntry -> {
Date month = balanceEntry.getMonth();
Optional<Price> price = Optional.ofNullable(averageBsqPriceByMonth.get(month));
Expand All @@ -188,12 +270,18 @@ public long getTotalAmountOfDistributedBsq() {
.sum();
}

private Stream<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntryStreamExcludingLegacyBurningmen() {
return balanceModelByBurningManName.entrySet().stream()
private List<ReceivedBtcBalanceEntry> getReceivedBtcBalanceEntryListExcludingLegacyBM() {
if (!receivedBtcBalanceEntryListExcludingLegacyBM.isEmpty()) {
return receivedBtcBalanceEntryListExcludingLegacyBM;
}

receivedBtcBalanceEntryListExcludingLegacyBM.addAll(balanceModelByBurningManName.entrySet().stream()
.filter(e -> !e.getKey().equals(BurningManPresentationService.LEGACY_BURNING_MAN_DPT_NAME) &&
!e.getKey().equals(BurningManPresentationService.LEGACY_BURNING_MAN_BTC_FEES_NAME))
.map(Map.Entry::getValue)
.flatMap(balanceModel -> balanceModel.getReceivedBtcBalanceEntries().stream());
.flatMap(balanceModel -> balanceModel.getReceivedBtcBalanceEntries().stream())
.collect(Collectors.toList()));
return receivedBtcBalanceEntryListExcludingLegacyBM;
}

public Stream<ReceivedBtcBalanceEntry> getDistributedBtcBalanceByMonth(Date month) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,8 @@ dao.burningman.contributorsComboBox.prompt=Select any of my contributors
dao.burningman.daoBalance=Balance for DAO
dao.burningman.daoBalanceTotalBurned=Total amount of burned BSQ
dao.burningman.daoBalanceTotalDistributed=Total amount of distributed BTC / BSQ
dao.burningman.daoBalanceTotalBtcFees=Total amount of BTC fees in BTC / BSQ
dao.burningman.daoBalanceTotalDPT=Total amount of delayed payout transactions BTC / BSQ
dao.burningman.selectedContributor=Selected contributor
dao.burningman.selectedContributor.disabledAccounting=(accounting data not updated)
dao.burningman.selectedContributor.processing=(still processing data...)
Expand Down
Loading