Implemented buyPrice and sellPrice. Closes #1
This commit is contained in:
parent
c046cd7341
commit
bef4b2ee20
18 changed files with 152 additions and 98 deletions
|
@ -2,13 +2,14 @@ package com.example;
|
|||
|
||||
import net.thauvin.erik.crypto.CryptoException;
|
||||
import net.thauvin.erik.crypto.CryptoPrice;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CryptoPriceSample {
|
||||
public static void main(String[] args) {
|
||||
public static void main(final String[] args) {
|
||||
try {
|
||||
if (args.length > 0) {
|
||||
final CryptoPrice price;
|
||||
|
@ -21,27 +22,27 @@ public class CryptoPriceSample {
|
|||
+ price.getCurrency());
|
||||
} else {
|
||||
// Get current Bitcoin spot price.
|
||||
final CryptoPrice price = CryptoPrice.spotPrice("BTC");
|
||||
final var price = CryptoPrice.spotPrice("BTC");
|
||||
System.out.println("The current Bitcoin price is " + price.toCurrency());
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Get current Ethereum spot price in Pound sterling.
|
||||
final CryptoPrice gbpPrice = CryptoPrice.spotPrice("ETH", "GBP");
|
||||
System.out.println("The current Ethereum price is " + gbpPrice.toCurrency());
|
||||
// Get current Ethereum buy price in Pound sterling.
|
||||
final var gbpPrice = CryptoPrice.buyPrice("ETH", "GBP");
|
||||
System.out.println("The current Ethereum buy price is " + gbpPrice.toCurrency());
|
||||
|
||||
// Get current Litecoin spot price in Euros.
|
||||
final CryptoPrice euroPrice = CryptoPrice.spotPrice("LTC", "EUR");
|
||||
System.out.println("The current Litecoin price is " + euroPrice.toCurrency());
|
||||
// Get current Litecoin sell price in Euros.
|
||||
final var euroPrice = CryptoPrice.sellPrice("LTC", "EUR");
|
||||
System.out.println("The current Litecoin sell price is " + euroPrice.toCurrency());
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Get current Bitcoin buy price using API.
|
||||
// See: https://developers.coinbase.com/api/v2#get-buy-price
|
||||
final CryptoPrice buyPrice = CryptoPrice
|
||||
.toPrice(CryptoPrice.apiCall(List.of("prices", "BTC-USD", "buy"), Collections.emptyMap()));
|
||||
System.out.println("The current " + buyPrice.getBase() + " buy price is " + buyPrice.getAmount()
|
||||
+ " in " + buyPrice.getCurrency());
|
||||
// Get exchange rate using API.
|
||||
// See: https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates
|
||||
final var response = CryptoPrice.apiCall(List.of("exchange-rates"),
|
||||
Collections.singletonMap("currency", "USD"));
|
||||
final var rates = new JSONObject(response).getJSONObject("data").getJSONObject("rates");
|
||||
System.out.printf("The USD-EUR exchange rate is: %s%n", rates.getString("EUR"));
|
||||
}
|
||||
} catch (CryptoException e) {
|
||||
System.err.println("HTTP Status Code: " + e.getStatusCode());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue