Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit c46c975

Browse files
author
Dan Forbes
committed
Document Formatting
1 parent f351e00 commit c46c975

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
sidebar_position: 3
3+
sidebar_label: Return Formats
4+
---
5+
6+
# Return Formats
7+
8+
By default, Web3.js formats byte values as hexadecimal strings (e.g. `"0x221`") and number values as [`BigInt`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). The default formats can be configured at the global level by updating the [`defaultReturnFormat` configuration option](/guides/web3_config/#defaultreturnformat). Many Web3.js functions (e.g. [`getBlock`](/api/web3-eth/function/getBlock), [`sendTransaction`](/api/web3-eth/function/sendTransaction)) accept an optional parameter named `returnFormat` of the [type `DataFormat`](/api/web3-types#DataFormat) that can be used to configure the format for data returned by that single function invocation.
9+
10+
The following example demonstrates working with return formats:
11+
12+
```js
13+
import { FMT_BYTES, FMT_NUMBER, Web3 } from "web3";
14+
15+
const web3 = new Web3("https://eth.llamarpc.com");
16+
17+
// use the default return format
18+
web3.eth.getBlock().then((block) => {
19+
console.log(`Block #${block.number} [Hash: ${block.hash}]`);
20+
});
21+
// ↳ Block #20614588 [Hash: 0x86784297b538a8bb2a13c9a0d808c01816fc952170609d28832a2eb6fa32c918]
22+
23+
// specify the return format for a single function invocation
24+
web3.eth
25+
.getBlock(undefined, undefined, {
26+
bytes: FMT_BYTES.UINT8ARRAY,
27+
number: FMT_NUMBER.HEX,
28+
})
29+
.then((block) => {
30+
console.log(`Block #${block.number} [Hash: ${block.hash}]`);
31+
});
32+
// ↳ Block #0x13a8dbc [Hash: 134,120,66,151,...,250,50,201,24]
33+
34+
// configure default return format for the web3-eth package
35+
web3.eth.defaultReturnFormat = {
36+
bytes: FMT_BYTES.UINT8ARRAY,
37+
number: FMT_NUMBER.HEX,
38+
};
39+
40+
web3.eth.getBlock().then((block) => {
41+
console.log(`Block #${block.number} [Hash: ${block.hash}]`);
42+
});
43+
// ↳ Block #0x13a8dbc [Hash: 134,120,66,151,...,250,50,201,24]
44+
```
45+
46+
The supported return formats are:
47+
48+
- Bytes
49+
- [`FMT_BYTES.HEX`](/api/web3-types/enum/FMT_BYTES#HEX): hexadecimal [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"0xdd"`)
50+
- [`FMT_BYTES.UINT8ARRAY`](/api/web3-types/enum/FMT_BYTES#UINT8ARRAY): [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) (e.g. `[ 2, 33 ]`)
51+
- Numbers
52+
- [`FMT_NUMBER.BIGINT`](/api/web3-types/enum/FMT_NUMBER#BIGINT): [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) (e.g. `221n`)
53+
- [`FMT_NUMBER.HEX`](/api/web3-types/enum/FMT_NUMBER#HEX): hexadecimal [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"0xdd"`)
54+
- [`FMT_NUMBER.NUMBER`](/api/web3-types/enum/FMT_NUMBER#NUMBER): [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) (e.g. `221`)
55+
- [`FMT_NUMBER.STR`](/api/web3-types/enum/FMT_NUMBER#STR): [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"221"`)

docs/docs/guides/web3_utils_module/mastering_web3-utils.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,70 @@ console.log(web3.utils.compareBlockNumbers(2, 2));
240240
// 0
241241
```
242242

243+
### Formatting
244+
245+
The [`format` function](/api/web3-utils/function/format) in the `web3-utils` package is used to convert data between equivalent formats. For example, bytes that are represented as a [`Uint8Array` type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) can be formatted as a hexademical string (e.g. `"0xdd"`) or primitive JavaScript [`Number` types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) can be formatted as [`BigInt` types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). The `format` function expects two required parameters, `schema` and `data`, and accepts a third optional parameter, `returnFormat`. The `schema` parameter is used to describe how the data should be interpreted. The `data` parameter represents the data that is to be formatted. The [`returnFormat` parameter](#return-formats) specifies how the data should be formatted.
246+
247+
Here are some example that demonstrate the use of the `format` function:
248+
249+
```js
250+
import { format } from "web3-utils";
251+
import { FMT_BYTES, FMT_NUMBER } from "web3-types";
252+
253+
// format a primitive number as a hexidecimal string
254+
console.log(format({ format: "uint" }, 221, { number: FMT_NUMBER.HEX }));
255+
// ↳ 0xdd
256+
257+
// format a primitive number as a BigInt
258+
console.log(format({ format: "uint" }, 221, { number: FMT_NUMBER.BIGINT }));
259+
// ↳ 221n
260+
261+
// format a stringified number as a hexidecimal string
262+
console.log(format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }));
263+
// ↳ 0xdd
264+
265+
// format a Uint8Array of bytes as a hexidecimal string
266+
console.log(
267+
format({ format: "bytes" }, new Uint8Array([2, 33]), {
268+
bytes: FMT_BYTES.HEX,
269+
}),
270+
);
271+
// ↳ 0x0221
272+
273+
// format an array of values
274+
console.log(
275+
format({ type: "array", items: { format: "uint" } }, ["221", 1983], {
276+
number: FMT_NUMBER.HEX,
277+
}),
278+
);
279+
// ↳ [ '0xdd', '0x7bf' ]
280+
281+
// format an object with multiple properties
282+
console.log(
283+
format(
284+
{
285+
type: "object",
286+
properties: {
287+
aNumber: { format: "uint" },
288+
someBytes: { format: "bytes" },
289+
},
290+
},
291+
{ aNumber: "221", someBytes: new Uint8Array([2, 33]) },
292+
{ bytes: FMT_BYTES.UINT8ARRAY, number: FMT_NUMBER.HEX },
293+
),
294+
);
295+
// ↳ { aNumber: '0xdd', someBytes: Uint8Array(2) [ 2, 33 ] }
296+
```
297+
298+
#### Return Formats
299+
300+
The following return formats are supported:
301+
302+
- Bytes
303+
- [`FMT_BYTES.HEX`](/api/web3-types/enum/FMT_BYTES#HEX): hexadecimal [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"0xdd"`)
304+
- [`FMT_BYTES.UINT8ARRAY`](/api/web3-types/enum/FMT_BYTES#UINT8ARRAY): [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) (e.g. `[ 2, 33 ]`)
305+
- Numbers
306+
- [`FMT_NUMBER.BIGINT`](/api/web3-types/enum/FMT_NUMBER#BIGINT): [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) (e.g. `221n`)
307+
- [`FMT_NUMBER.HEX`](/api/web3-types/enum/FMT_NUMBER#HEX): hexadecimal [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"0xdd"`)
308+
- [`FMT_NUMBER.NUMBER`](/api/web3-types/enum/FMT_NUMBER#NUMBER): [`Number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) (e.g. `221`)
309+
- [`FMT_NUMBER.STR`](/api/web3-types/enum/FMT_NUMBER#STR): [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (e.g. `"221"`)

packages/web3-types/src/data_format_types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export type ByteTypes = {
4141
[FMT_BYTES.UINT8ARRAY]: Uint8Array;
4242
};
4343

44+
/**
45+
* Used to specify how data should be formatted. Bytes can be formatted as hexadecimal strings or
46+
* Uint8Arrays. Numbers can be formatted as BigInts, hexadecimal strings, primitive numbers, or
47+
* strings.
48+
*/
4449
export type DataFormat = {
4550
readonly number: FMT_NUMBER;
4651
readonly bytes: FMT_BYTES;

packages/web3-utils/src/formatter.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,27 @@ export const convert = (
350350
return object;
351351
};
352352

353+
/**
354+
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted
355+
* according to the provided return format.
356+
*
357+
* @param schema - how to interpret the data
358+
* @param data - data to be formatted
359+
* @param returnFormat - how to format the data
360+
* @returns - formatted data
361+
*
362+
* @example
363+
*
364+
* ```js
365+
* import { FMT_NUMBER, utils } from "web3";
366+
*
367+
* console.log(
368+
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }),
369+
* );
370+
* // 0xdd
371+
* ```
372+
*
373+
*/
353374
export const format = <
354375
DataType extends Record<string, unknown> | unknown[] | unknown,
355376
ReturnType extends DataFormat,

0 commit comments

Comments
 (0)