const axios = require('axios') let _this class Mining { constructor (config) { this.restURL = config.restURL this.apiToken = config.apiToken this.authToken = config.authToken if (this.authToken) { // Add Basic Authentication token to the authorization header. this.axiosOptions = { headers: { authorization: this.authToken } } } else { // Add JWT token to the authorization header. this.axiosOptions = { headers: { authorization: `Token ${this.apiToken}` } } } _this = this } async getBlockTemplate (templateRequest) { try { const response = await axios.get( `${this.restURL}mining/getBlockTemplate/${templateRequest}`, _this.axiosOptions ) return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data else throw error } } async getMiningInfo () { try { const response = await axios.get( `${this.restURL}mining/getMiningInfo`, _this.axiosOptions ) return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data else throw error } } async getNetworkHashps (nblocks = 120, height = 1) { try { const response = await axios.get( `${this.restURL}mining/getNetworkHashps?nblocks=${nblocks}&height=${height}`, _this.axiosOptions ) return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data else throw error } } async submitBlock (hex, parameters) { let path = `${this.restURL}mining/submitBlock/${hex}` if (parameters) path = `${path}?parameters=${parameters}` try { const response = await axios.post(path, _this.axiosOptions) return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data else throw error } } } module.exports = Mining