opnet
Version:
The perfect library for building Bitcoin-based applications.
26 lines (25 loc) • 841 B
JavaScript
export class EpochDifficultyConverter {
static bitsToScaledDifficulty(bits) {
return 2n ** BigInt(Math.max(0, bits));
}
static formatDifficulty(difficulty) {
if (difficulty < 1000n) {
return difficulty.toString();
}
else if (difficulty < BigInt(1e6)) {
return (difficulty / BigInt(1e3)).toString() + 'K';
}
else if (difficulty < BigInt(1e9)) {
return (difficulty / BigInt(1e6)).toString() + 'M';
}
else if (difficulty < BigInt(1e12)) {
return (difficulty / BigInt(1e9)).toString() + 'G';
}
else if (difficulty < BigInt(1e15)) {
return (difficulty / BigInt(1e12)).toString() + 'T';
}
else {
return (difficulty / BigInt(1e15)).toString() + 'P';
}
}
}