Created
July 21, 2021 02:47
-
-
Save ottosch/9be05475e4699fad5a575d8ac0f93dec to your computer and use it in GitHub Desktop.
Finds block with the highest 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
#!/usr/bin/env node | |
// Create a directory with the script inside, then run npm init && npm i node-bitcoin-rpc | |
// Run it with ./rewards.js or node rewards.js | |
// Also change the username and password below | |
// As of 20 Jul 2021, it is block 409008, with 316,53275103 BTC | |
// Honourable mentions: block 254642, with 225,22600217 BTC | |
// And block 157235, with 221,84649523 BTC | |
const rpc = require("node-bitcoin-rpc") | |
rpc.init("127.0.0.1", 8332, "rpc_user", "rpc_pass"); | |
rpc.setTimeout(Number(process.env.TIMEOUT) || 20000); | |
errorHeights = []; | |
async function getBestBlock() { | |
return new Promise(resolve => { | |
rpc.call("getblockchaininfo", [], (e, d) => { | |
if (d.result) { | |
resolve(d.result.headers); | |
} else { | |
console.error(e); | |
} | |
}) | |
}); | |
} | |
async function getBlockStats(height) { | |
return new Promise(resolve => { | |
rpc.call("getblockstats", [height], (e, d) => { | |
if (!d) { | |
errorHeights.push(height); | |
resolve(0); | |
} else { | |
resolve(d.result); | |
} | |
}); | |
}); | |
} | |
async function findHighestReward(cb) { | |
const initialBlock = 1; | |
var bestBlock = await getBestBlock(); | |
var highFeeBlock = { | |
height: 0, | |
reward: 0 | |
}; | |
console.log(`Best block: ${bestBlock}`); | |
console.log("=============================\n"); | |
var newBestRewardFound = false; | |
for (var i = initialBlock; i <= bestBlock; i++) { | |
var block = await getBlockStats(i); | |
var reward = block.subsidy + block.totalfee; | |
if (reward > highFeeBlock.reward) { | |
newBestRewardFound = true; | |
highFeeBlock.reward = reward; | |
highFeeBlock.height = i; | |
} | |
if (i % 50 == 0) { | |
if (newBestRewardFound) { | |
console.log(`Block ${block.height} of ${bestBlock}. New highest: ${highFeeBlock.reward} sats at height ${highFeeBlock.height}`); | |
} else { | |
console.log(`Block ${block.height} of ${bestBlock}`); | |
} | |
newBestRewardFound = false; | |
} | |
} | |
console.log("Highest reward block:"); | |
console.log(highFeeBlock); | |
var rewardStr = (highFeeBlock.reward / 100000000).toString().replace(".", ","); | |
console.log(`${formatNumber(highFeeBlock.reward)} sats`); | |
console.log(`${rewardStr} BTC`); | |
console.log("\n\nProblematic heights:"); | |
console.log(errorHeights); | |
} | |
function formatNumber(num) { | |
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.') | |
} | |
findHighestReward(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment