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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target
*.iml
.idea
api-keys.properties
*.txt
*.csv
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ See `DefaultKrakenRestRequester` for the default implementation.

### Custom nonce generator (not yet implemented)



## Examples

The `examples` Maven module contains some examples that might be worth checking (e.g. total staking rewards summary). The examples can be run directly from your IDE, or from the command line.
Expand All @@ -99,10 +97,12 @@ For private endpoints, you need to rename `api-keys.properties.example` (located
mvn clean install

# run example classes
mvn -q -pl examples exec:java -Dexec.mainClass=dev.andstuff.kraken.example.Examples
mvn -q -pl examples exec:java -Dexec.mainClass=dev.andstuff.kraken.example.SimpleExamples
mvn -q -pl examples exec:java -Dexec.mainClass=dev.andstuff.kraken.example.TotalRewards
```

[1]: https://docs.kraken.com/rest/

[2]: https://github.com/FasterXML/jackson

[3]: https://github.com/nyg/kraken-api-java/blob/v1.0.0/examples/src/main/java/dev/andstuff/kraken/example/Examples.java
10 changes: 10 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<properties>
<log4j.version>2.23.0</log4j.version>
<opencsv.version>5.9</opencsv.version>
</properties>

<dependencies>
Expand All @@ -21,6 +22,8 @@
<artifactId>kraken-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand All @@ -33,6 +36,13 @@
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Third-party -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package dev.andstuff.kraken.example;

import static dev.andstuff.kraken.example.PropertiesHelper.readFromFile;
import static dev.andstuff.kraken.example.helper.CredentialsHelper.readFromFile;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.databind.JsonNode;

import dev.andstuff.kraken.api.KrakenAPI;
import dev.andstuff.kraken.api.model.KrakenCredentials;
import dev.andstuff.kraken.api.model.endpoint.market.params.AssetPairParams;
import dev.andstuff.kraken.api.model.endpoint.market.response.AssetInfo;
import dev.andstuff.kraken.api.model.endpoint.market.response.AssetPair;
import dev.andstuff.kraken.api.model.endpoint.market.response.ServerTime;
import dev.andstuff.kraken.api.model.endpoint.market.response.SystemStatus;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Examples {
public class SimpleExamples {

public static void main(String[] args) {

Expand All @@ -39,7 +40,7 @@ public static void main(String[] args) {
Map<String, AssetPair> pairs1 = publicAPI.assetPairs(List.of("ETH/BTC", "ETH/USD"));
log.info("{}", pairs1);

Map<String, AssetPair> pairs2 = publicAPI.assetPairs(List.of("DOT/USD", "ADA/USD"), AssetPair.Info.MARGIN);
Map<String, AssetPair> pairs2 = publicAPI.assetPairs(List.of("DOT/USD", "ADA/USD"), AssetPairParams.Info.MARGIN);
log.info("{}", pairs2);

JsonNode ticker = publicAPI.query(KrakenAPI.Public.TICKER, Map.of("pair", "XBTEUR"));
Expand All @@ -50,8 +51,8 @@ public static void main(String[] args) {

/* Private endpoint example */

Properties apiKeys = readFromFile("/api-keys.properties");
KrakenAPI api = new KrakenAPI(apiKeys.getProperty("key"), apiKeys.getProperty("secret"));
KrakenCredentials credentials = readFromFile("/api-keys.properties");
KrakenAPI api = new KrakenAPI(credentials);

JsonNode balance = api.query(KrakenAPI.Private.BALANCE);
log.info("{}", balance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package dev.andstuff.kraken.example;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import dev.andstuff.kraken.api.KrakenAPI;
import dev.andstuff.kraken.api.model.KrakenCredentials;
import dev.andstuff.kraken.api.model.KrakenException;
import dev.andstuff.kraken.api.model.endpoint.account.params.LedgerInfoParams;
import dev.andstuff.kraken.api.model.endpoint.account.response.LedgerEntry;
import dev.andstuff.kraken.api.model.endpoint.account.response.LedgerInfo;
import dev.andstuff.kraken.example.reward.AssetRates;
import dev.andstuff.kraken.example.reward.StakingRewards;
import dev.andstuff.kraken.example.reward.csv.CsvLedgerEntries;
import dev.andstuff.kraken.example.reward.csv.CsvStakingRewardsSummary;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* Generates a CSV file containing all the ledger entries corresponding to
* staking rewards, and another CSV file containing the summary of staking
* rewards earned each year for each asset.
*/
@Slf4j
@RequiredArgsConstructor
public class StakingRewardsSummaryExample {

private static final int SLEEP_BETWEEN_API_CALLS = 2000;

private final KrakenAPI api;

public static void main(String[] args) {
KrakenCredentials credentials = readFromFile("/api-keys.properties");
new StakingRewardsSummaryExample(new KrakenAPI(credentials))
.generate("rewards.csv", "rewards-summary.csv");
}

public void generate(String rewardsFileName, String rewardSummaryFileName) {
List<LedgerEntry> rewards = fetchStakingRewards();
StakingRewards stakingRewards = new StakingRewards(rewards);
AssetRates rates = fetchRatesFor(stakingRewards.getAssets());

new CsvLedgerEntries(rewards).writeToFile(rewardsFileName);
new CsvStakingRewardsSummary(stakingRewards, rates).writeToFile(rewardSummaryFileName);
}

private List<LedgerEntry> fetchStakingRewards() {

List<LedgerEntry> rewards = new ArrayList<>();
LedgerInfoParams params = LedgerInfoParams.builder()
.assetType(LedgerInfoParams.Type.STAKING)
.withoutCount(true)
.build();

boolean hasNext = true;
while (hasNext) {
LedgerInfo ledgerInfo = api.ledgerInfo(params);
params = params.withNextResultOffset();
hasNext = ledgerInfo.hasNext();

rewards.addAll(ledgerInfo.stakingRewards());
log.info("Fetched {} staking rewards", rewards.size());

try {
Thread.sleep(SLEEP_BETWEEN_API_CALLS);
}
catch (InterruptedException e) {
log.warn("Thread was interrupted");
Thread.currentThread().interrupt();
}
}

return rewards;
}

private AssetRates fetchRatesFor(Set<String> assets) {
try {
List<String> pairs = assets.stream()
.map(asset -> asset + AssetRates.REFERENCE_ASSET)
.filter(not(AssetRates.REFERENCE_PAIR::equals))
.toList();
return new AssetRates(api.ticker(pairs));
}
catch (KrakenException e) {
throw new RuntimeException("Couldn't fetch rates", e);
}
}
}
117 changes: 0 additions & 117 deletions examples/src/main/java/dev/andstuff/kraken/example/TotalRewards.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package dev.andstuff.kraken.example;
package dev.andstuff.kraken.example.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import dev.andstuff.kraken.api.model.KrakenCredentials;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PropertiesHelper {
public final class CredentialsHelper {

public static Properties readFromFile(String path) {
public static KrakenCredentials readFromFile(String path) {
try {
InputStream stream = Examples.class.getResourceAsStream(path);
InputStream stream = CredentialsHelper.class.getResourceAsStream(path);
Properties properties = new Properties();
properties.load(stream);
return properties;
return new KrakenCredentials(properties.getProperty("key"), properties.getProperty("secret"));
}
catch (IOException e) {
throw new RuntimeException(String.format("Could not read properties from file: %s", path));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.andstuff.kraken.example.helper;

import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;

public class HeaderAndPositionMappingStrategy<T> extends ColumnPositionMappingStrategy<T> {

@Override
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
super.generateHeader(bean);

int fieldCount = getFieldMap().values().size();
String[] header = new String[fieldCount];

for (int i = 0; i < fieldCount; i++) {
header[i] = findField(i).getField().getDeclaredAnnotation(CsvBindByName.class).column();
}

return header;
}
}
Loading