Skip to content

Commit

Permalink
feat: Add STX unit helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Sep 17, 2024
1 parent 5296c94 commit 27ae2eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/transactions/src/units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const MICROSTX_IN_STX = 1_000_000;

/**
* Convert μSTX (micro-STX) to STX denomination.
* `1 STX = 1,000,000 μSTX`
*
* @example
* ```ts
* microStxToStx(1000000n); // 1n
* ```
*/
export function microStxToStx(amountInMicroStx: number): number {
return amountInMicroStx / MICROSTX_IN_STX;
}

/**
* Convert STX to μSTX (micro-STX) denomination.
* `1 STX = 1,000,000 μSTX`
*
* @example
* ```ts
* stxToMicroStx(1); // 1000000
* ```
*/
export function stxToMicroStx(amountInStx: number): number {
return amountInStx * MICROSTX_IN_STX;
}
21 changes: 21 additions & 0 deletions packages/transactions/tests/units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { microStxToStx, stxToMicroStx } from '../src/units';

test(stxToMicroStx.name, () => {
expect(stxToMicroStx(1)).toBe(1000000);
expect(stxToMicroStx(1.23)).toBe(1230000);
expect(stxToMicroStx(0.000001)).toBe(1);

expect(stxToMicroStx(-1)).toBe(-1000000);
expect(stxToMicroStx(-2.34)).toBe(-2340000);
expect(stxToMicroStx(-0.000001)).toBe(-1);
});

test(microStxToStx.name, () => {
expect(microStxToStx(1000000)).toBe(1);
expect(microStxToStx(1230000)).toBe(1.23);
expect(microStxToStx(1)).toBe(0.000001);

expect(microStxToStx(-1000000)).toBe(-1);
expect(microStxToStx(-2340000)).toBe(-2.34);
expect(microStxToStx(-1)).toBe(-0.000001);
});

0 comments on commit 27ae2eb

Please sign in to comment.