A functional Scala client for Binance, powered by cats-effect and fs2.
This client is rate limited, based on Binance API specification.
JDK LTS version | Scala version |
---|---|
11 | 2.12.x, 2.13.x, 3.0.x, 3.1.x |
17 | 2.12.x, 2.13.x, 3.0.x, 3.1.x |
If you use sbt add the following dependency to your build file:
libraryDependencies += "io.github.paoloboni" %% "binance-scala-client" % "<version>"
The client initialisation returns an API object as a Cats Resource. There are two factories available at the moment, one for Spot API and the other one for Future API.
import cats.effect.{IO, Resource}
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.SpotConfig
import io.github.paoloboni.binance.spot.SpotApi
val config = SpotConfig.Default(
apiKey = "***", // your api key
apiSecret = "***" // your api secret
)
val client: Resource[IO, SpotApi[IO]] = BinanceClient.createSpotClient[IO](config)
import cats.effect.{IO, Resource}
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.FapiConfig
import io.github.paoloboni.binance.fapi.FutureApi
val config = FapiConfig.Default(
apiKey = "***", // your api key
apiSecret = "***" // your api secret
)
val client: Resource[IO, FutureApi[IO]] = BinanceClient.createFutureClient[IO](config)
The API documentation is available here.
This is a sample app to monitor the exchange prices (fetch every 5 seconds).
import cats.effect.std.Console
import cats.effect.{IO, IOApp}
import fs2.Stream
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.SpotConfig
import scala.concurrent.duration.DurationInt
object PriceMonitor extends IOApp.Simple {
val config = SpotConfig.Default(
apiKey = "***",
apiSecret = "***"
)
override def run: IO[Unit] =
BinanceClient
.createSpotClient[IO](config)
.use { client =>
Stream
.awakeEvery[IO](5.seconds)
.repeat
.evalMap(_ => client.V3.getPrices())
.evalMap(prices => Console[IO].println("Current prices: " + prices))
.compile
.drain
}
}
sbt test
FAPI_API_KEY=<your-fapi-api-key> \
FAPI_SECRET_KEY=<your-fapi-secret-key> \
SPOT_API_KEY=<your-spot-api-key> \
SPOT_SECRET_KEY=<your-spot-secret-key> \
sbt e2e:test
Thanks goes to these wonderful people (emoji key):
DarkWingMcQuack 💻 |
Swoorup Joshi 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!