cryptoprice/README.md
Erik C. Thauvin f252c1602b Changed marketPrice to spotPrice.
Using BigDecimal instead of Double.
2021-05-26 03:11:59 -07:00

3.9 KiB

License (3-Clause BSD) Release Maven Central Nexus Snapshot

Known Vulnerabilities Quality Gate Status GitHub CI CircleCI

Retrieve cryptocurrencies current prices

A simple Kotlin/Java/Android implementation of the spot price Coinbase Public API.

Examples (TL;DR)

import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice

// ...

val btc = spotPrice("BTC") // Bitcoin
println(btc.amount)

val eth = spotPrice("ETH", "EUR") // Ethereum in Euros
println(eth.amount)

Spot Price

The spotPrice function defines the following parameters:

spotPrice(
    base: String, // Required 
    currency: String = "USD",
    date: LocalDate? = null,
)
Parameters Description
base The cryptocurrency ticker symbol (BTC, ETH, ETH2, etc.)
currency The fiat currency ISO 4217 code. (USD, GBP, EUR, etc.)
date The LocalDate for historical price data.

A CryptoPrice is returned defined as follows:

CryptoPrice(val base: String, val currency: String, val amount: BigDecimal)

The parameter names match the Coinbase API.

To display the amount as a fomatted currency use the toCurrency function:

val price = CryptoPrice("BTC", "EUR", 12345.67.toBigDecimal())
println(price.toCurrency()) // will print €12,345.67

Extending

A generic apiCall() function is available to access other API data endpoints. For example to retried the current buy price of a cryptocurrency:

apiCall(paths = listOf("prices", "BTC-USD", "buy"), params = emptyMap())

will return something like:

{"data":{"base":"BTC","currency":"USD","amount":"58977.17"}}

See the examples for more details.

Gradle, Maven, etc.

To use with Gradle, include the following dependency in your build file:

dependencies {
    implementation("net.thauvin.erik:cryptoprice:0.9.0")
}

Instructions for using with Maven, Ivy, etc. can be found on Maven Central.