Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): improve unit name type safety and autocomplete #4942

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src.ts/_tests/test-utils-units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestCaseUnit>("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 },
Expand Down Expand Up @@ -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");
Expand Down
41 changes: 20 additions & 21 deletions src.ts/utils/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ 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
* places. The %%unit%% may be the number of decimal places or the name of
* 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");
}
Expand All @@ -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");
}
Expand Down