Browse Source

Refactor api for Vercel

pull/32/merge
Luke Childs 2 years ago
parent
commit
cc7f87341d
  1. 6
      api/algorithms.js
  2. 5
      api/coins.js
  3. 8
      api/data.js
  4. 44
      api/nicehash.js
  5. 27
      api/util/send-json.js

6
api/algorithms.js

@ -1,5 +1,3 @@
const sendJson = require('./util/send-json');
const ANTMINER_S19_PRO_WATTAGE = 3250;
const RTX_3090_WATTAGE = 285;
const INNOSILICON_A6_PLUS_WATTAGE = 2100;
@ -69,5 +67,5 @@ const getAlgorithms = () => [
}
];
module.exports = getAlgorithms;
module.exports.handler = () => sendJson(getAlgorithms);
module.exports = async (request, response) => response.json(await getAlgorithms());
module.exports.getAlgorithms = getAlgorithms;

5
api/coins.js

@ -1,5 +1,4 @@
const fetch = require('isomorphic-fetch');
const sendJson = require('./util/send-json');
const getData = async endpoint => fetch(
`https://whattomine.com/${endpoint}.json`
@ -31,5 +30,5 @@ const getCoinData = async () => {
return coins;
};
module.exports = getCoinData;
module.exports.handler = () => sendJson(getCoinData);
module.exports = async (request, response) => response.json(await getCoinData());
module.exports.getCoinData = getCoinData;

8
api/data.js

@ -1,6 +1,5 @@
const sendJson = require('./util/send-json');
const getCoinData = require('./coins');
const getAlgorithms = require('./algorithms');
const {getCoinData} = require('./coins');
const {getAlgorithms} = require('./algorithms');
const algorithms = getAlgorithms();
@ -23,5 +22,4 @@ const getData = async () => {
return data;
};
module.exports = getData;
module.exports.handler = () => sendJson(getData);
module.exports = async (request, response) => response.json(await getData())

44
api/nicehash.js

@ -1,44 +0,0 @@
const fetch = require('isomorphic-fetch');
const sendJson = require('./util/send-json');
const SECONDS = 1;
const MINUTES = SECONDS * 60;
const HOURS = MINUTES * 60;
const DAYS = HOURS * 24;
const getData = async endpoint => fetch(
'https://api2.nicehash.com/main/api/v2/' + endpoint
).then(res => res.json());
const getNiceHashData = async () => {
const [algorithmData, currentValues] = await Promise.all([
getData('mining/algorithms'),
getData('public/stats/global/current')
]);
const algorithms = algorithmData.miningAlgorithms
.map(value => {
const algorithm = {
id: value.order,
name: value.title
};
const {marketFactor} = value;
const {displayMarketFactor} = value;
const values = currentValues.algos.find(algo => algo.a === algorithm.id);
const pricePerHashPerDay = values.p / 100000000;
algorithm.pricePerHashPerSecond = pricePerHashPerDay / DAYS;
algorithm.hashrate = values.s;
algorithm.priceReadable = (pricePerHashPerDay * marketFactor).toFixed(4) + ` BTC/${displayMarketFactor}/day`;
algorithm.hashrateReadable = (algorithm.hashrate / marketFactor).toFixed(4) + ` ${displayMarketFactor}/s`;
return algorithm;
});
return algorithms;
};
module.exports = getNiceHashData;
module.exports.handler = () => sendJson(getNiceHashData);

27
api/util/send-json.js

@ -1,27 +0,0 @@
const sendJson = async getData => {
try {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, s-max-age=600',
'Content-Type': 'application/json'
},
body: JSON.stringify(await getData())
};
} catch (error) {
return {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
},
body: JSON.stringify({
error: true,
message: error.message
})
};
}
};
module.exports = sendJson;
Loading…
Cancel
Save