Skip to content

Commit 3847617

Browse files
authored
[SDK] fromGwei helper function (#2736)
1 parent 1d4fdd6 commit 3847617

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

.changeset/fast-hairs-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Adds fromGwei unit converter

packages/thirdweb/src/exports/thirdweb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export type { NFT } from "../utils/nft/parseNft.js";
147147
/**
148148
* UNITS
149149
*/
150-
export { toEther, toTokens, toUnits, toWei } from "../utils/units.js";
150+
export { toEther, toTokens, toUnits, toWei, fromGwei } from "../utils/units.js";
151151

152152
export {
153153
getBuyWithCryptoQuote,

packages/thirdweb/src/exports/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export { ensureBytecodePrefix } from "../utils/bytecode/prefix.js";
77
export { resolveImplementation } from "../utils/bytecode/resolveImplementation.js";
88

99
// units
10-
export { toEther, toTokens, toUnits, toWei } from "../utils/units.js";
10+
export { toEther, toTokens, toUnits, toWei, fromGwei } from "../utils/units.js";
1111

1212
// any-evm utils
1313
export {

packages/thirdweb/src/utils/units.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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";
33

44
describe("toTokens", () => {
55
it("converts value to number", () => {
@@ -268,3 +268,46 @@ describe("toWei", () => {
268268
);
269269
});
270270
});
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+
});

packages/thirdweb/src/utils/units.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,19 @@ export function toUnits(tokens: string, decimals: number): bigint {
130130
export function toWei(tokens: string) {
131131
return toUnits(tokens, 18);
132132
}
133+
134+
/**
135+
* Converts the specified number from gwei to wei.
136+
* @param gwei The number of gwei to convert.
137+
* @returns The converted value in wei.
138+
* @example
139+
* ```ts
140+
* import { fromGwei } from "thirdweb/utils";
141+
* fromGwei('1')
142+
* // 1000000000n
143+
* ```
144+
* @utils
145+
*/
146+
export function fromGwei(gwei: string) {
147+
return toUnits(gwei, 9);
148+
}

0 commit comments

Comments
 (0)