|
| 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 | + ``` |
0 commit comments