Skip to content
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
@@ -1,7 +1,6 @@
package dev.andstuff.kraken.example;

import static dev.andstuff.kraken.example.helper.CredentialsHelper.readFromFile;
import static java.util.function.Predicate.not;

import java.time.Instant;
import java.util.List;
Expand All @@ -10,6 +9,7 @@
import dev.andstuff.kraken.api.KrakenAPI;
import dev.andstuff.kraken.api.endpoint.KrakenException;
import dev.andstuff.kraken.api.endpoint.account.response.LedgerEntry;
import dev.andstuff.kraken.api.endpoint.market.response.AssetPairs;
import dev.andstuff.kraken.api.rest.KrakenCredentials;
import dev.andstuff.kraken.example.report.ReportFetcher;
import dev.andstuff.kraken.example.reward.AssetRates;
Expand Down Expand Up @@ -52,13 +52,24 @@ public void generate(String rewardsFileName, String rewardSummaryFileName) {

private AssetRates fetchRatesFor(Set<String> assets) {
try {
return new AssetRates(api.ticker(assets.stream()
AssetPairs allAssetPairs = api.assetPairs();
List<String> wantedAssetPairs = assets.stream()
.map(asset -> asset + AssetRates.REFERENCE_ASSET)
.filter(not(AssetRates.REFERENCE_PAIR::equals))
.toList()));
.filter(assetPair -> assetPairExists(allAssetPairs, assetPair))
.toList();

return new AssetRates(api.ticker(wantedAssetPairs));
}
catch (KrakenException e) {
throw new IllegalStateException("Couldn't fetch rates", e);
}
}

private boolean assetPairExists(AssetPairs assetPairs, String assetPair) {
boolean assetPairExists = assetPairs.findBy(assetPair).isPresent();
if (!assetPairExists) {
log.warn("Asset pair {} does not exist, rate will be 0", assetPair);
}
return assetPairExists;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public List<LedgerEntry> fetchReportData(String reportId) {
AtomicBoolean processed = new AtomicBoolean(false);
while (!processed.get()) {
try {
Thread.sleep(1_000);
Thread.sleep(2_000);
}
catch (InterruptedException e) {
log.warn("Thread interrupted while waiting for the report to be processed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public AssetRewards(String asset, List<LedgerEntry> rewards) {
this.yearlyRewards = rewards.stream()
.collect(toMap(LedgerEntry::year, LedgerEntry::netAmount, BigDecimal::add));
}

public BigDecimal totalFiatAmount(AssetRates rates) {
return rates.evaluate(totalReward, asset);
}

public BigDecimal fiatAmountFor(int year, AssetRates rates) {
return rates.evaluate(yearlyRewards.getOrDefault(year, BigDecimal.ZERO), asset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

Expand All @@ -29,4 +30,16 @@ public StakingRewards(List<LedgerEntry> rewards) {
.map(entry -> new AssetRewards(entry.getKey(), entry.getValue()))
.collect(toSet());
}

public BigDecimal totalFiatAmount(AssetRates rates) {
return assetRewards.stream()
.map(reward -> reward.totalFiatAmount(rates))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

public BigDecimal totalFiatAmountFor(int year, AssetRates rates) {
return assetRewards.stream()
.map(reward -> reward.fiatAmountFor(year, rates))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -48,12 +49,18 @@ public void writeToFile(String fileName) {
* @return an array of String: Asset, y1, _, y2, _, …, total, _
*/
private static String[] buildHeaderRow(Set<Integer> years) {
List<String> headerCells = years.stream()
List<String> headerCells = new ArrayList<>();
headerCells.add("Asset");
headerCells.addAll(buildYearlyHeaders(years));
headerCells.add("Total");
headerCells.add(AssetRates.REFERENCE_ASSET);
return headerCells.toArray(String[]::new);
}

private static List<String> buildYearlyHeaders(Set<Integer> years) {
return years.stream()
.flatMap(year -> Stream.of(year.toString(), AssetRates.REFERENCE_ASSET))
.collect(toList());
headerCells.addFirst("Asset");
headerCells.addAll(List.of("Total", AssetRates.REFERENCE_ASSET));
return headerCells.toArray(new String[0]);
.toList();
}

/**
Expand All @@ -79,23 +86,27 @@ private static List<String[]> buildRewardRows(StakingRewards rewards, AssetRates
}))
.entrySet().stream()
.map(entry -> {
List<String> cells = entry.getValue().stream().map(BigDecimal::toPlainString).collect(toList());
cells.addFirst(entry.getKey());
return cells.toArray(new String[0]);
List<String> cells = new ArrayList<>();
cells.add(entry.getKey());
cells.addAll(entry.getValue().stream().map(BigDecimal::toPlainString).toList());
return cells.toArray(String[]::new);
})
.sorted(comparing(e -> new BigDecimal(e[e.length - 1]), reverseOrder()))
.sorted(comparing(row -> new BigDecimal(row[row.length - 1]), reverseOrder()))
.toList();
}

private String[] buildFooterRow(StakingRewards rewards, AssetRates rates) {
List<String> footerCells = new ArrayList<>();
footerCells.add("Total");
footerCells.addAll(buildYearlyTotals(rewards, rates));
footerCells.add("");
footerCells.add(rewards.totalFiatAmount(rates).toPlainString());
return footerCells.toArray(String[]::new);
}

BigDecimal totalFiatAmount = rewards.getAssetRewards().stream()
.map(reward -> rates.evaluate(reward.getTotalReward(), reward.getAsset()))
.reduce(BigDecimal.ZERO, BigDecimal::add);

List<String> footerCells = rewards.getYears().stream().flatMap(year -> Stream.of("", "")).collect(toList());
footerCells.addFirst("Total");
footerCells.addAll(List.of("", totalFiatAmount.toPlainString()));
return footerCells.toArray(new String[0]);
private List<String> buildYearlyTotals(StakingRewards rewards, AssetRates rates) {
return rewards.getYears().stream()
.flatMap(year -> Stream.of("", rewards.totalFiatAmountFor(year, rates).toPlainString()))
.toList();
}
}