Skip to content

Instantly share code, notes, and snippets.

@NetOpWibby
Forked from pinheadmz/expiring-plugin.js
Created February 17, 2022 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetOpWibby/f3eb5793bb3bb2eed177d1f1afb19325 to your computer and use it in GitHub Desktop.
Save NetOpWibby/f3eb5793bb3bb2eed177d1f1afb19325 to your computer and use it in GitHub Desktop.
// USAGE: hsd --log-console=false --plugins=/path/to/this/file/expiring-plug.js
'use strict';
const {
Namestate,
Network
} = require('hsd');
const plugin = exports;
const network = Network.get('main');
class Plugin {
constructor(node) {
this.chain = node.chain;
this.tree = node.chain.db.tree;
this.map = {};
this.chain.on('tip', async (entry) => {
await this.run(entry.height);
});
}
async run(height) {
const start = Date.now();
const out = {};
let total = 0;
const iter = this.tree.iterator();
while (await iter.next()) {
total++;
const {value} = iter;
const ns = Namestate.decode(value);
// Skip reserved
if (ns.claimed)
continue;
// Aution is not yet finished
if (!ns.isClosed(height, network))
continue;
// No reveals, no winner, no owner
if (ns.owner.isNull())
continue;
const {renewalPeriodEnd} = ns.toStats(
this.chain.height,
this.chain.network
);
const keys = Object.keys(out);
if (keys.length >= 20) {
if (renewalPeriodEnd >= keys[keys.length - 1])
continue;
else
delete out[keys[keys.length -1]];
}
if (!out[renewalPeriodEnd])
out[renewalPeriodEnd] = [ns.name.toString('ascii')];
else
out[renewalPeriodEnd].push(ns.name.toString('ascii'));
console.log('\n\n\n');
console.log(out);
console.log(total);
}
console.log('DONE');
console.log(`elapsed: ${Date.now() - start}`);
}
}
plugin.id = 'expiring';
plugin.init = function init(node) {
return new Plugin(node);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment