|
1 | 1 | import { describe, expect, it } from "vitest";
|
2 |
| -import { toEther, toTokens, toUnits, toWei } from "./units.js"; |
| 2 | +import { fromGwei, toEther, toTokens, toUnits, toWei } from "./units.js"; |
3 | 3 |
|
4 | 4 | describe("toTokens", () => {
|
5 | 5 | it("converts value to number", () => {
|
@@ -268,3 +268,46 @@ describe("toWei", () => {
|
268 | 268 | );
|
269 | 269 | });
|
270 | 270 | });
|
| 271 | + |
| 272 | +describe("fromGwei", () => { |
| 273 | + it("converts gwei to wei correctly", () => { |
| 274 | + expect(fromGwei("1")).toMatchInlineSnapshot("1000000000n"); |
| 275 | + expect(fromGwei("10")).toMatchInlineSnapshot("10000000000n"); |
| 276 | + expect(fromGwei("100")).toMatchInlineSnapshot("100000000000n"); |
| 277 | + expect(fromGwei("1000")).toMatchInlineSnapshot("1000000000000n"); |
| 278 | + expect(fromGwei("12345")).toMatchInlineSnapshot("12345000000000n"); |
| 279 | + expect(fromGwei("0")).toMatchInlineSnapshot("0n"); |
| 280 | + expect(fromGwei("-1")).toMatchInlineSnapshot("-1000000000n"); |
| 281 | + expect(fromGwei("-10")).toMatchInlineSnapshot("-10000000000n"); |
| 282 | + expect(fromGwei("-100")).toMatchInlineSnapshot("-100000000000n"); |
| 283 | + expect(fromGwei("-1000")).toMatchInlineSnapshot("-1000000000000n"); |
| 284 | + expect(fromGwei("-12345")).toMatchInlineSnapshot("-12345000000000n"); |
| 285 | + }); |
| 286 | + |
| 287 | + it("handles fractional gwei inputs", () => { |
| 288 | + expect(fromGwei("1.2345")).toMatchInlineSnapshot("1234500000n"); |
| 289 | + expect(fromGwei("10.6789")).toMatchInlineSnapshot("10678900000n"); |
| 290 | + expect(fromGwei("0.0001")).toMatchInlineSnapshot("100000n"); |
| 291 | + expect(fromGwei("0.9999")).toMatchInlineSnapshot("999900000n"); |
| 292 | + expect(fromGwei("-1.2345")).toMatchInlineSnapshot("-1234500000n"); |
| 293 | + expect(fromGwei("-10.6789")).toMatchInlineSnapshot("-10678900000n"); |
| 294 | + expect(fromGwei("-0.0001")).toMatchInlineSnapshot("-100000n"); |
| 295 | + expect(fromGwei("-0.9999")).toMatchInlineSnapshot("-999900000n"); |
| 296 | + }); |
| 297 | + |
| 298 | + // I'm not sure there is any case that gwei would be fractional, but just in case someone tries it |
| 299 | + it("rounds fractional gwei inputs correctly and trims excessive decimals", () => { |
| 300 | + expect(fromGwei("1.000000000000008")).toMatchInlineSnapshot("1000000000n"); |
| 301 | + expect(fromGwei("1.000000000000004")).toMatchInlineSnapshot("1000000000n"); |
| 302 | + expect(fromGwei("0.0000000000000001")).toMatchInlineSnapshot("0n"); |
| 303 | + expect(fromGwei("0.0000000000000009")).toMatchInlineSnapshot("0n"); |
| 304 | + expect(fromGwei("-1.000000000000008")).toMatchInlineSnapshot( |
| 305 | + "-1000000000n", |
| 306 | + ); |
| 307 | + expect(fromGwei("-1.000000000000004")).toMatchInlineSnapshot( |
| 308 | + "-1000000000n", |
| 309 | + ); |
| 310 | + expect(fromGwei("-0.0000000000000001")).toMatchInlineSnapshot("0n"); |
| 311 | + expect(fromGwei("-0.0000000000000009")).toMatchInlineSnapshot("0n"); |
| 312 | + }); |
| 313 | +}); |
0 commit comments