Last active
August 5, 2020 20:33
-
-
Save rphansen91/176999826b03b7503bd8b08828ddbf4f to your computer and use it in GitHub Desktop.
Calculate estimated time until next NRG Masternode reward
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
const nodeApi = "https://nodeapi.energi.network/"; | |
const owner = "YOUR_MASTERNODE_OWNER_ADDRESS" | |
estimateBlocksTilNextMasternodeReward(owner) | |
.then(({ estimatedTimestamp }) => console.log(`Next reward at ${new Date(estimatedTimestamp).toLocaleString()}`)) | |
function listMasternodes () { | |
return rpc(nodeApi, "masternode_listMasternodes"); | |
} | |
async function estimateBlocksTilNextMasternodeReward( | |
owner | |
) { | |
// Load current list of masternodes | |
const { result } = await listMasternodes(); | |
// Find the current masternode rank | |
const rank = result.findIndex(({ Owner }) => Owner === owner); | |
const masternode = result[rank] | |
// If masternode not found throw error | |
if (!masternode) throw new Error(`Masternode Owner "${owner}" not found`) | |
// Create list of masternodes in queue ahead of us | |
const precedingResults = result.slice(0, rank); | |
// Convert binary representation to JS number array | |
const precedingAmounts = precedingResults.map( | |
({ Collateral }) => parseInt(Collateral) / 1e18 | |
); | |
// Calculate sum of collateral | |
const precedingCollateral = precedingAmounts.reduce((acc, c) => acc + c, 0); | |
// Estimated blocks until next reward | |
// Each block the queue receives 9.14 NRG per 10000 Collateral | |
const blocks = Math.floor(precedingCollateral / 10000); | |
// Estimated timestamp of next reward | |
const estimatedTimestamp = Date.now() + (blocks * 1000); | |
return { rank, blocks, estimatedTimestamp } | |
} | |
async function rpc( | |
url = "", | |
method = "", | |
params =[], | |
id = 15 | |
) { | |
return fetch(url, { | |
method: "POST", | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify({ jsonrpc: "2.0", id: 15, method, params }), | |
}).then((v) => v.json()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment