Skip to content

Commit

Permalink
feat: new /work/detailed endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Feb 20, 2021
1 parent 1f32432 commit 4dd187f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 deletions.
5 changes: 4 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "krist",
"version": "2.5.0",
"version": "2.6.0",
"description": "The new Krist node written in node.js.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -59,6 +59,9 @@
"GetLastBlock",
"GetBaseValue",
"GetValue",
"GetWork",
"GetWorkDay",
"GetWorkDetailed",
"SubmitBlock",
"TransactionGroup",
"GetTransactions",
Expand Down
11 changes: 10 additions & 1 deletion src/names.js
Expand Up @@ -22,8 +22,9 @@
const utils = require("./utils.js");
const constants = require("./constants.js");
const schemas = require("./schemas.js");
const database = require("./database.js");
const websockets = require("./websockets.js");
const { Op } = require("sequelize");
const { Op, QueryTypes } = require("sequelize");

const promClient = require("prom-client");
const promNamesPurchasedCounter = new promClient.Counter({
Expand All @@ -50,6 +51,14 @@ Names.lookupNames = function(addressList, limit, offset, orderBy, order) {
});
};

Names.getDetailedUnpaid = function() {
return database.getSequelize().query(`
SELECT COUNT(*) AS \`count\`, \`unpaid\` FROM \`names\`
GROUP BY \`unpaid\`
ORDER BY \`unpaid\` ASC;
`, { type: QueryTypes.SELECT });
};

Names.getNameCountByAddress = function(address) {
return schemas.name.count({where: {owner: address}});
};
Expand Down
2 changes: 1 addition & 1 deletion src/routes/lookup.js
Expand Up @@ -133,7 +133,7 @@ module.exports = function(app) {
* @apiParam (URLParameter) {String[]} [addresses] A comma-separated list of
* addresses to filter transactions to/from.
*
* @apiParam (QueryParameter) {String[]} fetchNames When supplied, fetch the
* @apiParam (QueryParameter) {Boolean} fetchNames When supplied, fetch the
* count of owned names for each address.
*
* @apiSuccess {Number} found The amount of addresses that were successfully
Expand Down
112 changes: 88 additions & 24 deletions src/routes/work.js
Expand Up @@ -20,6 +20,8 @@
*/

const krist = require("./../krist.js");
const blocks = require("./../blocks.js");
const names = require("./../names.js");

module.exports = function(app) {
app.get("/", async function(req, res, next) {
Expand All @@ -31,19 +33,19 @@ module.exports = function(app) {
});

/**
* @api {get} /work Get the current work
* @apiName GetWork
* @apiGroup MiscellaneousGroup
* @apiVersion 2.0.5
*
* @apiSuccess {Number} work The current Krist work (difficulty)
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "work": 18750
* @api {get} /work Get the current work
* @apiName GetWork
* @apiGroup MiscellaneousGroup
* @apiVersion 2.0.5
*
* @apiSuccess {Number} work The current Krist work (difficulty)
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "work": 18750
* }
*/
*/
app.get("/work", async function(req, res) {
res.json({
ok: true,
Expand All @@ -52,25 +54,87 @@ module.exports = function(app) {
});

/**
* @api {get} /work/day Get the work over the past 24 hours
* @apiName GetWorkDay
* @apiGroup MiscellaneousGroup
* @apiVersion 2.0.5
*
* @apiSuccess {Number[]} work The work every minute for the past 24 hours, starting with 24 hours ago.
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "work": [18750, 19250, ...]
* @api {get} /work/day Get the work over the past 24 hours
* @apiName GetWorkDay
* @apiGroup MiscellaneousGroup
* @apiVersion 2.0.5
*
* @apiSuccess {Number[]} work The work every minute for the past 24 hours, starting with 24 hours ago.
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "work": [18750, 19250, ...]
* }
*/
*/
app.get("/work/day", async function(req, res) {
res.json({
ok: true,
work: await krist.getWorkOverTime()
});
});

/**
* @api {get} /work/detailed Get detailed work and block value information
* @apiName GetWorkDetailed
* @apiGroup MiscellaneousGroup
* @apiVersion 2.6.0
*
* @apiSuccess {Number} work The current Krist work (difficulty)
* @apiSuccess {Number} unpaid The current number of unpaid names
*
* @apiSuccess {Number} base_value The base block value
* @apiSuccess {Number} block_value The current block value (base + unpaid)
*
* @apiSuccess {Object} decrease Information about the next block value
* decrease
* @apiSuccess {Number} decrease[value] How much Krist the block value will
* decrease by when the next name(s) expire
* @apiSuccess {Number} decrease[blocks] How many blocks before the next block
* value decrease
* @apiSuccess {Number} decrease[reset] How many blocks before the block value
* will completely reset to the base value
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "work": 92861,
* "unpaid": 3,
* "base_value": 1,
* "block_value": 4,
* "decrease": {
* "value": 2,
* "blocks": 496,
* "reset": 500
* }
* }
*/
app.get("/work/detailed", async function (req, res) {
const lastBlock = await blocks.getLastBlock();
const unpaidNames = await names.getUnpaidNameCount();
const baseValue = blocks.getBaseBlockValue(lastBlock.id);

const detailedUnpaid = await names.getDetailedUnpaid();
const nextUnpaid = detailedUnpaid.find(u => u.unpaid > 0);
const mostUnpaid = [...(detailedUnpaid.filter(u => u.unpaid > 0))];
mostUnpaid.sort((a, b) => b.unpaid - a.unpaid);

res.json({
ok: true,

work: await krist.getWork(),
unpaid: unpaidNames,

base_value: baseValue,
block_value: baseValue + unpaidNames,

decrease: {
value: nextUnpaid ? nextUnpaid.count : 0,
blocks: nextUnpaid ? nextUnpaid.unpaid : 0,
reset: mostUnpaid && mostUnpaid.length > 0 ? mostUnpaid[0].unpaid : 0
}
});
});

return app;
};

0 comments on commit 4dd187f

Please sign in to comment.