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

Commit 05b7ec6

Browse files
Merge branch '4.x' into nicolasbrugneaux/customTransactionSchema
2 parents 125bf12 + b3cb1b7 commit 05b7ec6

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
```ts
13+
import { Block, FMT_BYTES, FMT_NUMBER, Numbers, 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: Block) => {
19+
console.log(`Block #${block.number} Hash: ${block.hash}`);
20+
});
21+
// ↳ Block #20735255 Hash: 0xbaea6dbd46fa810a27be4c9eac782602f8efe7512fb30a8455c127b101a23e22
22+
23+
// specify the return format for a single function invocation
24+
web3.eth
25+
.getBlockNumber({
26+
bytes: FMT_BYTES.HEX,
27+
number: FMT_NUMBER.HEX,
28+
})
29+
.then((blockNumber: Numbers) => {
30+
console.log(`Block #${blockNumber}`);
31+
});
32+
// ↳ Block #0x13c6517
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: Block) => {
41+
console.log(`Block #${block.number} Hash: [${block.hash}]`);
42+
});
43+
// ↳ Block #0x13c6517 Hash: [186,234,109,...,162,62,34]
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+
```ts
51+
web3.eth
52+
.getBlock(undefined, undefined, {
53+
bytes: FMT_BYTES.HEX,
54+
number: FMT_NUMBER.BIGINT,
55+
})
56+
.then((block: Block) => {
57+
console.log(`Block hash: ${block.hash}`);
58+
});
59+
// ↳ Block hash: 0xbaea6dbd46fa810a27be4c9eac782602f8efe7512fb30a8455c127b101a23e22
60+
```
61+
- [`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 ]`)
62+
```ts
63+
web3.eth
64+
.getBlock(undefined, undefined, {
65+
bytes: FMT_BYTES.UINT8ARRAY,
66+
number: FMT_NUMBER.BIGINT,
67+
})
68+
.then((block: Block) => {
69+
console.log(`Block hash: [${block.hash}]`);
70+
});
71+
// ↳ Block hash: [186,234,109,...,162,62,34]
72+
```
73+
- Numbers
74+
- [`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`)
75+
```ts
76+
web3.eth
77+
.getBlockNumber({
78+
bytes: FMT_BYTES.HEX,
79+
number: FMT_NUMBER.BIGINT,
80+
})
81+
.then((blockNumber: Numbers) => {
82+
console.log(`Block #${blockNumber}`);
83+
});
84+
// ↳ Block #20735255
85+
```
86+
- [`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"`)
87+
```ts
88+
web3.eth
89+
.getBlockNumber({
90+
bytes: FMT_BYTES.HEX,
91+
number: FMT_NUMBER.HEX,
92+
})
93+
.then((blockNumber: Numbers) => {
94+
console.log(`Block #${blockNumber}`);
95+
});
96+
// ↳ Block #0x13c6517
97+
```
98+
- [`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`)
99+
```ts
100+
web3.eth
101+
.getBlockNumber({
102+
bytes: FMT_BYTES.HEX,
103+
number: FMT_NUMBER.NUMBER,
104+
})
105+
.then((blockNumber: Numbers) => {
106+
console.log(`Block #${blockNumber}`);
107+
});
108+
// ↳ Block #20735255
109+
```
110+
- [`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"`)
111+
```ts
112+
web3.eth
113+
.getBlockNumber({
114+
bytes: FMT_BYTES.HEX,
115+
number: FMT_NUMBER.STR,
116+
})
117+
.then((blockNumber: Numbers) => {
118+
console.log(`Block #${blockNumber}`);
119+
});
120+
// ↳ Block #20735255
121+
```

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
@@ -349,6 +349,27 @@ export const convert = (
349349
return object;
350350
};
351351

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

0 commit comments

Comments
 (0)