paoloboni / binance-scala-client

A functional Scala client for Binance

Version Matrix

Scala client for Binance API

All Contributors

Build Status Latest version License: MIT Join the chat at https://gitter.im/binance-scala-client/community

A functional Scala client for Binance, powered by cats-effect and fs2.

This client is rate limited, based on Binance API specification.

Getting started

If you use sbt add the following dependency to your build file:

libraryDependencies += "io.github.paoloboni" %% "binance-scala-client" % "<version>"

APIs supported

Spot API

Rest

WebSocket

Future API

Rest

WebSocket

Initialise the client

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.

Spot client

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[IO](
  apiKey = "***",     // your api key
  apiSecret = "***"   // your api secret
)
val client: Resource[IO, SpotApi[IO]] = BinanceClient.createSpotClient[IO](config)

Future client

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[IO](
  apiKey = "***",     // your api key
  apiSecret = "***"   // your api secret
)
val client: Resource[IO, FutureApi[IO]] = BinanceClient.createFutureClient[IO](config)

Documentation

The API documentation is available here.

Example app

This is a sample app to monitor the exchange prices (fetch every 5 seconds).

import cats.effect.{ExitCode, IO, IOApp}
import fs2.Stream
import io.github.paoloboni.binance.BinanceClient
import io.github.paoloboni.binance.common.SpotConfig
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

import scala.concurrent.duration.DurationInt

object PriceMonitor extends IOApp {

  val config = SpotConfig.Default[IO](
    apiKey = "***",
    apiSecret = "***"
  )

  override def run(args: List[String]): IO[ExitCode] = {
    implicit def log: Logger[IO] = Slf4jLogger.getLogger[IO]

    BinanceClient
      .createSpotClient[IO](config)
      .use { client =>
        Stream
          .awakeEvery[IO](5.seconds)
          .repeat
          .evalMap(_ => client.getPrices())
          .evalMap(prices => log.info("Current prices: " + prices))
          .compile
          .drain
      }
      .redeem(
        { t =>
          log.error(t)("Something went wrong")
          ExitCode(1)
        },
        _ => ExitCode.Success
      )
  }
}

Contributors

Thanks goes to these wonderful people (emoji key):


DarkWingMcQuack

💻

This project follows the all-contributors specification. Contributions of any kind welcome!