xchainjs-lib
xchainjs-lib copied to clipboard
ADD: Refactor providers and explorers implementation for @next release
overview
Currently the providers(APis to fetch live information and broadcast txs) and Explorers are hard wired into each client.
This is suboptimal for the following reasons:
- selecting a provider(sochain,haskoin,etherscan, etc) should be up to the user of the library
- providers should allow users to configure paid services (paid blockchair, paid infura node, etc)
- by default the library should support fall back providers in coase 1 providers is down
- it should be easy for users to develop their own provider and plug it into the library
Work to do
- [ ] Prototype explorers & providers with xchain-bitcoin
- [ ] Prototype explorers & providers with xchain-ethereum
- [ ] Support configurable Providers & Explorers for 3rd party functions
- [ ] For each Chain, default to free, reliable Providers & Explorers
- [ ] move all providers/explorers into xchain-client, since several bproviders support more that one chain BTC/BCH, etc
Example of finished product
- install the right libraries (npm,yarn,pnpm,etc)
# install the required base lib yarn add @xchainjs/xchain-client # install chains you want to interact with yarn add @xchainjs/xchain-bitcoin @xchainjs/xchain-ethereum
- Setup the client libs
import { Providers, Explorers } from '@xchainjs/xchain-client'; //import chains you want import { BtcClient } from '@xchainjs/xchain-bitcoin'; import { EthClient } from '@xchainjs/xchain-ethereum'; const network = 'testnet' const phrase = 'atom green various power must another rent imitate gadget creek fat then' //ETHEREUM Config const ethConfig = { network, phrase, explorer: Explorers.ETH.ETHERSCAN, providers: { // all methods take an array of providers, they will be invoked in order until one returns a success getBalance: [Providers.ETH.BLOCKCHAIR,Providers.ETH.ETHERSCAN], getTransactions: [Providers.ETH.BLOCKCHAIR,Providers.ETH.ETHERSCAN], getFees: [Providers.ETH.ETHPLORER, Providers.ETH.ETHERSCAN], transfer: [Providers.ETH.ETHERSCAN], } } const ethClient = EthClient(ethConfig) //BITCOIN Config const btcConfig = { network, phrase, explorer: Explorers.BTC.BLOCKSTREAM, providers: { // all methods take an array of providers, they will be invoked in order until one returns a success getBalance: [Providers.BTC.BLOCKCHAIR, Providers.BTC.SOCHAIN, Providers.BTC.HASKOIN ], getTransactions: [Providers.BTC.BLOCKCHAIR, Providers.BTC.SOCHAIN, Providers.BTC.HASKOIN ], getFees: [Providers.BTC.BLOCKCHAIR, Providers.BTC.HASKOIN], transfer: [Providers.BTC.BLOCKCHAIR], } } const btcClient = BtcClient(btcConfig) //overide a config with a provider const apiKey = '12345' const blockchairBtc = new Providers.BlockChair({apiKey, chain: 'BTC'}) //use paid blockchair and if we run out of credits, fall back to public api endpoints btcClient.config.providers.getBalance = [blockchairBtc, Providers.BTC.SOCHAIN, Providers.BTC.HASKOIN ]