BikBikBikBikBik/blocke

View on GitHub
lib/api/sochain.js

Summary

Maintainability
A
0 mins
Test Coverage
/*
Copyright (C) 2017 BikBikBikBikBik

This file is part of blocke.

blocke is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

blocke is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with blocke.  If not, see <http://www.gnu.org/licenses/>.
*/
const ApiClientBase = require('./api-client-base');
const resources = require('./resources');

const _soChainSupportedNetworks = [ 'BTC', 'DASH', 'DOGE', 'LTC' ];

class SoChainClient extends ApiClientBase {
    constructor(network) {
        super('https://chain.so/api/v2/');
        
        let formattedNetwork = network.trim().toUpperCase();
        if (_soChainSupportedNetworks.indexOf(formattedNetwork) === -1) {
            throw new Error(resources.generateUnsupportedNetworkMessage(network));
        }
        
        this._network = formattedNetwork;
    }
    
    getAccount(accountAddress) {
        return this.executeRequest(`get_address_balance/${this._network}/${accountAddress}`, 'Account').then((res) => res.data);
    }
    
    getBlockByNumberOrHash(blockId) {
        return this.executeRequest(`get_block/${this._network}/${blockId}`, 'Block').then((res) => res.data);
    }
    
    getNetworkInfo() {
        return this.executeRequest(`get_info/${this._network}`, 'Network Info').then((res) => res.data);
    }
    
    getTransaction(transactionHash) {
        return this.executeRequest(`get_tx/${this._network}/${transactionHash}`, 'Transaction').then((res) => res.data);
    }
}

module.exports = SoChainClient;