-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update mainsail decimals to 18 and add unit formatters (#114)
- Loading branch information
1 parent
9c32c5a
commit 47e0251
Showing
9 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const SATOSHI = 1e8; | ||
export const SATOSHI = 1e18; | ||
|
||
export const WEI_MULTIPLIER = 1; | ||
export const GWEI_MULTIPLIER = 1e9; | ||
export const ARK_MULTIPLIER = 1e18; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { formatUnits } from "./format-units"; | ||
|
||
describe("formatUnits", async ({ assert, it, loader }) => { | ||
it("should format the value to wei", () => { | ||
assert.equal(formatUnits("1", "wei"), "1"); | ||
assert.equal(formatUnits("1000000000", "gwei"), "1"); | ||
assert.equal(formatUnits("100000000000000000000", "ark"), "1"); | ||
}); | ||
|
||
it("should throw an error for unsupported units", () => { | ||
assert.throws( | ||
() => formatUnits("1", "btc"), | ||
"Unsupported unit: btc. Supported units are 'wei', 'gwei', and 'ark'.", | ||
); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable unicorn/require-number-to-fixed-digits-argument */ | ||
import { BigNumber } from "@mainsail/kernel/distribution/utils"; | ||
|
||
import { ARK_MULTIPLIER, GWEI_MULTIPLIER, WEI_MULTIPLIER } from "../crypto/constants"; | ||
|
||
export const formatUnits = (value: string, unit = "ark"): string => { | ||
switch (unit.toLowerCase()) { | ||
case "wei": | ||
return BigNumber.make(value).dividedBy(WEI_MULTIPLIER).toFixed(); | ||
case "gwei": | ||
return BigNumber.make(value).dividedBy(GWEI_MULTIPLIER).toFixed(); | ||
case "ark": | ||
return BigNumber.make(value).dividedBy(ARK_MULTIPLIER).toFixed(); | ||
default: | ||
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { parseUnits } from "./parse-units"; | ||
|
||
describe("parseUnits", async ({ assert, it, loader }) => { | ||
it("should parse the value to wei", () => { | ||
assert.equal(parseUnits(1, "wei"), "1"); | ||
assert.equal(parseUnits(1, "gwei"), "1000000000"); | ||
assert.equal(parseUnits(1, "ark"), "100000000000000000000"); | ||
}); | ||
|
||
it("should throw an error for unsupported units", () => { | ||
assert.throws( | ||
() => parseUnits(1, "btc"), | ||
"Unsupported unit: btc. Supported units are 'wei', 'gwei', and 'ark'.", | ||
); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { BigNumber } from "@mainsail/kernel/distribution/utils"; | ||
|
||
import { ARK_MULTIPLIER, GWEI_MULTIPLIER, WEI_MULTIPLIER } from "../crypto/constants"; | ||
|
||
export const parseUnits = (value: number | string, unit = "ark"): string => { | ||
switch (unit.toLowerCase()) { | ||
case "wei": | ||
return BigNumber.make(value).times(WEI_MULTIPLIER).toString(); | ||
case "gwei": | ||
return BigNumber.make(value).times(GWEI_MULTIPLIER).toString(); | ||
case "ark": | ||
return BigNumber.make(value).times(ARK_MULTIPLIER).toString(); | ||
default: | ||
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`); | ||
} | ||
}; |
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
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
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