ethers.js/lib.commonjs/_tests/test-utils-units.js

66 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2022-11-09 02:57:02 -05:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2023-04-25 20:04:48 +09:00
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
2022-11-09 02:57:02 -05:00
const utils_js_1 = require("./utils.js");
const index_js_1 = require("../index.js");
describe("Tests unit conversion", function () {
const tests = (0, utils_js_1.loadTests)("units");
const units = [
{ unit: "ether", format: "ether_format", decimals: 18 },
{ unit: "kwei", format: "kwei_format", decimals: 3 },
{ unit: "mwei", format: "mwei_format", decimals: 6 },
{ unit: "gwei", format: "gwei_format", decimals: 9 },
{ unit: "szabo", format: "szabo_format", decimals: 12 },
{ unit: "finney", format: "finney_format", decimals: 15 },
];
for (const { unit, format, decimals } of units) {
for (const test of tests) {
const str = (test[format]);
if (str == null) {
continue;
}
it(`converts wei to ${unit} string: ${test.name}`, function () {
const wei = BigInt(test.wei);
if (decimals === 18) {
assert_1.default.equal((0, index_js_1.formatEther)(wei), str, "formatEther");
assert_1.default.equal((0, index_js_1.formatUnits)(wei), str, "formatUnits");
}
assert_1.default.equal((0, index_js_1.formatUnits)(wei, unit), str, `formatUnits(${unit})`);
assert_1.default.equal((0, index_js_1.formatUnits)(wei, decimals), str, `formatUnits(${decimals})`);
});
}
for (const test of tests) {
const str = (test[format]);
if (str == null) {
continue;
}
it(`converts ${format} string to wei: ${test.name}`, function () {
const wei = BigInt(test.wei);
if (decimals === 18) {
assert_1.default.equal((0, index_js_1.parseEther)(str), wei, "parseEther");
assert_1.default.equal((0, index_js_1.parseUnits)(str), wei, "parseUnits");
}
assert_1.default.equal((0, index_js_1.parseUnits)(str, unit), wei, `parseUnits(${unit})`);
assert_1.default.equal((0, index_js_1.parseUnits)(str, decimals), wei, `parseUnits(${decimals})`);
});
}
}
});
describe("Tests bad unit conversion", function () {
2022-11-30 15:44:23 -05:00
it("correctly fails to convert non-string value", function () {
2022-11-09 02:57:02 -05:00
assert_1.default.throws(() => {
(0, index_js_1.parseUnits)(3, "ether");
}, (error) => {
return error.message.startsWith("value must be a string");
});
});
2022-11-30 15:44:23 -05:00
it("correctly fails to convert unknown unit", function () {
2022-11-09 02:57:02 -05:00
assert_1.default.throws(() => {
(0, index_js_1.parseUnits)("3", "foobar");
}, (error) => {
return error.message.startsWith("invalid unit");
});
});
});
//# sourceMappingURL=test-utils-units.js.map