Created
April 24, 2021 21:07
-
-
Save NetOpWibby/093bda96bbfc38288323e74461e54c06 to your computer and use it in GitHub Desktop.
Get the value of your Handshake names (obtained on Namebase), in HNS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the script fails with a message saying it couldn't connect to Namebase, change the timeout from
250
to500
.