-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Document Formatting * Add More Example with `getBlockNumber`
- Loading branch information
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_label: Return Formats | ||
--- | ||
|
||
# Return Formats | ||
|
||
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. | ||
|
||
The following example demonstrates working with return formats: | ||
|
||
```ts | ||
import { Block, FMT_BYTES, FMT_NUMBER, Numbers, Web3 } from "web3"; | ||
|
||
const web3 = new Web3("https://eth.llamarpc.com"); | ||
|
||
// use the default return format | ||
web3.eth.getBlock().then((block: Block) => { | ||
console.log(`Block #${block.number} Hash: ${block.hash}`); | ||
}); | ||
// ↳ Block #20735255 Hash: 0xbaea6dbd46fa810a27be4c9eac782602f8efe7512fb30a8455c127b101a23e22 | ||
|
||
// specify the return format for a single function invocation | ||
web3.eth | ||
.getBlockNumber({ | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.HEX, | ||
}) | ||
.then((blockNumber: Numbers) => { | ||
console.log(`Block #${blockNumber}`); | ||
}); | ||
// ↳ Block #0x13c6517 | ||
|
||
// configure default return format for the web3-eth package | ||
web3.eth.defaultReturnFormat = { | ||
bytes: FMT_BYTES.UINT8ARRAY, | ||
number: FMT_NUMBER.HEX, | ||
}; | ||
|
||
web3.eth.getBlock().then((block: Block) => { | ||
console.log(`Block #${block.number} Hash: [${block.hash}]`); | ||
}); | ||
// ↳ Block #0x13c6517 Hash: [186,234,109,...,162,62,34] | ||
``` | ||
|
||
The supported return formats are: | ||
|
||
- Bytes | ||
- [`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"`) | ||
```ts | ||
web3.eth | ||
.getBlock(undefined, undefined, { | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.BIGINT, | ||
}) | ||
.then((block: Block) => { | ||
console.log(`Block hash: ${block.hash}`); | ||
}); | ||
// ↳ Block hash: 0xbaea6dbd46fa810a27be4c9eac782602f8efe7512fb30a8455c127b101a23e22 | ||
``` | ||
- [`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 ]`) | ||
```ts | ||
web3.eth | ||
.getBlock(undefined, undefined, { | ||
bytes: FMT_BYTES.UINT8ARRAY, | ||
number: FMT_NUMBER.BIGINT, | ||
}) | ||
.then((block: Block) => { | ||
console.log(`Block hash: [${block.hash}]`); | ||
}); | ||
// ↳ Block hash: [186,234,109,...,162,62,34] | ||
``` | ||
- Numbers | ||
- [`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`) | ||
```ts | ||
web3.eth | ||
.getBlockNumber({ | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.BIGINT, | ||
}) | ||
.then((blockNumber: Numbers) => { | ||
console.log(`Block #${blockNumber}`); | ||
}); | ||
// ↳ Block #20735255 | ||
``` | ||
- [`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"`) | ||
```ts | ||
web3.eth | ||
.getBlockNumber({ | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.HEX, | ||
}) | ||
.then((blockNumber: Numbers) => { | ||
console.log(`Block #${blockNumber}`); | ||
}); | ||
// ↳ Block #0x13c6517 | ||
``` | ||
- [`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`) | ||
```ts | ||
web3.eth | ||
.getBlockNumber({ | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.NUMBER, | ||
}) | ||
.then((blockNumber: Numbers) => { | ||
console.log(`Block #${blockNumber}`); | ||
}); | ||
// ↳ Block #20735255 | ||
``` | ||
- [`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"`) | ||
```ts | ||
web3.eth | ||
.getBlockNumber({ | ||
bytes: FMT_BYTES.HEX, | ||
number: FMT_NUMBER.STR, | ||
}) | ||
.then((blockNumber: Numbers) => { | ||
console.log(`Block #${blockNumber}`); | ||
}); | ||
// ↳ Block #20735255 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b3cb1b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
21696
ops/sec (±8.85%
)22440
ops/sec (±7.06%
)1.03
processingContractDeploy
40328
ops/sec (±6.93%
)38564
ops/sec (±7.90%
)0.96
processingContractMethodSend
17416
ops/sec (±6.78%
)16696
ops/sec (±7.81%
)0.96
processingContractMethodCall
28028
ops/sec (±7.80%
)27650
ops/sec (±7.82%
)0.99
abiEncode
46457
ops/sec (±6.59%
)44651
ops/sec (±6.88%
)0.96
abiDecode
31537
ops/sec (±7.34%
)30424
ops/sec (±7.61%
)0.96
sign
1584
ops/sec (±0.53%
)1574
ops/sec (±0.58%
)0.99
verify
364
ops/sec (±2.47%
)365
ops/sec (±2.65%
)1.00
This comment was automatically generated by workflow using github-action-benchmark.