Skip to content

Commit

Permalink
fix: use bignumber.js for sat percentiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed May 5, 2023
1 parent b70e601 commit 8914e27
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@fastify/type-provider-typebox": "^2.1.0",
"@sinclair/typebox": "^0.24.20",
"@types/node": "^18.13.0",
"bignumber.js": "^9.1.1",
"bitcoinjs-lib": "^6.1.0",
"env-schema": "^5.2.0",
"fastify": "^4.3.0",
Expand Down
9 changes: 8 additions & 1 deletion src/api/util/ordinal-satoshi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BigNumber from 'bignumber.js';

const HALVING_BLOCKS = 210_000;
const DIFFICULTY_ADJUST_BLOCKS = 2016;
const INITIAL_SUBSIDY = 50;
Expand All @@ -13,6 +15,10 @@ export enum SatoshiRarity {
mythic = 'mythic',
}

/**
* Ordinal Satoshi calculator. Mostly translated from the original Rust implementation at
* https://github.com/casey/ord/blob/master/src/sat.rs
*/
export class OrdinalSatoshi {
public blockHeight: number;
public cycle: number;
Expand Down Expand Up @@ -75,7 +81,8 @@ export class OrdinalSatoshi {
}

public get percentile(): string {
return `${(this.ordinal / (SAT_SUPPLY - 1)) * 100.0}%`;
const percentile = new BigNumber((this.ordinal / (SAT_SUPPLY - 1)) * 100.0);
return `${percentile.toFixed()}%`;
}

public get rarity(): SatoshiRarity {
Expand Down
3 changes: 1 addition & 2 deletions tests/ordinal-satoshi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ describe('OrdinalSatoshi', () => {
expect(sat.epoch).toBe(0);
expect(sat.name).toBe('nvtdijuwxdx');
expect(sat.offset).toBe(200);
// TODO: Convert scientific notation to number.
// expect(sat.percentile).toBe('0.000000000009523809534285719%');
expect(sat.percentile).toBe('0.000000000009523809534285719%');
expect(sat.period).toBe(0);
expect(sat.blockHeight).toBe(0);
});
Expand Down

0 comments on commit 8914e27

Please sign in to comment.