2021-01-02 15:41:12 -08:00
|
|
|
const axios = require('axios')
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
let _this
|
|
|
|
|
|
|
|
|
|
class Mining {
|
2021-01-02 15:41:12 -08:00
|
|
|
constructor (config) {
|
2020-07-27 21:32:51 -07:00
|
|
|
this.restURL = config.restURL
|
|
|
|
|
this.apiToken = config.apiToken
|
2020-11-11 17:47:46 -08:00
|
|
|
this.authToken = config.authToken
|
2020-07-27 21:32:51 -07:00
|
|
|
|
2020-11-11 17:47:46 -08:00
|
|
|
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}`
|
|
|
|
|
}
|
2020-07-27 21:32:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_this = this
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
async getBlockTemplate (templateRequest) {
|
2020-07-27 21:32:51 -07:00
|
|
|
try {
|
|
|
|
|
const response = await axios.get(
|
2021-01-02 15:41:12 -08:00
|
|
|
`${this.restURL}mining/getBlockTemplate/${templateRequest}`,
|
2020-07-27 21:32:51 -07:00
|
|
|
_this.axiosOptions
|
|
|
|
|
)
|
|
|
|
|
return response.data
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.response && error.response.data) throw error.response.data
|
|
|
|
|
else throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
async getMiningInfo () {
|
2020-07-27 21:32:51 -07:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
async getNetworkHashps (nblocks = 120, height = 1) {
|
2020-07-27 21:32:51 -07:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
async submitBlock (hex, parameters) {
|
2020-07-27 21:32:51 -07:00
|
|
|
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
|