Retrieve cryptocurrencies current prices https://github.com/ethauvin/cryptoprice
Find a file
Erik C. Thauvin 727eab3de7
Updated dependencies
Bumped Kotlin to version 2.1.0
Bumped Kotlin extension to version 1.0.3
Bumped Dokka extension to version 1.0.2
Bumped Detekt extension to version 0.9.7
Bumped Junit to version to 5.11.4
Bumped Gradle to version 8.12
2024-12-22 14:35:11 -08:00
.circleci Tests against Kotlin 2.0.20 2024-09-20 22:31:08 -07:00
.github/workflows Updated dependencies 2024-12-22 14:35:11 -08:00
.idea Updated dependencies 2024-12-22 14:35:11 -08:00
examples Updated dependencies 2024-12-22 14:35:11 -08:00
lib/bld Updated dependencies 2024-12-22 14:35:11 -08:00
src Updated dependencies 2024-12-22 14:35:11 -08:00
.gitattributes Initial commit. 2021-05-08 01:35:55 -07:00
.gitignore Updated dependencies 2024-09-09 13:52:08 -07:00
.gitlab-ci.yml Updated dependencies 2024-09-09 13:52:08 -07:00
bitbucket-pipelines.yml Updated dependencies 2024-09-09 13:52:08 -07:00
bld Moved from Gradle to bld 2023-11-08 17:05:19 -08:00
bld.bat Moved from Gradle to bld 2023-11-08 17:05:19 -08:00
cryptoprice.iml Improved response parsing. 2022-01-03 11:51:31 -08:00
detekt-baseline.xml Updated dependencies 2023-09-23 10:50:58 -07:00
LICENSE.txt Updated copyright 2024-05-10 10:10:39 -07:00
pom.xml 1.0.1-SNAPSHOT 2024-09-20 22:43:10 -07:00
README.md Updated dependencies 2024-12-22 14:35:11 -08:00
sonar-project.properties Added sonar properties 2023-11-08 17:09:49 -08:00

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

Quality Gate Status GitHub CI CircleCI

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)

bld

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

repositories = List.of(MAVEN_CENTRAL);

scope(compile)
    .include(dependency("net.thauvin.erik:cryptoprice:1.0.2"));

Be sure to use the bld Kotlin extension in your project.

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.2")
}

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.

Contributing

If you want to contribute to this project, all you have to do is clone the GitHub repository:

git clone git@github.com:ethauvin/cryptoprice.git

Then use bld to build:

cd cryptoprice
./bld compile

The project has an IntelliJ IDEA project structure. You can just open it after all the dependencies were downloaded and peruse the code.