Skip to content

Instantly share code, notes, and snippets.

@NetOpWibby
Created April 24, 2021 21:07
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/093bda96bbfc38288323e74461e54c06 to your computer and use it in GitHub Desktop.
Save NetOpWibby/093bda96bbfc38288323e74461e54c06 to your computer and use it in GitHub Desktop.
Get the value of your Handshake names (obtained on Namebase), in HNS.
/// N A T I V E
import fs from "fs";
import https from "https";
import { join } from "path";
/// U T I L
const names = fs.readFileSync(join(process.cwd(), "names.txt"), "utf8");
const getNameValue = (suppliedName, index) => new Promise(res => {
setTimeout(async() => {
const { closeAmount } = JSON.parse(await getClosePrice(suppliedName));
const { history } = JSON.parse(await getSalePrice(suppliedName));
let value = 0;
if (history && history[0] && history[0].amount)
value = history[0].amount / 1000000;
else
value = closeAmount / 1000000;
res({ value });
/// give Namebase a break, with a throttle
}, 250 * index);
});
/// P R O G R A M
getPortfolioValue();
async function getPortfolioValue() {
const nameArray = names.split(/\r?\n/).filter(item => item);
let fileToWrite = "Handshake Portfolio Value\n\n\n\n";
let totalValue = 0;
try {
await Promise.all(nameArray.map(async(name, index) => {
if (!name || String(name).length === 0)
return;
index = index + 1; /// start count at 1, not 0
const { value } = await getNameValue(name, index);
fileToWrite += `${name}/ - ${value} HNS\n`;
totalValue += value;
console.log(`Processed name ${index} of ${nameArray.length}`);
// console.log(`TOTAL: ${totalValue.toFixed(2)}\n${name}/ - ${value} HNS\n`); /// uncomment to see name value while this runs
}));
} catch(error) {
console.log(error.toString());
console.log("<<<");
} finally {
fileToWrite += `\n\n\nTOTAL: ${totalValue.toFixed(2)} HNS\n`;
fs.writeFileSync(join(process.cwd(), "names-processed.txt"), fileToWrite, "utf-8");
console.log(">>> PROCESSING DONE, FILE WRITTEN");
return;
}
}
/// H E L P E R
async function getClosePrice(suppliedName) {
const data = await httpRequest(`https://www.namebase.io/api/domains/get/${suppliedName}`);
return data;
}
async function getSalePrice(suppliedName) {
const data = await httpRequest(`https://www.namebase.io/api/v0/marketplace/${suppliedName}/history`);
return data;
}
function httpRequest(url) {
return new Promise((resolve, reject) => {
const request = https.get(url, response => {
if (response.statusCode < 200 || response.statusCode > 299)
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
const body = [];
response.on("data", chunk => body.push(chunk));
response.on("end", () => resolve(body.join("")));
});
request.on("error", err => reject(err));
});
}
@NetOpWibby
Copy link
Author

Notes

  • This code assumes you have a file called names.txt, with each name on its own line. No slashes or anything, just the name.
  • Once processing is complete, you'll find the file names-processed.txt in your working directory.

Run

$ node names.mjs

Output

What you'll get resembles this:

Handshake Portfolio Value



0066cc/ - 0 HNS
0451/ - 100 HNS
07d0eb/ - 1 HNS
0x01/ - 2 HNS
0xebfe/ - 1 HNS
...
zenny/ - 10 HNS
zeux/ - 15 HNS
zoltan_istvan/ - 90 HNS
zwl/ - 175 HNS
zoltanistvan/ - 25 HNS



TOTAL: 54319.51 HNS

@NetOpWibby
Copy link
Author

You can use the HNS/USD converter on Nomics for the total.

@NetOpWibby
Copy link
Author

If the script fails with a message saying it couldn't connect to Namebase, change the timeout from 250 to 500.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment