diff --git a/src.ts/_tests/test-utils-units.ts b/src.ts/_tests/test-utils-units.ts index d5089a45a1..bc601a6f6e 100644 --- a/src.ts/_tests/test-utils-units.ts +++ b/src.ts/_tests/test-utils-units.ts @@ -5,12 +5,13 @@ import { loadTests } from "./utils.js"; import { formatEther, formatUnits, parseEther, parseUnits } from "../index.js"; import type { TestCaseUnit } from "./types.js"; +import { UnitNameType } from "../utils/units.js"; describe("Tests unit conversion", function() { const tests = loadTests("units"); - const units = [ + const units:{unit:UnitNameType,format:string,decimals:number}[] = [ { unit: "ether", format: "ether_format", decimals: 18 }, { unit: "kwei", format: "kwei_format", decimals: 3 }, { unit: "mwei", format: "mwei_format", decimals: 6 }, @@ -65,6 +66,7 @@ describe("Tests bad unit conversion", function() { it("correctly fails to convert unknown unit", function() { assert.throws(() => { + // @ts-ignore parseUnits("3", "foobar"); }, (error: any) => { return error.message.startsWith("invalid unit"); diff --git a/src.ts/utils/units.ts b/src.ts/utils/units.ts index feb9fba568..94fd2fe327 100644 --- a/src.ts/utils/units.ts +++ b/src.ts/utils/units.ts @@ -25,16 +25,17 @@ import { getNumber } from "./maths.js"; import type { BigNumberish, Numeric } from "../utils/index.js"; - -const names = [ - "wei", - "kwei", - "mwei", - "gwei", - "szabo", - "finney", - "ether", -]; +const unitNames = { + 'wei': 0, + 'kwei': 3, + 'mwei': 6, + 'gwei': 9, + 'szabo': 12, + 'finney': 15, + 'ether': 18, +} as const; +export type UnitNameType = keyof typeof unitNames; +export type UnitType = UnitNameType | Numeric; /** * Converts %%value%% into a //decimal string//, assuming %%unit%% decimal @@ -42,12 +43,11 @@ const names = [ * a unit (e.g. ``"gwei"`` for 9 decimal places). * */ -export function formatUnits(value: BigNumberish, unit?: string | Numeric): string { +export function formatUnits(value: BigNumberish, unit?: UnitType): string { let decimals = 18; - if (typeof(unit) === "string") { - const index = names.indexOf(unit); - assertArgument(index >= 0, "invalid unit", "unit", unit); - decimals = 3 * index; + if (typeof (unit) === "string") { + assertArgument(unitNames[unit] >= 0, "invalid unit", "unit", unit); + decimals = unitNames[unit]; } else if (unit != null) { decimals = getNumber(unit, "unit"); } @@ -60,14 +60,13 @@ export function formatUnits(value: BigNumberish, unit?: string | Numeric): strin * %%unit%% decimal places. The %%unit%% may the number of decimal places * or the name of a unit (e.g. ``"gwei"`` for 9 decimal places). */ -export function parseUnits(value: string, unit?: string | Numeric): bigint { - assertArgument(typeof(value) === "string", "value must be a string", "value", value); +export function parseUnits(value: string, unit?: UnitType): bigint { + assertArgument(typeof (value) === "string", "value must be a string", "value", value); let decimals = 18; - if (typeof(unit) === "string") { - const index = names.indexOf(unit); - assertArgument(index >= 0, "invalid unit", "unit", unit); - decimals = 3 * index; + if (typeof (unit) === "string") { + assertArgument(unitNames[unit] >= 0, "invalid unit", "unit", unit); + decimals = unitNames[unit]; } else if (unit != null) { decimals = getNumber(unit, "unit"); }