LibCoin
A modern library for accessing cryptocurrency data from the browser and server (NodeJS). WebSocket and HTTP Request calls are embedded together, using something called a Cascade structure, to ensure data is always its latest value.
If you only want SLP token data, try looking at my other library, SLPClient.
Currently supported assets:
- BCH
- SLP
Installation
npm install LibCoin
This library was built using NodeJS v8.14.0
Simple Usage
# 1. Initialize your coin or token:
var BCH = await new LibCoin().BCH().build();
# 2. Make an HTTP request to get a balance:
var balanceInBCH = await BCH.getBalance(address);
Advanced Features (Cascade)
Most cryptocurrency libraries today are more difficult than they need to be, largely because of separated request and websocket development teams. Traditional requests are generally easier to test, so they're done first. WebSocket requests are harder to test, but have the advantage of always giving you the most recent data as it comes in. Generally, a real-time web developer will use both these endpoints in their code to get the intitial data, then update the data as it comes in. LibCoin aims to do this for you automatically, using a .live() chain variable. Traditional requests are available with a .get() chain, and websocket requests are available with the .on() chain.
For example. You can access a given data (e.g. balance)in three different ways:
BCH.balance(address).get()will make an HTTP request to get the latest balance.BCH.balance(address).on(callback)fires a callback with the latest Websocket data as it comes inBCH.balance(address).live()is a combination of .get() and .on(), which first performs an HTTP request, then updates its response with the latest data from the WebSocket. This function returns apointerto an object containing the latest data.
So, instead of firing a callback, the .live() variable allows your object to have a key which always refreshes with the latest value. This should produce much cleaner and simpler code.
var x = {};
x.userBalance = BCH.balance(address).live();
x.userHistory = BCH.history(address).live();
.price, .block, .transaction, .fee, .utxo, .coinBalance, .tokenBalance, and .history() are some other methods with this feature. If you're interested in using this, see the Cascade documentation for the latest rules, tricks, and gotchas. Alternatively, just append on, get, or live before the capitalized data name (e.g. getBalance or onBlock) to use the library without chaining.