manyfacedcoin
v1.0.124
Published
Modern library for using cryptocurrency 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.
Downloads
7
Readme
ManyFacedCoin
Modern library for using cryptocurrency 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 manyfacedcoin
This library was built using NodeJS v8.14.0
Simple Usage
# 1. Initialize your coin or token:
var BCH = await new ManyFacedCoin().BCH().build();
# 2. Make an HTTP request to get a balance:
var balanceInBCH = await BCH.getBalance(address);
Advanced Features (Cascade)
A lot of cryptocurrency libraries are more verbose than they need to be. Traditional HTTP 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. Nowadays most people in Bitcoin are using BitDB, which provides built-in websockets to a MongoDB instance.
Generally, a real-time web developer will use both HTTP and WebSocket requests in their code. An HTTP request gets the intitial data as the page loads, then callbacks are used to update the data as the network state changes. ManyFacedCoin 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 apointer
to 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.