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

@ -1,10 +1,10 @@
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](https://opensource.org/licenses/BSD-3-Clause) [![Release](https://img.shields.io/github/release/ethauvin/cryptoprice.svg)](https://github.com/ethauvin/cryptoprice/releases/latest) [![Maven Central](https://img.shields.io/maven-central/v/net.thauvin.erik/cryptoprice.svg?label=maven%20central&color=blue)](https://search.maven.org/search?q=g:%22net.thauvin.erik%22%20AND%20a:%22cryptoprice%22) <!-- [![Nexus Snapshot](https://img.shields.io/nexus/s/net.thauvin.erik/cryptoprice?server=https%3A%2F%2Foss.sonatype.org%2F)](https://oss.sonatype.org/content/repositories/snapshots/net/thauvin/erik/cryptoprice/) -->
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](https://opensource.org/licenses/BSD-3-Clause) [![Release](https://img.shields.io/github/release/ethauvin/cryptoprice.svg)](https://github.com/ethauvin/cryptoprice/releases/latest) <!-- [![Maven Central](https://img.shields.io/maven-central/v/net.thauvin.erik/cryptoprice.svg?label=maven%20central&color=blue)](https://search.maven.org/search?q=g:%22net.thauvin.erik%22%20AND%20a:%22cryptoprice%22) --> [![Nexus Snapshot](https://img.shields.io/nexus/s/net.thauvin.erik/cryptoprice?server=https%3A%2F%2Foss.sonatype.org%2F)](https://oss.sonatype.org/content/repositories/snapshots/net/thauvin/erik/cryptoprice/)
[![Known Vulnerabilities](https://snyk.io/test/github/ethauvin/cryptoprice/badge.svg?targetFile=pom.xml)](https://snyk.io/test/github/ethauvin/cryptoprice?targetFile=pom.xml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ethauvin_cryptoprice&metric=alert_status)](https://sonarcloud.io/dashboard?id=ethauvin_cryptoprice) [![GitHub CI](https://github.com/ethauvin/cryptoprice/actions/workflows/gradle.yml/badge.svg)](https://github.com/ethauvin/cryptoprice/actions/workflows/gradle.yml) [![CircleCI](https://circleci.com/gh/ethauvin/cryptoprice/tree/master.svg?style=shield)](https://circleci.com/gh/ethauvin/cryptoprice/tree/master)
# Retrieve cryptocurrencies current prices
# Retrieve cryptocurrencies current (buy, sell or spot) prices
A simple Kotlin/Java/Android implementation of the spot price [Coinbase Public API](https://developers.coinbase.com/api/v2#get-spot-price).
A simple Kotlin/Java/Android implementation of the prices [Coinbase Public API](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-prices).
## Examples (TL;DR)
@ -16,7 +16,10 @@ import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
val btc = spotPrice("BTC") // Bitcoin
println(btc.amount)
val eth = spotPrice("ETH", "EUR") // Ethereum in Euros
val eth = sellPrice("ETH", "EUR") // Ethereum in Euro
println(eth.amount)
val eth = buyPrice("LTC", "GBP") // Litecoin in Pound sterling
println(eth.amount)
```
@ -28,21 +31,31 @@ To use with [Gradle](https://gradle.org/), include the following dependency in y
```gradle
dependencies {
implementation("net.thauvin.erik:cryptoprice:0.9.0")
implementation("net.thauvin.erik:cryptoprice:1.0.0-SNAPSHOT")
}
```
Instructions for using with Maven, Ivy, etc. can be found on [Maven Central](https://search.maven.org/artifact/net.thauvin.erik/cryptoprice/0.9.0/jar).
Instructions for using with Maven, Ivy, etc. can be found on [Maven Central](https://search.maven.org/search?q=g:%22net.thauvin.erik%22%20AND%20a:%22cryptoprice%22).
### Spot Price
### Prices
The `spotPrice` function defines the following parameters:
The `spotPrice`, `buyPrice` and `sellPrice` functions define the following parameters:
```kotlin
spotPrice(
base: String, // Required
currency: String = "USD",
date: LocalDate? = null,
date: LocalDate? = null
)
buyPrice(
base: String, // Required
currency: String = "USD"
)
sellPrice(
base: String, // Required
currency: String = "USD"
)
```
@ -57,7 +70,7 @@ A `CryptoPrice` object is returned defined as follows:
```kotlin
CryptoPrice(val base: String, val currency: String, val amount: BigDecimal)
```
The parameter names match the [Coinbase API](https://developers.coinbase.com/api/v2#get-spot-price).
The parameter names match the [Coinbase API](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-prices).
#### Format
@ -86,7 +99,7 @@ println(price.toJson())
{"data":{"base":"BTC","currency":"USD","amount":"34567.89"}}
```
The `data` object matches the [Coinbase API](https://developers.coinbase.com/api/v2#get-spot-price). To specify a different (or no) key, use:
The `data` object matches the [Coinbase API](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-prices#get-spot-price). To specify a different (or no) key, use:
```kotlin
println(price.toJson("bitcoin"))
@ -109,15 +122,15 @@ val eth = """{"ether":{"base":"ETH","currency":"USD","amount":"2345.67"}}""".toP
### Extending
A generic `apiCall()` function is available to access other [API data endpoints](https://developers.coinbase.com/api/v2#data-endpoints). For example to retrieve the current [buy price](https://developers.coinbase.com/api/v2#get-buy-price) of a cryptocurrency:
A generic `apiCall()` function is available to access other [data API endpoints](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-currencies). For example to retrieve the [exchange rates](https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates#get-exchange-rates):
```kotlin
apiCall(paths = listOf("prices", "BTC-USD", "buy"), params = emptyMap())
apiCall(listOf("exchange-rates"), mapOf("currency" to "usd"))
```
will return something like:
```json
{"data":{"base":"BTC","currency":"USD","amount":"34554.32"}}
{"data":{"currency":"BTC","rates":{"AED":"36.73","AFN":"589.50",...}}}
```
See the [examples](https://github.com/ethauvin/cryptoprice/blob/master/examples/) for more details.