1.0.0 • Published 6 years ago

blockchain-bridge v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

CircleCI

BlockchainBridge

This library has everything we need to get data about bitcoin, ethereum and Life Tokens.

For all the asyncronous calls we wrap the returned values (or errors) in a Maybe() monad, using the folktale library. The reason behind it is to never have an empty value when the async call completes/fails.

Why this? Because it saves us many security checks. Without a wrapped value, we would need to check if the returned value containes a value or no. For example, in the axios.get(), the response is request.data.something. If you want to access a child in a logic safely, it would needed to check something like

if(request){
  if(request.data) {
    return request.data.something
  }
}

How to use the returned values

Each function returns a Maybe() monad and it needs to be tested against the possible cases in this way:

// call the async function 
const transactions = await getBtcTransactions('1BvvRfz4XnxSWJ524TusetYKrtZnAbgV3r');
    
    // unwrap the data and 
    transactions.matchWith({
      //if success, use a lambda to return the wrapped data, or return the 
      // data inside a new object
      Success: ({data}) => `User's balance is ${data}`,
      //if failure, just throw an error
      Failure: error => {throw new Error(`Error while user\'s balance.`)}
    })

The app flow

The first thing the app will try to do, is to fetch the api keys from our server. The keys live in an object called keychain, that can store keys from different services and the state of each key fetch (if loading, success, empty or failure).

As soon as the app is trying to fetch the keys, the state of the key is changed to Loading() and the request is done through a Promise that will update accordingly if success/fails.

The app will try to fetch the keys an arbitrary number of times, after which it will return a "Impossible to fetch keys" error.

It is not allowed to query the services until the keys in the keychain are fetched, but as soon as the key of a service is ready, that service can be queried even if the other keys are not ready yet, which gives us quite a lot of flexibility and improves UX and UI responses (for example we can granularly show which services we're fetching and which we're still waiting for the key).

Bitcoin settings

Mnemonic: network great leisure blanket laptop repeat report idea someone couple long horse liberty stumble sauce

Networks switch

When the library is imported, it assumes to be using the livenet. To pass from livenet to testnet you can do:

import {isTestnet} from 'blockchai-bridge/switch'

isTestnet()

you can also specify an Ethereum network like isTestnet('rinkeby')

and can swith back to live using isTestnet(false)