mirror of
https://github.com/Permissionless-Software-Foundation/bch-js.git
synced 2026-09-22 09:02:01 -07:00
74 lines
1.8 KiB
JavaScript
74 lines
1.8 KiB
JavaScript
const axios = require("axios")
|
|
|
|
let _this
|
|
|
|
class Mining {
|
|
constructor(config) {
|
|
this.restURL = config.restURL
|
|
this.apiToken = config.apiToken
|
|
|
|
// Add JWT token to the authorization header.
|
|
this.axiosOptions = {
|
|
headers: {
|
|
authorization: `Token ${this.apiToken}`
|
|
}
|
|
}
|
|
|
|
_this = this
|
|
}
|
|
|
|
async getBlockTemplate(template_request) {
|
|
try {
|
|
const response = await axios.get(
|
|
`${this.restURL}mining/getBlockTemplate/${template_request}`,
|
|
_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
|