.circleci | ||
.github/workflows | ||
.idea | ||
.settings | ||
.vscode | ||
examples | ||
lib/bld | ||
src | ||
.gitattributes | ||
.gitignore | ||
.gitlab-ci.yml | ||
baseline.xml | ||
bitbucket-pipelines.yml | ||
bld | ||
bld.bat | ||
cryptoprice.iml | ||
detekt-baseline.xml | ||
LICENSE.txt | ||
pom.xml | ||
README.md | ||
sonar-project.properties |
Retrieve cryptocurrencies current (buy, sell or spot) prices
A simple implementation of the prices Coinbase Public API.
Examples (TL;DR)
import net.thauvin.erik.crypto.CryptoPrice.Companion.buyPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.sellPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
val btc = spotPrice("BTC") // Bitcoin
println(btc.amount)
val eth = sellPrice("ETH", "EUR") // Ethereum in Euro
println(eth.amount)
val eth = buyPrice("LTC", "GBP") // Litecoin in Pound sterling
println(eth.amount)
Gradle, Maven, etc.
To use with Gradle, include the following dependency in your build file:
repositories {
mavenCentral()
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } // only needed for SNAPSHOT
}
dependencies {
implementation("net.thauvin.erik:cryptoprice:1.0.1")
}
Instructions for using with Maven, Ivy, etc. can be found on Maven Central.
Prices
The spotPrice
, buyPrice
and sellPrice
functions define the following parameters:
spotPrice(
base: String, // Required
currency: String = "USD",
date: LocalDate? = null
)
buyPrice(
base: String, // Required
currency: String = "USD"
)
sellPrice(
base: String, // Required
currency: String = "USD"
)
Parameters | Description |
---|---|
base |
The cryptocurrency ticker symbol (BTC , ETH , LTC , etc.) |
currency |
The fiat currency ISO 4217 code. (USD , GBP , EUR , etc.) |
date |
The LocalDate for historical price data. |
A CryptoPrice
object is returned defined as follows:
CryptoPrice(val base: String, val currency: String, val amount: BigDecimal)
The parameter names match the Coinbase API.
Format
To display the amount as a formatted currency, use the toCurrency
function:
val euro = CryptoPrice("BTC", "EUR", 23456.78.toBigDecimal())
println(euro.toCurrency()) // €23,456.78
val krone = CryptoPrice("BTC", "DKK", 123456.78.toBigDecimal())
println(krone.toCurrency(Locale.Builder().setLanguage("da").setRegion("DK").build())) // 123.456,78 kr.
JSON
To convert a CryptoPrice
object back to JSON, use the toJson
function:
val price = CryptoPrice("BTC", "USD", 34567.89.toBigDecimal())
println(price.toJson())
output:
{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}
The data
object matches the Coinbase API. To specify a different (or no) key, use:
println(price.toJson("bitcoin"))
println(price.toJson("")) // or price.toString()
output:
{"bitcoin":{"base":"BTC","currency":"USD","amount":"34567.89"}}
{"base":"BTC","currency":"USD","amount":"34567.89"}
Similarly, to create a CryptoPrice
object from JSON, use the toPrice
function:
val btc = """{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}""".toPrice()
val eth = """{"ether":{"base":"ETH","currency":"USD","amount":"2345.67"}}""".toPrice("ether")
Extending
A generic apiCall()
function is available to access other data API endpoints. For example to retrieve the exchange rates:
apiCall(listOf("exchange-rates"), mapOf("currency" to "usd"))
will return something like:
{"data":{"currency":"BTC","rates":{"AED":"36.73","AFN":"589.50","...":"..."}}}
See the examples for more details.