NiceHash API からリグ情報を取得するスクリプト
#TypeScript #Node.js
code:ts
require("dotenv").config();
if (!process.env.NICEHASH_API_KEY)
throw new Error("NICEHASH_API_KEY is not set");
if (!process.env.NICEHASH_API_SECRET)
throw new Error("NICEHASH_API_SECRET is not set");
if (!process.env.NICEHASH_ORG_ID) throw new Error("NICEHASH_ORG_ID is not set");
import axios, { AxiosError } from "axios";
import { createHmac, randomBytes } from "crypto";
import { stringify } from "querystring";
import { name as APP_NAME, version as APP_VERSION } from "./package.json";
const NICEHASH_API_KEY = process.env.NICEHASH_API_KEY;
const NICEHASH_API_SECRET = process.env.NICEHASH_API_SECRET;
const NICEHASH_ORG_ID = process.env.NICEHASH_ORG_ID;
const NICEHASH_API_HOST = "https://api2.nicehash.com" as const;
const USER_AGENT =
${APP_NAME}/${APP_VERSION} (+https://github.com/hoge/foo) as const;
declare module NicehashRigs {
interface MinerStatuses {
MINING: number;
}
interface RigTypes {
MANAGED: number;
}
interface DevicesStatuses {
MINING: number;
DISABLED: number;
}
interface DeviceType {
enumName: string;
description: string;
}
interface Status {
enumName: string;
description: string;
}
interface PowerMode {
enumName: string;
description: string;
}
interface Speed {
algorithm: string;
title: string;
speed: string;
displaySuffix: string;
}
interface Intensity {
enumName: string;
description: string;
}
interface Device {
id: string;
name: string;
deviceType: DeviceType;
status: Status;
temperature: number;
load: number;
revolutionsPerMinute: number;
revolutionsPerMinutePercentage: number;
powerMode: PowerMode;
powerUsage: number;
speeds: Speed[];
intensity: Intensity;
nhqm: string;
}
interface Algorithm {
enumName: string;
description: string;
}
interface Stat {
statsTime: number;
market: string;
algorithm: Algorithm;
unpaidAmount: string;
difficulty: number;
proxyId: number;
timeConnected: number;
xnsub: boolean;
speedAccepted: number;
speedRejectedR1Target: number;
speedRejectedR2Stale: number;
speedRejectedR3Duplicate: number;
speedRejectedR4NTime: number;
speedRejectedR5Other: number;
speedRejectedTotal: number;
profitability: number;
}
interface MiningRig {
rigId: string;
type: string;
name: string;
statusTime: number;
joinTime: number;
minerStatus: string;
groupName: string;
unpaidAmount: string;
softwareVersions: string;
devices: Device[];
cpuMiningEnabled: boolean;
cpuExists: boolean;
stats: Stat[];
profitability: number;
localProfitability: number;
rigPowerMode: string;
}
interface Pagination {
size: number;
page: number;
totalPageCount: number;
}
interface RootObject {
minerStatuses: MinerStatuses;
rigTypes: RigTypes;
totalRigs: number;
totalProfitability: number;
groupPowerMode: string;
totalDevices: number;
devicesStatuses: DevicesStatuses;
unpaidAmount: string;
path: string;
btcAddress: string;
nextPayoutTimestamp: string;
lastPayoutTimestamp: string;
miningRigGroups: any[];
miningRigs: MiningRig[];
rigNhmVersions: string[];
externalAddress: boolean;
totalProfitabilityLocal: number;
pagination: Pagination;
}
}
function createSignature(
method: string,
endpoint: string,
time: number,
nonce: string,
query: string | Record<any, any> | null = null,
body: string | object | null = null
) {
const hmac = createHmac("sha256", NICEHASH_API_SECRET);
hmac.update(
${NICEHASH_API_KEY}\0${time}\0${nonce}\0\0${NICEHASH_ORG_ID}\0\0${method.toUpperCase()}\0${endpoint}\0
);
if (query)
hmac.update(${typeof query === "object" ? stringify(query) : query});
if (body)
hmac.update(\0${typeof body === "object" ? JSON.stringify(body) : body});
return ${NICEHASH_API_KEY}:${hmac.digest("hex")};
}
async function getRigs() {
const client = axios.create({
baseURL: NICEHASH_API_HOST,
});
const date = Date.now();
const nonce = randomBytes(16).toString("base64");
const data = await new Promise<NicehashRigs.RootObject>((resolve, reject) =>
client
.get<NicehashRigs.RootObject>(/main/api/v2/mining/rigs2, {
responseType: "json",
headers: {
"X-Time": date,
"X-Nonce": nonce,
"X-Organization-Id": NICEHASH_ORG_ID,
"X-Request-Id": nonce,
"X-User-Agent": USER_AGENT,
"X-User-Lang": "ja",
"X-Auth": createSignature(
"GET",
/main/api/v2/mining/rigs2,
date,
nonce
),
},
})
.then(({ data }) => {
resolve(data)
})
.catch((err) => {
throw err as AxiosError;
})
);
return data
}
const rigs = await getRigs()
console.log(JSON.stringify(rigs))