Implemented buyPrice and sellPrice. Closes #1

This commit is contained in:
Erik C. Thauvin 2022-08-29 19:21:44 -07:00
parent c046cd7341
commit bef4b2ee20
18 changed files with 152 additions and 98 deletions

View file

@ -3,7 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("application")
id("com.github.ben-manes.versions") version "0.42.0"
kotlin("jvm") version "1.6.21"
kotlin("jvm") version "1.7.10"
}
// ./gradlew run
@ -20,7 +20,8 @@ repositories {
}
dependencies {
implementation("net.thauvin.erik:cryptoprice:0.9.0")
implementation("net.thauvin.erik:cryptoprice:1.0.0-SNAPSHOT")
implementation("org.json:json:20220320")
}
java {

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View file

@ -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());

View file

@ -2,9 +2,10 @@ package com.example
import net.thauvin.erik.crypto.CryptoException
import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall
import net.thauvin.erik.crypto.CryptoPrice.Companion.buyPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.sellPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
import org.json.JSONObject
import java.io.IOException
fun main(args: Array<String>) {
@ -19,20 +20,21 @@ fun main(args: Array<String>) {
println()
// Get current Ethereum spot price in Pound sterling.
val gbpPrice = spotPrice("ETH", "GBP")
println("The current Ethereum price is ${gbpPrice.toCurrency()}")
// Get current Ethereum sell price in Pound sterling.
val gbpPrice = sellPrice("ETH", "GBP")
println("The current Ethereum sell price is ${gbpPrice.toCurrency()}")
// Get current Litecoin spot price in Euro.
val euroPrice = spotPrice("LTC", "EUR")
println("The current Litecoin price is ${euroPrice.toCurrency()}")
// Get current Litecoin buy price in Euro.
val euroPrice = buyPrice("LTC", "EUR")
println("The current Litecoin buy price is ${euroPrice.toCurrency()}")
println()
// Get current Bitcoin buy price using API.
// See: https://developers.coinbase.com/api/v2#get-buy-price
val buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice()
println("The current ${buyPrice.base} buy price is ${buyPrice.amount} in ${buyPrice.currency}")
// Get exchange rate using API.
// See: https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates
val response = apiCall(listOf("exchange-rates"), mapOf("currency" to "usd"))
val rates = JSONObject(response).getJSONObject("data").getJSONObject("rates")
println("The USD-EUR exchange rate is: ${rates.getString("EUR")}")
}
} catch (e: CryptoException) {
System.err.println("HTTP Status Code: ${e.statusCode}")