npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sb-api

v0.4.0

Published

JS client library for Suredbits APIs

Downloads

65

Readme

npm version Build Status

This is a client library for interacting with the Suredbits APIs for NFL, NBA and cryptocurrency market data. See our API docs for more information.

You're also welcome to join our Slack, if you have any questions about our APIs, our open source Bitcoin library Bitcoin-S or just want to discuss Bitcoin, Lightning, NBA & NFL or anything else.

Add it to your project:

Yarn

$ yarn add sb-api

npm

$ npm install --save sb-api

Usage:

Setting up the connection

Our APIs are paid for by using the Bitcoin Lightning Network. We suport the major Lightning implementations: lnd, c-lightning and Eclair.

We assume you have a running Lightning client. Behind the scenes it does two things with the provided client:

  1. Pay invoices. We pay for access to the Suredbits` APIs by paying Lightning invoices.
  2. Generate invoices without an amount specified. When setting up a subscription we provide a refund invoice where money can be refunded to us if we abort or subscription before it's up or something causes the server to abort the subscription.

You should not keep large amounts of money on the node you're using with this library.

Eclair

import { Eclair, Sockets } from 'sb-api'

const eclair = await Eclair({ rpcPass: 'super_secret' })
const futuresSocket = await Sockets.exchangeSpot(eclair)

LND

import { Lnd, Sockets } from 'sb-api'

const lnd = await Lnd()
// You then pass lnd into the appropriate socket/REST API you're interested in.

c-lightning

import { CLightning } from 'sb-api'

const client = await CLightning()

// You then pass client into the appropriate socket/REST API you're interested in.

See doc/ln-clients.md for more information on how to use your favorite LN implementation, and what the available options are.

Request data from REST APIs

We provide REST APIs for data that are not realtime-based (such as historical market data). These functions are regular promises that can call .then or await on. Behind the scenes we make a HTTP request, pay the accompanying invoice and then use the preimage of the invoice to decrypt the received data.

const lnd = await Lnd()

const historicalRest = HistoricalRestAPI(lnd)

const historicalResponse = await historicalRest.call({
  exchange: 'bitstamp',
  pair: 'BTCUSD',
  period: 'daily',
  year: 2018,
})

const nflRest = NflRestAPI(lnd)

const nflResponse = await nflRest.players({
  firstName: 'Colin',
  lastName: 'Kaepernick',
})

Request data from socket based APIs

We provide streaming realtime data over WebSockets. These APIs require you to specify callbacks (functions) that gets executed whenever certain events happen. Note that if you're using TypeScript, the callbacks are fully typed, with inferred types from the exchange you're requesting data from.

import { Lnd, Sockets } from 'sb-api'

const ln = await Lnd()
const spot = await Sockets.exchangeSpot(ln)
spot.tickers({
  duration: 10000,
  exchange: 'binance',
  // data has types! data.bidSize works, data.wrongField gives an error
  onData: data => console.log('received some data:', data),
  // snap has types! snap.map(s => handleS(s)) works, snap.wrongField gives an error
  onSnapshot: snap => console.log('received a snapshot:', snap),
  // this callback is called at the end of your subscription, and provides
  // you with a list of all the elements you've received
  onSubscriptionEnded: datapoints => datapoints[0],
  refundInvoice,
})

// the comments above on the callbacks used for the spot socket
// also apply for the futures socket

const futures = await Sockets.exchangeFutures(ln)
futures.trades({
  exchange: 'bitmex',
  symbol: 'BTCUSD',
  interval: 'biquarterly',
  onData: data => data,
  onSnapshot: snap => snap[0],
  onSubscriptionEnded: datapoints => datapoints[0],
  duration: 20000,
})

const nbaSocket = await Sockets.nbaTestnet()
const dalRoster = await nbaSocket.roster({ teamId: 'DAL' })

See our API docs for complete code samples for making requests, as well as what responses look like, for all API endpoints and request types.

Types

We recommend using TypeScript with this library. It has excellent type support built in, as it's written in TypeScript. When using TS you'll have all parameters passed to your requests type checked. The data type of the argument in onData or onSnapshot in a exchange socket will also change based on which exchange and which data type (tickers, books or trades) you request.

Debug logging

This library uses the debug module for logging what's going on. debug logs using what it calls namespaces. This library logs under the following namespaces:

  • sb-abi:lightning:lnd - LND related functionality
  • sb-api:lightning:eclair - Eclair related functionality
  • sb-api:lightning:clightning - c-lightning related functionality
  • sb-api:socket:base - functionality common for all sockets
  • sb-api:socket:ppc - functionality common for pay-per-call sockets (NBA)
  • sb-api:socket:exchange - crypto market data logging
  • sb-api:validation - logs the validation of incoming data from the API
  • sb-api:rest - logs queries and responses to the Suredbits REST APIs

To activate logging output for a given namespace you need to set the DEBUG environment variable. Namespaces are hierarchically organized with : as the level separator. It's possible to specify multiple namespaces by comma-separating them. DEBUG=sb-api:* causes all namespaces to get logged. Setting DEBUG to sb-api:socket:* activates the sb-api:socket:base, sb-api:socket:ppc and sb-api:socket:exchange namespaces.

DEBUG=sb-api:lightning:*,sb-api:socket:base,sb-api:socket:ppc would enable logging of all Lightning activity, the functionality common for all sockets and the functionality common for pay-per-call sockets (NBA).

Utilities

The script decrypt.ts is provided as a utility script for decrypting payloads adhering to the PAID standard. Usage: npx ts-node -T decrypt.ts $encrypted_payload $invoice_preimage.

Publishing

$ yarn publish

Lints, checks formatting, makes new git tag, pushes new git tag, build, pushes build to npm

Testing

When testing this library, you need two secret environment variables, MAGIC_UUID and MAGIC_PREIMAGE. After that, simply do:

$ env MAGIC_UUID=... MAGIC_PREIMAGE=... yarn test --your --options --to --jest --here