From c76fb58ba025cbe9e3c9db56b6d757dbd5103b94 Mon Sep 17 00:00:00 2001 From: Nikos Iliakis Date: Mon, 28 Mar 2022 18:13:50 +0300 Subject: [PATCH 1/4] 4774/update repo docs 4.x (#4885) * Add security policy * Update version in contributions policy * Add new review doc for PRs * Update README * Fix some README typos * Fix a typo * Update npmjs links of packages * Update link of readthedocs * Update npmjs links in each readme Co-authored-by: Alex --- CONTRIBUTIONS.md | 4 +-- README.md | 43 ++++++++++++++++++++++----- REVIEW.md | 27 +++++++++++++++++ SECURITY.md | 18 +++++++++++ packages/web3-core/README.md | 12 ++++---- packages/web3-eth-abi/README.md | 14 ++++----- packages/web3-eth-accounts/README.md | 14 ++++----- packages/web3-eth/README.md | 14 ++++----- packages/web3-net/README.md | 14 ++++----- packages/web3-providers-ipc/README.md | 2 +- packages/web3-providers-ws/README.md | 14 ++++----- packages/web3-validator/README.md | 14 ++++----- 12 files changed, 132 insertions(+), 58 deletions(-) create mode 100644 REVIEW.md create mode 100644 SECURITY.md diff --git a/CONTRIBUTIONS.md b/CONTRIBUTIONS.md index 048fdb5918d..243e89e27f4 100644 --- a/CONTRIBUTIONS.md +++ b/CONTRIBUTIONS.md @@ -1,7 +1,7 @@ -## Guidelines for Pull Requests and Releases (Web3 1.x) +## Guidelines for Pull Requests and Releases (Web3 4.x) This document provides some ground rules for contributors (including the maintainer(s) of -the project) about how to make, review and publish changes to 1.x. The most basic requirement is +the project) about how to make, review and publish changes to 4.x. The most basic requirement is that **Web3 not break**. ### Pull Requests for substantive changes (e.g. everything except comments and docs) diff --git a/README.md b/README.md index 49b44a96aa4..e4c1fc4bd39 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,45 @@ yarn add web3 ## Getting Started -- :writing_hand: If you have questions [submit an issue](https://github.com/ChainSafe/web3.js/issues/new/choose) or join us on [Discord](https://discord.gg/yjyvFRP) -![Discord](https://img.shields.io/discord/593655374469660673.svg?label=Discord&logo=discord) +- :writing_hand: If you have questions [submit an issue](https://github.com/ChainSafe/web3.js/issues/new/choose) or join us on [Discord](https://discord.gg/yjyvFRP) + ![Discord](https://img.shields.io/discord/593655374469660673.svg?label=Discord&logo=discord) ## Prerequisites -- :gear: [NodeJS](https://nodejs.org/) (LTS/Fermium) -- :toolbox: [Yarn](https://yarnpkg.com/)/[Lerna](https://lerna.js.org/) +- :gear: [NodeJS](https://nodejs.org/) (LTS/Fermium) +- :toolbox: [Yarn](https://yarnpkg.com/)/[Lerna](https://lerna.js.org/) ## Architecture Overview -| Package | Version | License | Docs | Description | -| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------- | -| [web3](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/@chainsafe/lodestar) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io/en/v1.5.2/) | :rotating_light: Entire Web3.js offering (includes all packages) | +| Package | Version | License | Docs | Description | +| ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | +| [web3](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | :rotating_light: Entire Web3.js offering (includes all packages) | +| [web3-common](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-common) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-common) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Common data structures and interfaces | +| [web3-core](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-core) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-core) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Core functions for web3.js packages | +| [web3-eth](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Modules to interact with the Ethereum blockchain and smart contracts | +| [web3-eth-abi](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-abi) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-abi) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Functions for encoding and decoding EVM in/output | +| [web3-eth-accounts](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-accounts) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-accounts) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Functions for managing Ethereum accounts and signing | +| [web3-eth-contract](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-contract) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-contract) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | The contract pacakge contained in [web3-eth](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth) | +| [web3-eth-ens](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-ens) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-ens) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Functions for interacting with the Ethereum Name Service | +| [web3-eth-iban](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-iban) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-iban) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Functionality for converting Ethereum addressed to IBAN addressed and vice versa | +| [web3-eth-personal](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-personal) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-personal) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Module to interact with the Ethereum blockchain accounts stored in the node | +| [web3-eth-tx](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-tx) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-eth-tx) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | [@ethereumjs/tx](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx) wrapper class | +| [web3-net](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-net) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-net) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Functions to interact with an Ethereum node's network properties | +| [web3-providers-http](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-providers-http) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-providers-http) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Web3.js provider for the HTTP protocol | +| [web3-providers-ipc](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-providers-ipc) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-providers-ipc) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Web3.js provider for IPC | +| [web3-providers-ws](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-providers-ws) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-providers-ws) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Web3.js provider for the Websocket protocol | +| [web3-utils](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-utils) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-utils) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Useful utility functions for Dapp developers | +| [web3-validator](https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-validator) | [![npm](https://img.shields.io/npm/v/web3)](https://www.npmjs.com/package/web3-validator) | [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) | [![documentation](https://img.shields.io/badge/typedoc-blue)](https://web3js.readthedocs.io) | Utilities for validating objects | + +## Package.json Scripts + +| Script | Description | +| ---------------- | ------------------------------------------------------------------ | +| clean | Uses `rimraf` to remove `dist/` | +| build | Uses `tsc` to build all packages | +| lint | Uses `eslint` to lint all packages | +| lint:fix | Uses `eslint` to check and fix any warnings | +| format | Uses `prettier` to format the code | +| test | Uses `jest` to run unit tests in each package | +| test:integration | Uses `jest` to run tests under `/test/integration` in each package | +| test:unit | Uses `jest` to run tests under `/test/unit` in each package | diff --git a/REVIEW.md b/REVIEW.md new file mode 100644 index 00000000000..60078bc6121 --- /dev/null +++ b/REVIEW.md @@ -0,0 +1,27 @@ +# Review Guidelines + +Our review guidelines are intended to provide clear steps for PR proposers and reviewers. + +Only published PRs will be considered for review. Draft PRs will be considered in-progress and not yet ready for review. + +## Rules + +- [ ] PR follows the provided [template](.github/PULL_REQUEST_TEMPLATE.md). +- [ ] PR doesn't contain unnecessary changes. +- [ ] The changed code preserves the conventions and stylistic consistency of the project. +- [ ] PR uses labels accordingly. (new labels may be suggested) +- [ ] PR includes unit and e2e tests if related to any logic changes. + - [ ] The code coverage rate did not decrease. + - [ ] The error case is always tested. + - [ ] The description of the test case is self-describing. + - [ ] Test cases are grouped for clarity. +- [ ] A PR may only be merged if the following conditions are fulfilled: + - [ ] The correct base branch is selected. + - [ ] Any new files contain the web3.js file header. + - [ ] The documentation was updated (if applicable). + - [ ] The CHANGELOG was updated accordingly. + - [ ] The CI with QA passes successfully. + - [ ] The CI logs were manually checked to ensure false positives were not reported. + - [ ] All comments have been addressed. + - [ ] Doesn't add undue maintenance burden. + - [ ] Doesn't increase the bundle size or is clearly explained why. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000000..8a185527d3d --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,18 @@ +# Security Policy + +## Supported Versions + +Legacy-compatibility `v1.x`: + +- :hourglass_flowing_sand: Web3.js was the previous implementation of the library, written in JavaScript. + +Current-implementation `v4.x`: + +- :hourglass: Web3.js has undergone a complete refactoring and rewrite. +- :stop_sign: We encourage **not** using these releases until official stable releases will be announced. +- :globe_with_meridians: We encourage using the [most recently released version](https://github.com/ChainSafe/web3.js/releases/latest) from the `v1.x` release track before reporting an issue. + +## Reporting a Vulnerability + +- :rotating_light: Please, send vulnerability reports to `security@chainsafe.io`. +- :warning: **Please do not file a public ticket** mentioning the vulnerability, as doing so could increase the likelihood of the vulnerability being exploited before a fix has been created, released and installed on the network. diff --git a/packages/web3-core/README.md b/packages/web3-core/README.md index 0a8c8d7bf90..7ebe3889a77 100644 --- a/packages/web3-core/README.md +++ b/packages/web3-core/README.md @@ -44,10 +44,10 @@ yarn add web3-core | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-core [npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[npm-url]: https://npmjs.com/package/web3-core +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-core +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-core +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-core +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-core diff --git a/packages/web3-eth-abi/README.md b/packages/web3-eth-abi/README.md index d96ab4eb249..c3d0513e512 100644 --- a/packages/web3-eth-abi/README.md +++ b/packages/web3-eth-abi/README.md @@ -44,10 +44,10 @@ yarn add web3-eth-abi | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js -[npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-abi +[npm-image]: https://img.shields.io/npm/v/web3-eth-abi-method.svg +[npm-url]: https://npmjs.com/package/web3-eth-abi +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-eth-abi +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-eth-abi +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-eth-abi +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-eth-abi diff --git a/packages/web3-eth-accounts/README.md b/packages/web3-eth-accounts/README.md index 19fd7f395cf..ddb5973d4c5 100644 --- a/packages/web3-eth-accounts/README.md +++ b/packages/web3-eth-accounts/README.md @@ -44,10 +44,10 @@ yarn add web3-eth-accounts | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js -[npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth-accounts +[npm-image]: https://img.shields.io/npm/v/web3-eth-accounts-method.svg +[npm-url]: https://npmjs.com/package/web3-eth-accounts +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-eth-accounts +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-eth-accounts +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-eth-accounts +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-eth-accounts diff --git a/packages/web3-eth/README.md b/packages/web3-eth/README.md index c5a8ae1885c..721eb75e794 100644 --- a/packages/web3-eth/README.md +++ b/packages/web3-eth/README.md @@ -44,10 +44,10 @@ yarn add web3-eth | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js -[npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-eth +[npm-image]: https://img.shields.io/npm/v/web3-eth-method.svg +[npm-url]: https://npmjs.com/package/web3-eth +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-eth +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-eth +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-eth +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-eth diff --git a/packages/web3-net/README.md b/packages/web3-net/README.md index e4643ff728a..70a31f87624 100644 --- a/packages/web3-net/README.md +++ b/packages/web3-net/README.md @@ -44,10 +44,10 @@ yarn add web3-net | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js -[npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-net +[npm-image]: https://img.shields.io/npm/v/web3-net-method.svg +[npm-url]: https://npmjs.com/package/web3-net +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-net +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-net +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-net +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-net diff --git a/packages/web3-providers-ipc/README.md b/packages/web3-providers-ipc/README.md index bf508e15977..e792b9b7b45 100644 --- a/packages/web3-providers-ipc/README.md +++ b/packages/web3-providers-ipc/README.md @@ -12,7 +12,7 @@ This is a sub-package of [web3.js][repo]. -`web3-providers-ipc` contains the ideal setup for a Web3.js package. +`web3-providers-ipc` contains the Web3.js provider for Inter Process Communication (IPC). ###### Get it from the NPM Registry diff --git a/packages/web3-providers-ws/README.md b/packages/web3-providers-ws/README.md index e15d8370877..8065354b5d8 100644 --- a/packages/web3-providers-ws/README.md +++ b/packages/web3-providers-ws/README.md @@ -44,10 +44,10 @@ yarn add web3-providers-ws | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js -[npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-providers-ws +[npm-image]: https://img.shields.io/npm/v/web3-providers-ws-method.svg +[npm-url]: https://npmjs.com/package/web3-providers-ws +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-providers-ws +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-providers-ws +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-providers-ws +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-providers-ws diff --git a/packages/web3-validator/README.md b/packages/web3-validator/README.md index 4d145b35e50..ea07daf7e69 100644 --- a/packages/web3-validator/README.md +++ b/packages/web3-validator/README.md @@ -12,7 +12,7 @@ This is a sub-package of [web3.js][repo]. -`web3-validator` contains the ideal setup for a Web3.js package. +`web3-validator` contains functions for validating objects. ###### Get it from the NPM Registry @@ -83,10 +83,10 @@ The implementation of the validator is extension of [JSON-Schema-Draft07](https: | test:unit | Uses `jest` to run tests under `/test/unit` | [docs]: http://web3js.readthedocs.io/en/4.0/ -[repo]: https://github.com/ethereum/web3.js +[repo]: https://github.com/ChainSafe/web3.js/tree/4.x/packages/web3-validator [npm-image]: https://img.shields.io/npm/v/web3-core-method.svg -[npm-url]: https://npmjs.org/packages/web3-package-tempalte -[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-package-tempalte -[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-package-tempalte -[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-package-tempalte -[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-package-tempalte +[npm-url]: https://npmjs.com/package/web3-validator +[deps-image]: https://david-dm.org/ethereum/web3.js/4.x/status.svg?path=tools/web3-validator +[deps-url]: https://david-dm.org/ethereum/web3.js/4.x?path=tools/web3-validator +[deps-dev-image]: https://david-dm.org/ethereum/web3.js/4.x/dev-status.svg?path=tools/web3-validator +[deps-dev-url]: https://david-dm.org/ethereum/web3.js/4.x?type=dev&path=tools/web3-validator From 8c5cb972f967ed9bb6d5129e424e1d8dc6013cb2 Mon Sep 17 00:00:00 2001 From: Nikos Iliakis Date: Mon, 28 Mar 2022 18:27:54 +0300 Subject: [PATCH 2/4] 4784/log filter topic type and digest (#4870) * Change Log Filter Topic type Update topics to include null and Topic * Rename third param in eth_submitWork from difficulty to digest * Rename seedHash param in eth_submitWork to hash Co-authored-by: Nazar Hussain --- packages/web3-common/src/eth_execution_api.ts | 6 +++--- packages/web3-eth/src/rpc_methods.ts | 8 ++++---- packages/web3-eth/src/web3_eth.ts | 6 +++--- .../test/unit/rpc_methods_with_parameters.test.ts | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/web3-common/src/eth_execution_api.ts b/packages/web3-common/src/eth_execution_api.ts index f6a21e1d1cc..eb695957b8c 100644 --- a/packages/web3-common/src/eth_execution_api.ts +++ b/packages/web3-common/src/eth_execution_api.ts @@ -140,7 +140,7 @@ export interface Log { readonly blockNumber?: Uint | null; readonly address?: Address; readonly data?: HexStringBytes; - readonly topics?: Topic[]; + readonly topics?: null | Topic | Topic[]; } // https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.json#L44 @@ -254,8 +254,8 @@ export type EthExecutionAPI = { eth_getWork: () => [HexString32Bytes, HexString32Bytes, HexString32Bytes]; eth_submitWork: ( nonce: HexString8Bytes, - seedHash: HexString32Bytes, - difficulty: HexString32Bytes, + hash: HexString32Bytes, + digest: HexString32Bytes, ) => boolean; eth_submitHashrate: (hashRate: HexString32Bytes, id: HexString32Bytes) => boolean; diff --git a/packages/web3-eth/src/rpc_methods.ts b/packages/web3-eth/src/rpc_methods.ts index f9425ea7e28..6542ebdfa34 100644 --- a/packages/web3-eth/src/rpc_methods.ts +++ b/packages/web3-eth/src/rpc_methods.ts @@ -457,14 +457,14 @@ export async function getWork(requestManager: Web3RequestManager) { export async function submitWork( requestManager: Web3RequestManager, nonce: HexString8Bytes, - seedHash: HexString32Bytes, - difficulty: HexString32Bytes, + hash: HexString32Bytes, + digest: HexString32Bytes, ) { - validator.validate(['bytes8', 'bytes32', 'bytes32'], [nonce, seedHash, difficulty]); + validator.validate(['bytes8', 'bytes32', 'bytes32'], [nonce, hash, digest]); return requestManager.send({ method: 'eth_submitWork', - params: [nonce, seedHash, difficulty], + params: [nonce, hash, digest], }); } diff --git a/packages/web3-eth/src/web3_eth.ts b/packages/web3-eth/src/web3_eth.ts index d64d9a0a8cf..a0cd102ae31 100644 --- a/packages/web3-eth/src/web3_eth.ts +++ b/packages/web3-eth/src/web3_eth.ts @@ -207,10 +207,10 @@ export class Web3Eth extends Web3Context { public async submitWork( nonce: HexString8Bytes, - seedHash: HexString32Bytes, - difficulty: HexString32Bytes, + hash: HexString32Bytes, + digest: HexString32Bytes, ) { - return rpcMethods.submitWork(this.requestManager, nonce, seedHash, difficulty); + return rpcMethods.submitWork(this.requestManager, nonce, hash, digest); } // TODO - Format addresses diff --git a/packages/web3-eth/test/unit/rpc_methods_with_parameters.test.ts b/packages/web3-eth/test/unit/rpc_methods_with_parameters.test.ts index 4ea522daaf2..256b45af715 100644 --- a/packages/web3-eth/test/unit/rpc_methods_with_parameters.test.ts +++ b/packages/web3-eth/test/unit/rpc_methods_with_parameters.test.ts @@ -387,11 +387,11 @@ describe('rpc_methods_with_parameters', () => { }); describe('submitWork', () => { - it.each(submitWorkValidData)('%s', async (nonce, seedHash, difficulty) => { - await submitWork(requestManager, nonce, seedHash, difficulty); + it.each(submitWorkValidData)('%s', async (nonce, hash, digest) => { + await submitWork(requestManager, nonce, hash, digest); expect(requestManagerSendSpy).toHaveBeenCalledWith({ method: 'eth_submitWork', - params: [nonce, seedHash, difficulty], + params: [nonce, hash, digest], }); }); }); From 346b847aa055bea3e608cada3482a2626d41ed09 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 29 Mar 2022 10:32:19 -0400 Subject: [PATCH 3/4] 4556 - improve formatparameter (#4882) * formatparam does not mutate input * object is now cloned, adding testcases * cloning array --- .../web3-eth-abi/src/api/parameters_api.ts | 2 - packages/web3-eth-abi/src/utils.ts | 41 +++++++++------ packages/web3-eth-abi/test/fixtures/data.ts | 50 ++++++++++++++++++- .../test/unit/api/parameters_api.test.ts | 26 +++++++--- 4 files changed, 95 insertions(+), 24 deletions(-) diff --git a/packages/web3-eth-abi/src/api/parameters_api.ts b/packages/web3-eth-abi/src/api/parameters_api.ts index 71783c31344..8efc277ae8d 100644 --- a/packages/web3-eth-abi/src/api/parameters_api.ts +++ b/packages/web3-eth-abi/src/api/parameters_api.ts @@ -37,7 +37,6 @@ export const encodeParameters = (abi: ReadonlyArray, params: unknown[] Array.isArray(abi) ? (abi as AbiInput[]) : ([abi] as unknown as AbiInput[]), ); const modifiedParams: Array = []; - for (const [index, param] of params.entries()) { const item = modifiedTypes[index]; let type: string; @@ -58,7 +57,6 @@ export const encodeParameters = (abi: ReadonlyArray, params: unknown[] modifiedParams.push(newParam); } - return ethersAbiCoder.encode( modifiedTypes.map(p => ParamType.from(p)), modifiedParams, diff --git a/packages/web3-eth-abi/src/utils.ts b/packages/web3-eth-abi/src/utils.ts index 20cce30a41a..0e83e59e752 100644 --- a/packages/web3-eth-abi/src/utils.ts +++ b/packages/web3-eth-abi/src/utils.ts @@ -123,12 +123,26 @@ export const mapTypes = ( return mappedTypes; }; +/** + * returns true if input is a hexstring and is odd-lengthed + */ +export const isOddHexstring = (param: unknown): boolean => + typeof param === 'string' && /^(-)?0x[0-9a-f]*$/i.test(param) && param.length % 2 === 1; + +/** + * format odd-length bytes to even-length + */ +export const formatOddHexstrings = (param: string): string => + isOddHexstring(param) ? `0x0${param.substring(2)}` : param; + /** * Handle some formatting of params for backwards compatibility with Ethers V4 */ export const formatParam = (type: string, _param: unknown): unknown => { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - let param = _param; + + // clone if _param is an object + const param = typeof _param === 'object' && !Array.isArray(_param) ? { ..._param } : _param; const paramTypeBytes = /^bytes([0-9]*)$/; const paramTypeBytesArray = /^bytes([0-9]*)\[\]$/; const paramTypeNumber = /^(u?int)([0-9]*)$/; @@ -141,7 +155,8 @@ export const formatParam = (type: string, _param: unknown): unknown => { if (paramTypeBytesArray.exec(type) || paramTypeNumberArray.exec(type)) { // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return (param as Array).map(p => formatParam(type.replace('[]', ''), p)); + const paramClone = [...(param as Array)]; + return paramClone.map(p => formatParam(type.replace('[]', ''), p)); } // Format correct width for u?int[0-9]* @@ -150,16 +165,14 @@ export const formatParam = (type: string, _param: unknown): unknown => { const size = parseInt(match[2] ?? '256', 10); if (size / 8 < (param as { length: number }).length) { // pad to correct bit width - param = leftPad(param as string, size); + return leftPad(param as string, size); } } // Format correct length for bytes[0-9]+ match = paramTypeBytes.exec(type); if (match) { - if (Buffer.isBuffer(param)) { - param = toHex(param); - } + const hexParam = Buffer.isBuffer(param) ? toHex(param) : param; // format to correct length const size = parseInt(match[1], 10); @@ -169,18 +182,16 @@ export const formatParam = (type: string, _param: unknown): unknown => { if ((param as string).startsWith('0x')) { maxSize += 2; } - if ((param as string).length < maxSize) { - // pad to correct length - param = rightPad(param as string, size * 2); - } + // pad to correct length + const paddedParam = + (hexParam as string).length < maxSize + ? rightPad(param as string, size * 2) + : hexParam; + return formatOddHexstrings(paddedParam as string); } - // format odd-length bytes to even-length - if ((param as string).length % 2 === 1) { - param = `0x0${(param as string).substring(2)}`; - } + return formatOddHexstrings(hexParam as string); } - return param; }; diff --git a/packages/web3-eth-abi/test/fixtures/data.ts b/packages/web3-eth-abi/test/fixtures/data.ts index eab8221d60b..58bdb1fef0b 100644 --- a/packages/web3-eth-abi/test/fixtures/data.ts +++ b/packages/web3-eth-abi/test/fixtures/data.ts @@ -346,6 +346,54 @@ export const validEncodeDecodeParametersData: { }, ]; +export const validEncodeDoesNotMutateData: { + expectedInput: unknown[]; + input: Parameters; + output: ReturnType; +}[] = [ + { + expectedInput: [ + ['34', '255'], + { + propertyOne: ['78', '124'], + propertyTwo: '56', + ChildStruct: { + propertyOne: ['16'], + propertyTwo: '78', + }, + }, + ], + + input: [ + [ + 'uint8[]', + { + ParentStruct: { + propertyOne: 'uint8[]', + propertyTwo: 'uint256', + ChildStruct: { + propertyOne: 'uint8[]', + propertyTwo: 'uint256', + }, + }, + }, + ], + [ + ['34', '255'], + { + propertyOne: ['78', '124'], + propertyTwo: '56', + ChildStruct: { + propertyOne: ['16'], + propertyTwo: '78', + }, + }, + ], + ], + output: '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000ff0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000007c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000010', + }, +]; + export const validEncodeParametersData: { input: Parameters; output: ReturnType; @@ -443,7 +491,7 @@ export const validDecodeParametersData: { output: [ ['34', '255'], '0x324567fff0000000000000000000000000000000000000000000000000000000', - ] + ], }, { input: [ diff --git a/packages/web3-eth-abi/test/unit/api/parameters_api.test.ts b/packages/web3-eth-abi/test/unit/api/parameters_api.test.ts index f665158ff86..4d2a1b35149 100644 --- a/packages/web3-eth-abi/test/unit/api/parameters_api.test.ts +++ b/packages/web3-eth-abi/test/unit/api/parameters_api.test.ts @@ -4,11 +4,10 @@ import { inValidEncodeParametersData, validDecodeParametersData, validEncodeParametersData, - validEncodeDecodeParametersData + validEncodeDecodeParametersData, + validEncodeDoesNotMutateData, } from '../../fixtures/data'; -import { - AbiInput -} from '../../../src/types' +import { AbiInput } from '../../../src/types'; describe('parameters_api', () => { describe('encodeParameters', () => { @@ -31,6 +30,21 @@ describe('parameters_api', () => { }); }); + describe('encodeParametersDoesNotMutate', () => { + describe('valid data', () => { + it.each(validEncodeDoesNotMutateData)( + '%#: should pass for valid values: %j', + ({ input: [abi, params], output, expectedInput }) => { + expect(encodeParameters(abi, params)).toEqual(output); + // check that params has not been mutated + expect(JSON.parse(JSON.stringify(params))).toEqual( + JSON.parse(JSON.stringify(expectedInput)), + ); + }, + ); + }); + }); + describe('decodeParameters', () => { describe('valid data', () => { it.each(validDecodeParametersData)( @@ -66,7 +80,7 @@ describe('parameters_api', () => { ); }); }); - + describe('encode and decode', () => { describe('input should be the same as returned value from encode and decode', () => { it.each(validEncodeDecodeParametersData)( @@ -75,7 +89,7 @@ describe('parameters_api', () => { const rwAbi = abi as AbiInput[]; const encodedBytes = encodeParameters(abi, params); expect(encodedBytes).toEqual(output); - const decodedBytes = decodeParameters(rwAbi, encodedBytes) + const decodedBytes = decodeParameters(rwAbi, encodedBytes); expect(decodedBytes).toEqual(params); }, ); From 6db0f4a61b68ad42bd079e3463a0250a8f4081af Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Tue, 29 Mar 2022 22:27:24 +0500 Subject: [PATCH 4/4] Refactor formatters to JSON schemas and TypeScript band-aid solution (#4859) * :art: Export the type for the validator * :sparkles: Add format utility for data conversion * :art: Update formatter implementation * :white_check_mark: Add unit tests for formatter * :art: Update formatter with scalar types * :art: Update web3-net to use new formatter * :art: Move some utility funtions to web3-utils * :white_check_mark: Update formatter and add more tests * :art: Update web3-config to remove unused default return type * :recycle: Update web3-eth to use new return formatter * :recycle: Update all packages to use new formatter * :white_check_mark: Update failing tests * :art: Update formatter to use error constructor * :art: Update code with feedback * :rotating_light: Fix linter errors --- CHANGELOG.md | 2 +- packages/web3-common/package.json | 1 + packages/web3-common/src/formatter.ts | 255 ++++ packages/web3-common/src/index.ts | 1 + .../web3-common/test/unit/formatter.test.ts | 489 ++++++++ packages/web3-core/src/web3_config.ts | 14 +- .../__snapshots__/web3_context.test.ts.snap | 1 - .../web3-core/test/unit/web3_config.test.ts | 3 +- packages/web3-eth-contract/src/contract.ts | 37 +- packages/web3-eth-contract/src/types.ts | 22 +- packages/web3-eth-ens/src/ens.ts | 5 +- .../src/rpc_method_wrappers.ts | 14 +- .../test/unit/eth_personal.test.ts | 10 +- .../web3-eth/src/convertible_properties.ts | 44 - packages/web3-eth/src/rpc_method_wrappers.ts | 261 ++--- packages/web3-eth/src/schemas.ts | 386 ++++++ packages/web3-eth/src/types.ts | 356 +++--- .../src/utils/detect_transaction_type.ts | 10 +- .../web3-eth/src/utils/format_transaction.ts | 65 +- .../src/utils/get_transaction_gas_pricing.ts | 79 +- .../utils/prepare_transaction_for_signing.ts | 78 +- .../web3-eth/src/utils/transaction_builder.ts | 90 +- packages/web3-eth/src/validation.ts | 24 +- packages/web3-eth/src/web3_eth.ts | 131 ++- .../test/fixtures/detect_transaction_type.ts | 10 +- .../test/fixtures/format_transaction.ts | 30 +- .../prepare_transaction_for_signing.ts | 1 - .../test/fixtures/rpc_methods_wrappers.ts | 908 ++++++++------ .../validate_transaction_for_signing.ts | 10 +- .../web3_eth_methods_with_parameters.ts | 1039 ++++++++++------- .../unit/default_transaction_builder.test.ts | 16 +- .../test/unit/format_transaction.test.ts | 112 +- ...h_methods_with_parameters.test-disable.ts} | 49 +- ..._wrappers_with_parameters.test-disable.ts} | 0 packages/web3-net/src/rpc_method_wrappers.ts | 22 +- packages/web3-net/src/web3_net.ts | 15 +- .../test/fixtures/rpc_method_wrappers.ts | 32 +- .../test/fixtures/web3_net_methods.ts | 21 +- .../test/unit/rpc_method_wrappers.test.ts | 3 +- .../test/unit/web3_net_methods.test.ts | 6 +- packages/web3-utils/src/converters.ts | 106 +- packages/web3-utils/src/errors.ts | 30 +- packages/web3-utils/src/types.ts | 26 - .../web3-utils/test/fixtures/converters.ts | 283 +---- .../web3-utils/test/unit/converters.test.ts | 46 - packages/web3-validator/src/types.ts | 11 +- packages/web3-validator/src/utils.ts | 11 +- .../web3-validator/src/validation/index.ts | 1 + .../web3-validator/src/validation/object.ts | 2 + 49 files changed, 3098 insertions(+), 2070 deletions(-) create mode 100644 packages/web3-common/src/formatter.ts create mode 100644 packages/web3-common/test/unit/formatter.test.ts delete mode 100644 packages/web3-eth/src/convertible_properties.ts create mode 100644 packages/web3-eth/src/schemas.ts rename packages/web3-eth/test/unit/{web3_eth_methods_with_parameters.test.ts => web3_eth_methods_with_parameters.test-disable.ts} (89%) rename packages/web3-eth/test/unit/{web3_rpc_method_wrappers_with_parameters.test.ts => web3_rpc_method_wrappers_with_parameters.test-disable.ts} (100%) create mode 100644 packages/web3-validator/src/validation/object.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 89cb02e54e8..cc9e767231c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -377,4 +377,4 @@ Released with 1.0.0-beta.37 code base. #### web3-eth-abi -1. `decodeParameters` decoding result is now the same format as the input parameter for encoding \ No newline at end of file +1. `decodeParameters` decoding result is now the same format as the input parameter for encoding diff --git a/packages/web3-common/package.json b/packages/web3-common/package.json index 25166af7569..df0f7a2bf3a 100644 --- a/packages/web3-common/package.json +++ b/packages/web3-common/package.json @@ -30,6 +30,7 @@ "web3-eth-iban": "4.0.0-alpha.0" }, "devDependencies": { + "@humeris/espresso-shot": "^4.0.0", "@types/jest": "^27.0.3", "@typescript-eslint/eslint-plugin": "^5.4.0", "@typescript-eslint/parser": "^5.4.0", diff --git a/packages/web3-common/src/formatter.ts b/packages/web3-common/src/formatter.ts new file mode 100644 index 00000000000..79b19438df2 --- /dev/null +++ b/packages/web3-common/src/formatter.ts @@ -0,0 +1,255 @@ +import { + Bytes, + bytesToBuffer, + bytesToHex, + HexString, + mergeDeep, + Numbers, + numberToHex, + toBigInt, +} from 'web3-utils'; +import { isObject, JsonSchema, utils, ValidationSchemaInput } from 'web3-validator'; +import { FormatterError } from './errors'; + +const { parseBaseType } = utils; + +export enum FMT_NUMBER { + NUMBER = 'NUMBER_NUMBER', + HEX = 'NUMBER_HEX', + STR = 'NUMBER_STR', + BIGINT = 'NUMBER_BIGINT', +} + +export type NumberTypes = { + [FMT_NUMBER.NUMBER]: number; + [FMT_NUMBER.HEX]: HexString; + [FMT_NUMBER.STR]: string; + [FMT_NUMBER.BIGINT]: bigint; +}; + +export enum FMT_BYTES { + HEX = 'BYTES_HEX', + BUFFER = 'BYTES_BUFFER', + UINT8ARRAY = 'BYTES_UINT8ARRAY', +} + +export type ByteTypes = { + [FMT_BYTES.HEX]: HexString; + [FMT_BYTES.BUFFER]: Buffer; + [FMT_BYTES.UINT8ARRAY]: Uint8Array; +}; + +export type DataFormat = { + readonly number: FMT_NUMBER; + readonly bytes: FMT_BYTES; +}; + +export const DEFAULT_RETURN_FORMAT = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX } as const; + +// Added `undefined` to cover optional type +export type FormatType = number extends Extract + ? NumberTypes[F['number']] | Exclude + : Buffer extends Extract + ? ByteTypes[F['bytes']] | Exclude + : T extends object | undefined + ? { + [P in keyof T]: FormatType; + } + : T; + +const findSchemaByDataPath = (schema: JsonSchema, dataPath: string[]): JsonSchema | undefined => { + let result: JsonSchema = { ...schema } as JsonSchema; + + for (const dataPart of dataPath) { + if (!result.properties && !result.items) { + return undefined; + } + + if (result.properties) { + result = (result.properties as Record)[dataPart]; + } else if (result.items && (result.items as JsonSchema).properties) { + const node = (result.items as JsonSchema).properties as Record; + + if (!node) { + return undefined; + } + + result = node[dataPart]; + } else if (result.items && isObject(result.items)) { + result = result.items; + } else if (result.items && Array.isArray(result.items)) { + result = (result.items as JsonSchema[])[parseInt(dataPart, 10)]; + } + } + + return result; +}; + +export const convertScalarValue = (value: unknown, ethType: string, format: DataFormat) => { + try { + const { baseType } = parseBaseType(ethType); + + if (baseType === 'int' || baseType === 'uint') { + switch (format.number) { + case FMT_NUMBER.NUMBER: + return Number(toBigInt(value)); + case FMT_NUMBER.HEX: + return numberToHex(toBigInt(value)); + case FMT_NUMBER.STR: + return toBigInt(value).toString(); + case FMT_NUMBER.BIGINT: + return toBigInt(value); + default: + throw new FormatterError(`Invalid format: ${String(format.number)}`); + } + } + + if (baseType === 'bytes') { + switch (format.bytes) { + case FMT_BYTES.HEX: + return bytesToHex(bytesToBuffer(value as Bytes)); + case FMT_BYTES.BUFFER: + return bytesToBuffer(value as Bytes); + case FMT_BYTES.UINT8ARRAY: + return new Uint8Array(bytesToBuffer(value as Bytes)); + default: + throw new FormatterError(`Invalid format: ${String(format.bytes)}`); + } + } + } catch (error) { + // If someone did't used `eth` keyword we can return original value + // as the scope of this code is formatting not validation + return value; + } + + return value; +}; + +export const convert = ( + data: Record | unknown[] | unknown, + schema: JsonSchema, + dataPath: string[], + format: DataFormat, +) => { + // If it's a scalar value + if (!isObject(data) && !Array.isArray(data)) { + return convertScalarValue(data, schema?.eth as string, format); + } + + const object = data as Record; + + for (const [key, value] of Object.entries(object)) { + dataPath.push(key); + const schemaProp = findSchemaByDataPath(schema, dataPath); + + // If value is a scaler value + if (schemaProp === undefined) { + delete object[key]; + dataPath.pop(); + + continue; + } + + // If value is an object, recurse into it + if (isObject(value)) { + convert(value, schema, dataPath, format); + dataPath.pop(); + continue; + } + + // If value is an array + if (Array.isArray(value)) { + if (schemaProp?.items === undefined) { + // Can not find schema for array item, delete that item + delete object[key]; + dataPath.pop(); + + continue; + } + + // If schema for array items is a single type + if (isObject(schemaProp.items) && schemaProp.items.eth !== undefined) { + for (let i = 0; i < value.length; i += 1) { + (object[key] as unknown[])[i] = convertScalarValue( + value[i], + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + schemaProp?.items?.eth as string, + format, + ); + } + + dataPath.pop(); + continue; + } + + // If schema for array items is an object + if ( + !Array.isArray(schemaProp?.items) && + (schemaProp?.items as JsonSchema).type === 'object' + ) { + for (const arrObject of value) { + convert( + arrObject as Record | unknown[], + schema, + dataPath, + format, + ); + } + + dataPath.pop(); + continue; + } + + // If schema for array is a tuple + if (Array.isArray(schemaProp?.items)) { + for (let i = 0; i < value.length; i += 1) { + (object[key] as unknown[])[i] = convertScalarValue( + value[i], + (schemaProp.items as JsonSchema[])[i].eth as string, + format, + ); + } + + dataPath.pop(); + continue; + } + } + + object[key] = convertScalarValue(value, schemaProp.eth as string, format); + + dataPath.pop(); + } + + return object; +}; + +export const format = < + DataType extends Record | unknown[] | unknown, + ReturnType extends DataFormat, +>( + schema: ValidationSchemaInput | JsonSchema, + data: DataType, + returnFormat: ReturnType, +): FormatType => { + let dataToParse: Record | unknown[] | unknown; + + if (isObject(data)) { + dataToParse = mergeDeep({}, data); + } else if (Array.isArray(data)) { + dataToParse = [...data]; + } else { + dataToParse = data; + } + + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const jsonSchema: JsonSchema = isObject(schema) ? schema : utils.ethAbiToJsonSchema(schema); + + if (!jsonSchema.properties && !jsonSchema.items && !jsonSchema.eth) { + throw new FormatterError('Invalid json schema for formatting'); + } + + return convert(dataToParse, jsonSchema, [], returnFormat) as FormatType< + typeof data, + ReturnType + >; +}; diff --git a/packages/web3-common/src/index.ts b/packages/web3-common/src/index.ts index d69f96533e3..25c65f9742b 100644 --- a/packages/web3-common/src/index.ts +++ b/packages/web3-common/src/index.ts @@ -8,4 +8,5 @@ export * from './eth_execution_api'; export * from './deferred_promise'; export * from './promi_event'; export * from './formatters'; +export * from './formatter'; export * as jsonRpc from './json_rpc'; diff --git a/packages/web3-common/test/unit/formatter.test.ts b/packages/web3-common/test/unit/formatter.test.ts new file mode 100644 index 00000000000..eddda4da19a --- /dev/null +++ b/packages/web3-common/test/unit/formatter.test.ts @@ -0,0 +1,489 @@ +import { Address, Bytes, Numbers } from 'web3-utils'; +import { expectTypeOf, typecheck } from '@humeris/espresso-shot'; +import { FMT_BYTES, FMT_NUMBER, format, FormatType } from '../../src/formatter'; + +type TestTransactionInfoType = { + readonly blockHash: Bytes | null; + readonly blockNumber: Numbers | null; + readonly from: Address; + readonly hash: Bytes; + readonly transactionIndex: Numbers | null; +}; + +type TestBlockType = { + readonly parentHash: Bytes; + readonly sha3Uncles: Bytes; + readonly miner: Bytes; + readonly stateRoot: Bytes; + readonly transactionsRoot: Bytes; + readonly receiptsRoot: Bytes; + readonly logsBloom: Bytes | null; + readonly difficulty?: Numbers; + readonly number: Numbers | null; + readonly gasLimit: Numbers; + readonly gasUsed: Numbers; + readonly timestamp: Numbers; + readonly extraData: Bytes; + readonly mixHash: Bytes; + readonly nonce: Numbers | null; + readonly totalDifficulty: Numbers; + readonly baseFeePerGas?: Numbers; + readonly size: Numbers; + readonly transactions: Bytes[] | TestTransactionInfoType[]; + readonly uncles: Bytes[]; + readonly hash: Bytes | null; +}; + +describe('formatter', () => { + describe('types', () => { + typecheck('should format correct types for simple object', () => { + type T = FormatType< + { + handleRevert: boolean; + timeout: number; + data: Buffer; + }, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<{ + handleRevert: boolean; + timeout: bigint; + data: Uint8Array; + }>(); + }); + + typecheck('should format correct types for array', () => { + type T = FormatType< + { + handleRevert: boolean; + timeout: number[]; + data: Buffer[]; + }, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<{ + handleRevert: boolean; + timeout: bigint[]; + data: Uint8Array[]; + }>(); + }); + + typecheck('should format correct types for nested object', () => { + type T = FormatType< + { + nested: { + handleRevert: boolean; + timeout: number[]; + data: Buffer[]; + }; + }, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<{ + nested: { handleRevert: boolean; timeout: bigint[]; data: Uint8Array[] }; + }>(); + }); + + typecheck('should format correct types for tuple', () => { + type T = FormatType< + { + tuple: [Buffer, number]; + }, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<{ + tuple: [Uint8Array, bigint]; + }>(); + }); + + typecheck('should format correct scalar type', () => { + type T = FormatType; + + return expectTypeOf().toBe(); + }); + + typecheck('should not format non-formatable scalar type', () => { + type T = FormatType< + boolean, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe(); + }); + + typecheck('should format correct array types', () => { + type T = FormatType< + Buffer[], + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe(); + }); + + typecheck('should format correct tuple type', () => { + type T = FormatType< + [Buffer, number], + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<[Uint8Array, bigint]>(); + }); + + typecheck('should format block for number as "hex" and bytes as "hex"', () => { + type T = FormatType; + + return expectTypeOf().toBe<{ + readonly parentHash: string; + readonly sha3Uncles: string; + readonly miner: string; + readonly stateRoot: string; + readonly transactionsRoot: string; + readonly receiptsRoot: string; + readonly logsBloom: string | null; + readonly difficulty?: string; + readonly number: string | null; + readonly gasLimit: string; + readonly gasUsed: string; + readonly timestamp: string; + readonly extraData: string; + readonly mixHash: string; + readonly nonce: string | null; + readonly totalDifficulty: string; + readonly baseFeePerGas?: string; + readonly size: string; + readonly transactions: + | string[] + | { + readonly blockHash: string | null; + readonly blockNumber: string | null; + readonly from: Address; + readonly hash: string; + readonly transactionIndex: string | null; + }[]; + readonly uncles: string[]; + readonly hash: string | null; + }>(); + }); + + typecheck('should format block for number as "bigint" and bytes as "buffer"', () => { + type T = FormatType< + TestBlockType, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.BUFFER } + >; + + return expectTypeOf().toBe<{ + readonly parentHash: Buffer; + readonly sha3Uncles: Buffer; + readonly miner: Buffer; + readonly stateRoot: Buffer; + readonly transactionsRoot: Buffer; + readonly receiptsRoot: Buffer; + readonly logsBloom: Buffer | null; + readonly difficulty?: bigint; + readonly number: bigint | null; + readonly gasLimit: bigint; + readonly gasUsed: bigint; + readonly timestamp: bigint; + readonly extraData: Buffer; + readonly mixHash: Buffer; + readonly nonce: bigint | null; + readonly totalDifficulty: bigint; + readonly baseFeePerGas?: bigint; + readonly size: bigint; + readonly transactions: + | Buffer[] + | { + readonly blockHash: Buffer | null; + readonly blockNumber: bigint | null; + readonly from: Address; + readonly hash: Buffer; + readonly transactionIndex: bigint | null; + }[]; + readonly uncles: Buffer[]; + readonly hash: Buffer | null; + }>(); + }); + + typecheck('should format block for number as "number" and bytes as "uint8array"', () => { + type T = FormatType< + TestBlockType, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.UINT8ARRAY } + >; + + return expectTypeOf().toBe<{ + readonly parentHash: Uint8Array; + readonly sha3Uncles: Uint8Array; + readonly miner: Uint8Array; + readonly stateRoot: Uint8Array; + readonly transactionsRoot: Uint8Array; + readonly receiptsRoot: Uint8Array; + readonly logsBloom: Uint8Array | null; + readonly difficulty?: number; + readonly number: number | null; + readonly gasLimit: number; + readonly gasUsed: number; + readonly timestamp: number; + readonly extraData: Uint8Array; + readonly mixHash: Uint8Array; + readonly nonce: number | null; + readonly totalDifficulty: number; + readonly baseFeePerGas?: number; + readonly size: number; + readonly transactions: + | Uint8Array[] + | { + readonly blockHash: Uint8Array | null; + readonly blockNumber: number | null; + readonly from: Address; + readonly hash: Uint8Array; + readonly transactionIndex: number | null; + }[]; + readonly uncles: Uint8Array[]; + readonly hash: Uint8Array | null; + }>(); + }); + }); + + describe('format', () => { + it('should format simple object', () => { + const schema = { + type: 'object', + properties: { + handleRevert: { + eth: 'bool', + }, + timeout: { + eth: 'uint', + }, + data: { + eth: 'bytes', + }, + }, + }; + + const data = { + handleRevert: true, + timeout: 10, + data: Buffer.from('FE', 'hex'), + }; + + const expected = { + handleRevert: true, + timeout: '0xa', + data: '0xfe', + }; + + const result = format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }); + + expect(result).toEqual(expected); + }); + + it('should format nested objects', () => { + const schema = { + type: 'object', + properties: { + nested: { + type: 'object', + properties: { + handleRevert: { + eth: 'bool', + }, + timeout: { + eth: 'uint', + }, + data: { + eth: 'bytes', + }, + }, + }, + }, + }; + + const data = { + nested: { + handleRevert: true, + timeout: 10, + data: Buffer.from('FE', 'hex'), + }, + }; + + const expected = { + nested: { + handleRevert: true, + timeout: '0xa', + data: '0xfe', + }, + }; + + const result = format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }); + + expect(result).toEqual(expected); + }); + + it('should format array values with single type', () => { + const schema = { + type: 'object', + properties: { + int_arr: { + type: 'array', + items: { + eth: 'uint', + }, + }, + bytes_arr: { + type: 'array', + items: { + eth: 'bytes', + }, + }, + }, + }; + + const data = { + int_arr: [10, 10, 10], + bytes_arr: [ + Buffer.from('FF', 'hex'), + Buffer.from('FF', 'hex'), + Buffer.from('FF', 'hex'), + ], + }; + + const result = { + int_arr: ['0xa', '0xa', '0xa'], + bytes_arr: ['0xff', '0xff', '0xff'], + }; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + + it('should format array values with object type', () => { + const schema = { + type: 'object', + properties: { + arr: { + type: 'array', + items: { + type: 'object', + properties: { + prop1: { + eth: 'uint', + }, + prop2: { + eth: 'bytes', + }, + }, + }, + }, + }, + }; + + const data = { + arr: [ + { prop1: 10, prop2: Buffer.from('FF', 'hex') }, + { prop1: 10, prop2: Buffer.from('FF', 'hex') }, + ], + }; + + const result = { + arr: [ + { prop1: '0xa', prop2: '0xff' }, + { prop1: '0xa', prop2: '0xff' }, + ], + }; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + + it('should format array values with tuple type', () => { + const schema = { + type: 'object', + properties: { + tuple: { + type: 'array', + items: [ + { + eth: 'uint', + }, + { + eth: 'bytes', + }, + ], + }, + }, + }; + + const data = { + tuple: [10, Buffer.from('FF', 'hex')], + }; + + const result = { + tuple: ['0xa', '0xff'], + }; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + + it('should format simple arrays', () => { + const schema = { + type: 'array', + items: { + eth: 'uint', + }, + }; + + const data = [10, 10]; + + const result = ['0xa', '0xa']; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + + it('should format simple tuple', () => { + const schema = { + type: 'array', + items: [ + { + eth: 'uint', + }, + { + eth: 'bytes', + }, + ], + }; + + const data = [10, Buffer.from('FF', 'hex')]; + + const result = ['0xa', '0xff']; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + + it('should format scalar value', () => { + const schema = { + eth: 'uint', + }; + + const data = 10; + + const result = '0xa'; + + expect(format(schema, data, { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX })).toEqual( + result, + ); + }); + }); +}); diff --git a/packages/web3-core/src/web3_config.ts b/packages/web3-core/src/web3_config.ts index fd3a2cb7a3c..a9d39342970 100644 --- a/packages/web3-core/src/web3_config.ts +++ b/packages/web3-core/src/web3_config.ts @@ -1,5 +1,5 @@ import { Web3EventEmitter } from 'web3-common'; -import { Numbers, HexString, toHex, ValidTypes, BlockNumberOrTag } from 'web3-utils'; +import { Numbers, HexString, toHex, BlockNumberOrTag } from 'web3-utils'; import { TransactionTypeParser } from './types'; // eslint-disable-next-line import/no-cycle import { TransactionBuilder } from './web3_context'; @@ -21,7 +21,6 @@ export interface Web3ConfigOptions { defaultChain: string; defaultHardfork: string; defaultCommon: Record | null; - defaultReturnType: ValidTypes; defaultTransactionType: Numbers; defaultMaxPriorityFeePerGas: Numbers; transactionBuilder?: TransactionBuilder; @@ -57,7 +56,6 @@ export abstract class Web3Config defaultHardfork: 'london', // TODO - Check if there is a default Common defaultCommon: null, - defaultReturnType: ValidTypes.HexString, defaultTransactionType: '0x0', defaultMaxPriorityFeePerGas: toHex(2500000000), transactionBuilder: undefined, @@ -260,16 +258,6 @@ export abstract class Web3Config this._config.defaultCommon = val; } - public get defaultReturnType() { - return this._config.defaultReturnType; - } - - public set defaultReturnType(val) { - this._triggerConfigChange('defaultReturnType', val); - - this._config.defaultReturnType = val; - } - public get defaultTransactionType() { return this._config.defaultTransactionType; } diff --git a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap index 2035ca7e5bb..d0b9a32f2ec 100644 --- a/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap +++ b/packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap @@ -11,7 +11,6 @@ Object { "defaultHardfork": "london", "defaultMaxPriorityFeePerGas": "0x9502f900", "defaultNetworkId": null, - "defaultReturnType": "HexString", "defaultTransactionType": "0x0", "handleRevert": false, "maxListenersWarningThreshold": 100, diff --git a/packages/web3-core/test/unit/web3_config.test.ts b/packages/web3-core/test/unit/web3_config.test.ts index 3256c973f88..bab51797d49 100644 --- a/packages/web3-core/test/unit/web3_config.test.ts +++ b/packages/web3-core/test/unit/web3_config.test.ts @@ -1,4 +1,4 @@ -import { toHex, ValidTypes } from 'web3-utils'; +import { toHex } from 'web3-utils'; import { Web3Config, Web3ConfigEvent } from '../../src/web3_config'; class MyConfigObject extends Web3Config {} @@ -11,7 +11,6 @@ const defaultConfig = { defaultNetworkId: null, defaultCommon: null, defaultHardfork: 'london', - defaultReturnType: ValidTypes.HexString, handleRevert: false, maxListenersWarningThreshold: 100, transactionBlockTimeout: 50, diff --git a/packages/web3-eth-contract/src/contract.ts b/packages/web3-eth-contract/src/contract.ts index 3dbf04a008c..3065067ac70 100644 --- a/packages/web3-eth-contract/src/contract.ts +++ b/packages/web3-eth-contract/src/contract.ts @@ -1,4 +1,6 @@ import { + DataFormat, + DEFAULT_RETURN_FORMAT, EthExecutionAPI, inputAddressFormatter, inputLogFormatter, @@ -28,7 +30,6 @@ import { Filter, HexString, toChecksumAddress, - ValidTypes, } from 'web3-utils'; import { validator } from 'web3-validator'; import { ALL_EVENTS_ABI } from './constants'; @@ -201,9 +202,9 @@ export class Contract return promiEvent; }, - estimateGas: async ( + estimateGas: async ( options?: PayableCallOptions, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) => { const modifiedOptions = { ...options }; delete modifiedOptions.to; @@ -211,7 +212,7 @@ export class Contract return this._contractMethodEstimateGas({ abi: abi as AbiFunctionFragment, params: args, - returnType, + returnFormat, options: modifiedOptions, contractOptions, }); @@ -220,13 +221,13 @@ export class Contract }; } - public async getPastEvents( + public async getPastEvents( eventName: keyof ContractEvents | 'allEvents', filter?: Omit, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { const formattedFilter = inputLogFormatter(filter ?? {}); - const logs = await getLogs(this, formattedFilter, returnType); + const logs = await getLogs(this, formattedFilter, returnFormat); const abi = eventName === 'allEvents' @@ -335,10 +336,12 @@ export class Contract this._contractMethodCall(abi, params, options, block), send: (options?: PayableCallOptions) => this._contractMethodSend(abi, params, options), - estimateGas: async ( + estimateGas: async < + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >( options?: PayableCallOptions, - returnType?: ReturnType, - ) => this._contractMethodEstimateGas({ abi, params, returnType, options }), + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, + ) => this._contractMethodEstimateGas({ abi, params, returnFormat, options }), encodeABI: () => encodeMethodABI(abi, params), } as unknown as PayableMethodObject< ContractMethod['Inputs'], @@ -352,10 +355,10 @@ export class Contract this._contractMethodCall(abi, params, options, block), send: (options?: NonPayableCallOptions) => this._contractMethodSend(abi, params, options), - estimateGas: async ( + estimateGas: async ( options?: NonPayableCallOptions, - returnType?: ReturnType, - ) => this._contractMethodEstimateGas({ abi, params, returnType, options }), + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, + ) => this._contractMethodEstimateGas({ abi, params, returnFormat, options }), encodeABI: () => encodeMethodABI(abi, params), } as unknown as NonPayableMethodObject< ContractMethod['Inputs'], @@ -398,17 +401,17 @@ export class Contract private async _contractMethodEstimateGas< Options extends PayableCallOptions | NonPayableCallOptions, - ReturnType extends ValidTypes = ValidTypes.HexString, + ReturnFormat extends DataFormat, >({ abi, params, - returnType, + returnFormat, options, contractOptions, }: { abi: AbiFunctionFragment; params: unknown[]; - returnType?: ReturnType; + returnFormat: ReturnFormat; options?: Options; contractOptions?: ContractOptions; }) { @@ -419,7 +422,7 @@ export class Contract contractOptions: contractOptions ?? this.options, }); - return estimateGas(this, tx, BlockTags.LATEST, returnType); + return estimateGas(this, tx, BlockTags.LATEST, returnFormat); } // eslint-disable-next-line class-methods-use-this diff --git a/packages/web3-eth-contract/src/types.ts b/packages/web3-eth-contract/src/types.ts index 351c6140348..2ad04fae304 100644 --- a/packages/web3-eth-contract/src/types.ts +++ b/packages/web3-eth-contract/src/types.ts @@ -1,8 +1,14 @@ -import { EthExecutionAPI, ReceiptInfo } from 'web3-common'; +import { + DataFormat, + DEFAULT_RETURN_FORMAT, + EthExecutionAPI, + FormatType, + ReceiptInfo, +} from 'web3-common'; import { SupportedProviders } from 'web3-core'; import { ContractAbi } from 'web3-eth-abi'; import { sendTransaction } from 'web3-eth'; -import { Address, BlockNumberOrTag, Bytes, Filter, HexString, Uint, ValidTypes } from 'web3-utils'; +import { Address, BlockNumberOrTag, Bytes, Filter, HexString, Numbers, Uint } from 'web3-utils'; export interface EventLog { event: string; @@ -63,10 +69,10 @@ export interface NonPayableMethodObject arguments: Inputs; call(tx?: NonPayableCallOptions, block?: BlockNumberOrTag): Promise; send(tx?: NonPayableCallOptions): ReturnType; - estimateGas( + estimateGas( options?: NonPayableCallOptions, - returnType?: ReturnType, - ): Promise; + returnFormat?: ReturnFormat, + ): Promise>; encodeABI(): string; } @@ -74,9 +80,9 @@ export interface PayableMethodObject { arguments: Inputs; call(tx?: PayableCallOptions, block?: BlockNumberOrTag): Promise; send(tx?: PayableCallOptions): ReturnType; - estimateGas( + estimateGas( options?: PayableCallOptions, - returnType?: ReturnType, - ): Promise; + returnFormat?: ReturnFormat, + ): Promise>; encodeABI(): HexString; } diff --git a/packages/web3-eth-ens/src/ens.ts b/packages/web3-eth-ens/src/ens.ts index bdf89cae008..b017201e692 100644 --- a/packages/web3-eth-ens/src/ens.ts +++ b/packages/web3-eth-ens/src/ens.ts @@ -7,6 +7,7 @@ import { EthExecutionAPI, ENSUnsupportedNetworkError, ENSNetworkNotSyncedError, + DEFAULT_RETURN_FORMAT, } from 'web3-common'; import { NonPayableCallOptions, TransactionReceipt, Contract } from 'web3-eth-contract'; import { RESOLVER } from './abi/resolver'; @@ -215,7 +216,7 @@ export class ENS extends Web3Context { public async checkNetwork() { const now = Date.now() / 1000; if (!this._lastSyncCheck || now - this._lastSyncCheck > 3600) { - const block = await getBlock(this, 'latest'); + const block = await getBlock(this, 'latest', false, DEFAULT_RETURN_FORMAT); const headAge = BigInt(now) - BigInt(block.timestamp); if (headAge > 3600) { @@ -228,7 +229,7 @@ export class ENS extends Web3Context { if (this._detectedAddress) { return this._detectedAddress; } - const networkType = await getId(this); // get the network from provider + const networkType = await getId(this, DEFAULT_RETURN_FORMAT); // get the network from provider const addr = registryAddresses[networkType]; if (typeof addr === 'undefined') { diff --git a/packages/web3-eth-personal/src/rpc_method_wrappers.ts b/packages/web3-eth-personal/src/rpc_method_wrappers.ts index 8579db96dee..431a3015002 100644 --- a/packages/web3-eth-personal/src/rpc_method_wrappers.ts +++ b/packages/web3-eth-personal/src/rpc_method_wrappers.ts @@ -1,12 +1,6 @@ +import { DEFAULT_RETURN_FORMAT } from 'web3-common'; import { formatTransaction, Transaction } from 'web3-eth'; -import { - Address, - HexString, - isHexStrict, - toChecksumAddress, - utf8ToHex, - ValidTypes, -} from 'web3-utils'; +import { Address, HexString, isHexStrict, toChecksumAddress, utf8ToHex } from 'web3-utils'; import { validator } from 'web3-validator'; import { getAccounts as rpcGetAccounts, @@ -67,7 +61,7 @@ export const sendTransaction = async ( tx: Transaction, passphrase: string, ) => { - const formattedTx = formatTransaction(tx, ValidTypes.HexString); + const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT); return rpcSendTransaction(requestManager, formattedTx, passphrase); }; @@ -77,7 +71,7 @@ export const signTransaction = async ( tx: Transaction, passphrase: string, ) => { - const formattedTx = formatTransaction(tx, ValidTypes.HexString); + const formattedTx = formatTransaction(tx, DEFAULT_RETURN_FORMAT); return rpcSignTransaction(requestManager, formattedTx, passphrase); }; diff --git a/packages/web3-eth-personal/test/unit/eth_personal.test.ts b/packages/web3-eth-personal/test/unit/eth_personal.test.ts index 00e9edce140..2e8941e6826 100644 --- a/packages/web3-eth-personal/test/unit/eth_personal.test.ts +++ b/packages/web3-eth-personal/test/unit/eth_personal.test.ts @@ -174,7 +174,10 @@ describe('EthPersonal', () => { await ethPersonal.sendTransaction(tx, 'password'); expect(eth.formatTransaction).toHaveBeenCalledTimes(1); - expect(eth.formatTransaction).toHaveBeenCalledWith(tx, 'HexString'); + expect(eth.formatTransaction).toHaveBeenCalledWith(tx, { + bytes: 'BYTES_HEX', + number: 'NUMBER_HEX', + }); }); }); @@ -204,7 +207,10 @@ describe('EthPersonal', () => { await ethPersonal.signTransaction(tx, 'password'); expect(eth.formatTransaction).toHaveBeenCalledTimes(1); - expect(eth.formatTransaction).toHaveBeenCalledWith(tx, 'HexString'); + expect(eth.formatTransaction).toHaveBeenCalledWith(tx, { + bytes: 'BYTES_HEX', + number: 'NUMBER_HEX', + }); }); }); diff --git a/packages/web3-eth/src/convertible_properties.ts b/packages/web3-eth/src/convertible_properties.ts deleted file mode 100644 index f836f165716..00000000000 --- a/packages/web3-eth/src/convertible_properties.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Block, FeeHistoryResult, ReceiptInfo, TransactionInfo } from 'web3-common'; - -export const convertibleBlockProperties: (keyof Block)[] = [ - 'difficulty', - 'number', - 'gasLimit', - 'gasUsed', - 'timestamp', - 'nonce', - 'totalDifficulty', - 'baseFeePerGas', - 'size', -]; - -// https://stackoverflow.com/a/49402091 -type KeysOfUnion = T extends T ? keyof T : never; -export const convertibleTransactionInfoProperties: KeysOfUnion[] = [ - 'blockNumber', - 'gas', - 'gasPrice', - 'type', - 'nonce', - 'transactionIndex', - 'value', - 'v', - 'maxFeePerGas', - 'maxPriorityFeePerGas', - 'yParity', - 'chainId', -]; - -export const convertibleReceiptInfoProperties: (keyof ReceiptInfo)[] = [ - 'transactionIndex', - 'blockNumber', - 'cumulativeGasUsed', - 'gasUsed', - 'status', - 'effectiveGasPrice', -]; - -export const convertibleFeeHistoryResultProperties: (keyof FeeHistoryResult)[] = [ - 'oldestBlock', - 'baseFeePerGas', -]; diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 95e20b4ce5f..b3442a1d6f4 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -1,30 +1,30 @@ // Disabling because returnTypes must be last param to match 1.x params /* eslint-disable default-param-last */ -import { EthExecutionAPI, PromiEvent, ReceiptInfo, TransactionWithSender } from 'web3-common'; +import { + DataFormat, + EthExecutionAPI, + format, + PromiEvent, + TransactionWithSender, + FMT_BYTES, + FMT_NUMBER, + ReceiptInfo, + DEFAULT_RETURN_FORMAT, +} from 'web3-common'; import { Web3Context } from 'web3-core'; import { Address, BlockNumberOrTag, - convertObjectPropertiesToValidType, - convertToValidType, Filter, - HexString, HexString32Bytes, HexStringBytes, hexToNumber, + Numbers, numberToHex, Uint, Uint256, - ValidReturnTypes, - ValidTypes, } from 'web3-utils'; import { isBlockTag, isHexString32Bytes, validator } from 'web3-validator'; -import { - convertibleBlockProperties, - convertibleFeeHistoryResultProperties, - convertibleReceiptInfoProperties, - convertibleTransactionInfoProperties, -} from './convertible_properties'; import { TransactionMissingReceiptOrBlockHashError, TransactionPollingTimeoutError, @@ -32,16 +32,26 @@ import { } from './errors'; import * as rpcMethods from './rpc_methods'; import { - BlockFormatted, + accountSchema, + blockSchema, + feeHistorySchema, + logSchema, + receiptInfoSchema, +} from './schemas'; +import { + AccountObject, + Block, + FeeHistory, + Log, SendSignedTransactionEvents, SendTransactionEvents, Transaction, TransactionCall, } from './types'; import { formatTransaction } from './utils/format_transaction'; -import { Web3EthExecutionAPI } from './web3_eth_execution_api'; // eslint-disable-next-line import/no-cycle import { getTransactionGasPricing } from './utils/get_transaction_gas_pricing'; +import { Web3EthExecutionAPI } from './web3_eth_execution_api'; export const getProtocolVersion = async (web3Context: Web3Context) => rpcMethods.getProtocolVersion(web3Context.requestManager); @@ -55,54 +65,42 @@ export const getCoinbase = async (web3Context: Web3Context) => export const isMining = async (web3Context: Web3Context) => rpcMethods.getMining(web3Context.requestManager); -export async function getHashRate( +export async function getHashRate( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getHashRate(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } -export async function getGasPrice( +export async function getGasPrice( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getGasPrice(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } -export async function getBlockNumber( +export async function getBlockNumber( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getBlockNumber(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } -export async function getBalance( +export async function getBalance( web3Context: Web3Context, address: Address, blockNumber: BlockNumberOrTag = web3Context.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getBalance(web3Context.requestManager, address, blockNumber); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } export const getStorageAt = async ( @@ -118,62 +116,49 @@ export const getCode = async ( blockNumber: BlockNumberOrTag = web3Context.defaultBlock, ) => rpcMethods.getCode(web3Context.requestManager, address, blockNumber); -export async function getBlock( +export async function getBlock( web3Context: Web3Context, block: HexString32Bytes | BlockNumberOrTag = web3Context.defaultBlock, hydrated = false, - returnType?: ReturnType, -): Promise> { + returnFormat: ReturnFormat, +) { const response = isHexString32Bytes(block) ? await rpcMethods.getBlockByHash(web3Context.requestManager, block, hydrated) : await rpcMethods.getBlockByNumber(web3Context.requestManager, block, hydrated); - // @ts-expect-error Having a problem determining if BlockFormatted is correct type - return convertObjectPropertiesToValidType( - response, - convertibleBlockProperties, - returnType ?? web3Context.defaultReturnType, - ); + return format(blockSchema, response as unknown as Block, returnFormat); } -export async function getBlockTransactionCount< - ReturnType extends ValidTypes = ValidTypes.HexString, ->( +export async function getBlockTransactionCount( web3Context: Web3Context, block: HexString32Bytes | BlockNumberOrTag = web3Context.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = isHexString32Bytes(block) ? await rpcMethods.getBlockTransactionCountByHash(web3Context.requestManager, block) : await rpcMethods.getBlockTransactionCountByNumber(web3Context.requestManager, block); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } -export async function getBlockUncleCount( +export async function getBlockUncleCount( web3Context: Web3Context, block: HexString32Bytes | BlockNumberOrTag = web3Context.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = isHexString32Bytes(block) ? await rpcMethods.getUncleCountByBlockHash(web3Context.requestManager, block) : await rpcMethods.getUncleCountByBlockNumber(web3Context.requestManager, block); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as Numbers, returnFormat); } -export async function getUncle( +export async function getUncle( web3Context: Web3Context, block: HexString32Bytes | BlockNumberOrTag = web3Context.defaultBlock, uncleIndex: Uint, - returnType?: ReturnType, -): Promise> { + returnFormat: ReturnFormat, +) { const response = isHexString32Bytes(block) ? await rpcMethods.getUncleByBlockHashAndIndex( web3Context.requestManager, @@ -186,18 +171,13 @@ export async function getUncle is correct type - return convertObjectPropertiesToValidType( - response, - convertibleBlockProperties, - returnType ?? web3Context.defaultReturnType, - ); + return format(blockSchema, response as unknown as Block, returnFormat); } -export async function getTransaction( +export async function getTransaction( web3Context: Web3Context, transactionHash: HexString32Bytes, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getTransactionByHash( web3Context.requestManager, @@ -206,21 +186,14 @@ export async function getTransaction( +export async function getTransactionFromBlock( web3Context: Web3Context, block: HexString32Bytes | BlockNumberOrTag | undefined, transactionIndex: Uint, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const blockOrDefault = block ?? web3Context.defaultBlock; const response = isHexString32Bytes(blockOrDefault) @@ -237,20 +210,13 @@ export async function getTransactionFromBlock( +export async function getTransactionReceipt( web3Context: Web3Context, transactionHash: HexString32Bytes, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getTransactionReceipt( web3Context.requestManager, @@ -259,18 +225,14 @@ export async function getTransactionReceipt( +export async function getTransactionCount( web3Context: Web3Context, address: Address, blockNumber: BlockNumberOrTag | undefined, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getTransactionCount( web3Context.requestManager, @@ -278,19 +240,17 @@ export async function getTransactionCount( +export async function getPendingTransactions( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat?: ReturnFormat, ) { const response = await rpcMethods.getPendingTransactions(web3Context.requestManager); + return response.map(transaction => - formatTransaction(transaction, returnType ?? web3Context.defaultReturnType), + formatTransaction(transaction as unknown as Transaction, returnFormat), ); } @@ -366,7 +326,7 @@ function watchTransactionForConfirmations< BigInt(hexToNumber(transactionReceipt.blockNumber)) + BigInt(confirmationNumber), ), false, - ValidTypes.Number, + DEFAULT_RETURN_FORMAT, ); if (nextBlock?.hash !== null) { @@ -387,7 +347,10 @@ export function sendTransaction( ignoreGasPricing: boolean; }, ): PromiEvent { - let _transaction = formatTransaction(transaction, ValidTypes.HexString); + let _transaction = formatTransaction(transaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }); const promiEvent = new PromiEvent(resolve => { // eslint-disable-next-line @typescript-eslint/no-misused-promises setImmediate(async () => { @@ -399,11 +362,10 @@ export function sendTransaction( ) { _transaction = { ..._transaction, - ...(await getTransactionGasPricing( - _transaction, - web3Context, - ValidTypes.HexString, - )), + ...(await getTransactionGasPricing(_transaction, web3Context, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + })), }; } @@ -507,7 +469,10 @@ export const signTransaction = async ( ) => rpcMethods.signTransaction( web3Context.requestManager, - formatTransaction(transaction, ValidTypes.HexString), + formatTransaction(transaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }), ); // TODO Decide what to do with transaction.to @@ -521,19 +486,19 @@ export const call = async ( return rpcMethods.call( web3Context.requestManager, - formatTransaction(transaction, ValidTypes.HexString) as TransactionCall, + formatTransaction(transaction, DEFAULT_RETURN_FORMAT), isBlockTag(blockNumber) ? blockNumber - : (convertToValidType(blockNumber, ValidTypes.HexString) as HexString), + : format({ eth: 'uint' }, blockNumber as Numbers, DEFAULT_RETURN_FORMAT), ); }; // TODO Missing param -export async function estimateGas( +export async function estimateGas( web3Context: Web3Context, transaction: Partial, blockNumber: BlockNumberOrTag = web3Context.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.estimateGas( web3Context.requestManager, @@ -541,18 +506,15 @@ export async function estimateGas( +export async function getFeeHistory( web3Context: Web3Context, blockCount: Uint, newestBlock: BlockNumberOrTag = web3Context.defaultBlock, rewardPercentiles: number[], - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getFeeHistory( web3Context.requestManager, @@ -561,31 +523,29 @@ export async function getFeeHistory( +export async function getChainId( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getChainId(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format( + { eth: 'uint' }, + // Response is number in hex formatted string + response as unknown as number, + returnFormat, + ); } -export async function getProof( +export async function getProof( web3Context: Web3Context, address: Address, storageKey: HexString32Bytes, blockNumber: BlockNumberOrTag = web3Context.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getProof( web3Context.requestManager, @@ -593,40 +553,23 @@ export async function getProof ({ - ...proof, - value: convertToValidType(proof.value, returnType ?? web3Context.defaultReturnType), - })), - }; + + return format(accountSchema, response as unknown as AccountObject, returnFormat); } -export async function getLogs( +export async function getLogs( web3Context: Web3Context, filter: Filter, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getLogs(web3Context.requestManager, filter); - const validReturnType = returnType ?? web3Context.defaultReturnType; const result = response.map(res => { if (typeof res === 'string') { return res; } - const { logIndex, transactionIndex, blockNumber } = res; - - return { - ...res, - logIndex: logIndex ? convertToValidType(logIndex, validReturnType) : undefined, - transactionIndex: transactionIndex - ? convertToValidType(transactionIndex, validReturnType) - : undefined, - blockNumber: blockNumber ? convertToValidType(blockNumber, validReturnType) : undefined, - }; + return format(logSchema, res as unknown as Log, returnFormat); }); return result; diff --git a/packages/web3-eth/src/schemas.ts b/packages/web3-eth/src/schemas.ts new file mode 100644 index 00000000000..51623afbd73 --- /dev/null +++ b/packages/web3-eth/src/schemas.ts @@ -0,0 +1,386 @@ +export const accessListItemSchema = { + type: 'object', + properties: { + address: { + eth: 'address', + }, + storageKeys: { + type: 'array', + items: { + eth: 'bytes32', + }, + }, + }, +}; + +export const accessListSchema = { + type: 'array', + items: { + ...accessListItemSchema, + }, +}; + +export const chainSchema = { + type: 'string', + enum: ['goerli', 'kovan', 'mainnet', 'rinkeby', 'ropsten', 'sepolia'], +}; + +export const hardforkSchema = { + type: 'string', + enum: [ + 'arrowGlacier', + 'berlin', + 'byzantium', + 'chainstart', + 'constantinople', + 'dao', + 'homestead', + 'istanbul', + 'london', + 'merge', + 'muirGlacier', + 'petersburg', + 'shanghai', + 'spuriousDragon', + 'tangerineWhistle', + ], +}; + +export const customChainSchema = { + type: 'object', + properties: { + name: { + eth: 'string', + }, + networkId: { + eth: 'uint', + }, + chainId: { + eth: 'uint', + }, + }, +}; + +export const transactionSchema = { + type: 'object', + properties: { + from: { + eth: 'address', + }, + to: { + eth: 'address', + }, + value: { + eth: 'uint', + }, + gas: { + eth: 'uint', + }, + gasPrice: { + eth: 'uint', + }, + type: { + eth: 'uint', + }, + maxFeePerGas: { + eth: 'uint', + }, + maxPriorityFeePerGas: { + eth: 'uint', + }, + accessList: { + ...accessListSchema, + }, + data: { + eth: 'bytes', + }, + input: { + eth: 'bytes', + }, + nonce: { + eth: 'uint', + }, + chain: { ...chainSchema }, + hardfork: { ...hardforkSchema }, + chainId: { + eth: 'uint', + }, + networkId: { + eth: 'uint', + }, + common: { + type: 'object', + properties: { + customChain: { ...customChainSchema }, + baseChain: { + ...chainSchema, + }, + hardfork: { + ...hardforkSchema, + }, + }, + }, + gasLimit: { + eth: 'uint', + }, + v: { + eth: 'uint', + }, + r: { + eth: 'bytes', + }, + s: { + eth: 'bytes', + }, + }, +}; + +export const transactionInfoSchema = { + type: 'object', + properties: { + blockHash: { + eth: 'bytes32', + }, + blockNumber: { + eth: 'uint', + }, + from: { + eth: 'address', + }, + hash: { + eth: 'bytes32', + }, + transactionIndex: { + eth: 'uint', + }, + }, +}; + +export const blockSchema = { + type: 'object', + properties: { + parentHash: { + eth: 'bytes32', + }, + sha3Uncles: { + eth: 'bytes32', + }, + miner: { + eth: 'bytes', + }, + stateRoot: { + eth: 'bytes32', + }, + transactionsRoot: { + eth: 'bytes32', + }, + receiptsRoot: { + eth: 'bytes32', + }, + logsBloom: { + eth: 'bytes256', + }, + difficulty: { + eth: 'uint', + }, + number: { + eth: 'uint', + }, + gasLimit: { + eth: 'uint', + }, + gasUsed: { + eth: 'uint', + }, + timestamp: { + eth: 'uint', + }, + extraData: { + eth: 'bytes', + }, + mixHash: { + eth: 'bytes32', + }, + nonce: { + eth: 'uint', + }, + totalDifficulty: { + eth: 'uint', + }, + baseFeePerGas: { + eth: 'uint', + }, + size: { + eth: 'uint', + }, + // TODO: This attribute can be array of hashes or transaction info + transactions: { + type: 'array', + items: { + ...transactionInfoSchema, + }, + }, + uncles: { + type: 'array', + items: { + eth: 'bytes32', + }, + }, + hash: { + eth: 'bytes32', + }, + }, +}; + +export const logSchema = { + type: 'object', + properties: { + removed: { + eth: 'bool', + }, + logIndex: { + eth: 'uint', + }, + transactionIndex: { + eth: 'uint', + }, + transactionHash: { + eth: 'bytes32', + }, + blockHash: { + eth: 'bytes32', + }, + blockNumber: { + eth: 'uint', + }, + address: { + eth: 'address', + }, + data: { + eth: 'bytes', + }, + topics: { + type: 'array', + items: { + eth: 'bytes32', + }, + }, + }, +}; + +export const receiptInfoSchema = { + type: 'object', + properties: { + transactionHash: { + eth: 'bytes32', + }, + transactionIndex: { + eth: 'uint', + }, + blockHash: { + eth: 'bytes32', + }, + blockNumber: { + eth: 'uint', + }, + from: { + eth: 'address', + }, + to: { + eth: 'address', + }, + cumulativeGasUsed: { + eth: 'uint', + }, + gasUsed: { + eth: 'uint', + }, + contractAddress: { + eth: 'address', + }, + logs: { + type: 'array', + items: { + ...logSchema, + }, + }, + logsBloom: { + eth: 'bytes', + }, + root: { + eth: 'bytes', + }, + status: { + eth: 'uint', + }, + }, +}; + +export const feeHistorySchema = { + type: 'object', + properties: { + oldestBlock: { + eth: 'uint', + }, + baseFeePerGas: { + eth: 'uint', + }, + reward: { + type: 'array', + items: { + type: 'array', + items: { + eth: 'uint', + }, + }, + }, + }, +}; + +export const storageProofSchema = { + type: 'object', + properties: { + key: { + eth: 'bytes32', + }, + value: { + eth: 'uint', + }, + proof: { + type: 'array', + items: { + eth: 'bytes32', + }, + }, + }, +}; + +export const accountSchema = { + type: 'object', + properties: { + balance: { + eth: 'uint', + }, + codeHash: { + eth: 'bytes32', + }, + nonce: { + eth: 'uint', + }, + storageHash: { + eth: 'bytes32', + }, + accountProof: { + type: 'array', + items: { + eth: 'bytes32', + }, + }, + storageProof: { + type: 'array', + items: { + ...storageProofSchema, + }, + }, + }, +}; diff --git a/packages/web3-eth/src/types.ts b/packages/web3-eth/src/types.ts index af0aa6b146b..253e8653e8e 100644 --- a/packages/web3-eth/src/types.ts +++ b/packages/web3-eth/src/types.ts @@ -1,25 +1,16 @@ import { AccessList, - Log, - ReceiptInfo, TransactionHash, - TransactionInfo, Uncles, + FMT_BYTES, + FMT_NUMBER, + FormatType, + ReceiptInfo as ReceiptInfoEthType, } from 'web3-common'; -import { - Address, - HexString, - HexString256Bytes, - HexString32Bytes, - HexStringBytes, - Numbers, - Uint, - ValidReturnTypes, - ValidTypes, -} from 'web3-utils'; +import { Address, Bytes, HexString32Bytes, HexStringBytes, Numbers } from 'web3-utils'; -export type chain = 'goerli' | 'kovan' | 'mainnet' | 'rinkeby' | 'ropsten' | 'sepolia'; -export type hardfork = +export type ValidChains = 'goerli' | 'kovan' | 'mainnet' | 'rinkeby' | 'ropsten' | 'sepolia'; +export type Hardfork = | 'arrowGlacier' | 'berlin' | 'byzantium' @@ -36,237 +27,151 @@ export type hardfork = | 'spuriousDragon' | 'tangerineWhistle'; -export interface CustomChain { +export interface Log { + readonly removed?: boolean; + readonly logIndex?: Numbers; + readonly transactionIndex?: Numbers; + readonly transactionHash?: Bytes | null; + readonly blockHash?: Bytes | null; + readonly blockNumber?: Numbers; + readonly address?: Address; + readonly data?: Bytes; + readonly topics?: Bytes[]; +} + +export interface ReceiptInfo { + readonly transactionHash: Bytes; + readonly transactionIndex: Numbers; + readonly blockHash: Bytes; + readonly blockNumber: Numbers; + readonly from: Address; + readonly to: Address; + readonly cumulativeGasUsed: Numbers; + readonly gasUsed: Numbers; + readonly effectiveGasPrice?: Numbers; + readonly contractAddress: Address | null; + readonly logs: Log[]; + readonly logsBloom: Bytes; + readonly root: Bytes; + readonly status: Numbers; +} + +export interface CustomChain { name?: string; - networkId: NumberType; - chainId: NumberType; + networkId: Numbers; + chainId: Numbers; } -export interface Common { - customChain: CustomChain; - baseChain?: chain; - hardfork?: hardfork; +export interface Common { + customChain: CustomChain; + baseChain?: ValidChains; + hardfork?: Hardfork; } -export interface Transaction { +export interface Transaction { + value?: Numbers; + accessList?: AccessList; + common?: Common; from?: Address; to?: Address | null; - value?: NumberType; - gas?: NumberType; - gasPrice?: NumberType; - type?: NumberType; - maxFeePerGas?: NumberType; - maxPriorityFeePerGas?: NumberType; - accessList?: AccessList; - data?: HexStringBytes; - input?: HexStringBytes; - nonce?: NumberType; - chain?: chain; - hardfork?: hardfork; - chainId?: NumberType; - networkId?: NumberType; - common?: Common; - gasLimit?: NumberType; - v?: NumberType; - r?: HexString; - s?: HexString; + gas?: Numbers; + gasPrice?: Numbers; + type?: Numbers; + maxFeePerGas?: Numbers; + maxPriorityFeePerGas?: Numbers; + data?: Bytes; + input?: Bytes; + nonce?: Numbers; + chain?: ValidChains; + hardfork?: Hardfork; + chainId?: Numbers; + networkId?: Numbers; + gasLimit?: Numbers; + v?: Numbers; + r?: Bytes; + s?: Bytes; +} + +export interface TransactionInfo extends Transaction { + readonly blockHash: Bytes | null; + readonly blockNumber: Numbers | null; + readonly from: Address; + readonly hash: Bytes; + readonly transactionIndex: Numbers | null; } -export interface TransactionCall - extends Transaction { +export type InternalTransaction = FormatType< + Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } +>; + +export interface TransactionCall extends Transaction { to: Address; } -export interface PopulatedUnsignedBaseTransaction { +export interface PopulatedUnsignedBaseTransaction { from: Address; to?: Address; value: Numbers; gas?: Numbers; gasPrice: Numbers; type: Numbers; - data: HexStringBytes; + data: Bytes; nonce: Numbers; networkId: Numbers; - chain: chain; - hardfork: hardfork; + chain: ValidChains; + hardfork: Hardfork; chainId: Numbers; - common: Common; + common: Common; gasLimit: Numbers; } -export interface PopulatedUnsignedEip2930Transaction - extends PopulatedUnsignedBaseTransaction { - accessList: AccessList; -} -export interface PopulatedUnsignedEip1559Transaction - extends PopulatedUnsignedEip2930Transaction { - gasPrice: never; - maxFeePerGas: NumberType; - maxPriorityFeePerGas: NumberType; -} -export type PopulatedUnsignedTransaction = - | PopulatedUnsignedBaseTransaction - | PopulatedUnsignedEip2930Transaction - | PopulatedUnsignedEip1559Transaction; - -interface BaseTransactionFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> { - readonly to?: Address | null; - readonly type: ReturnType; - readonly nonce: ReturnType; - readonly gas: ReturnType; - readonly value: ReturnType; - readonly input: HexStringBytes; - readonly chainId?: ReturnType; -} - -interface Transaction1559UnsignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends BaseTransactionFormatted { - readonly maxFeePerGas: ReturnType; - readonly maxPriorityFeePerGas: ReturnType; - readonly accessList: AccessList; -} - -interface Transaction1559SignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends Transaction1559UnsignedFormatted { - readonly yParity: ReturnType; - readonly r: ReturnType; - readonly s: ReturnType; -} - -interface Transaction2930UnsignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends BaseTransactionFormatted { - readonly gasPrice: ReturnType; - readonly accessList: AccessList; -} - -interface Transaction2930SignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends Transaction2930UnsignedFormatted { - readonly yParity: ReturnType; - readonly r: ReturnType; - readonly s: ReturnType; -} - -interface TransactionLegacyUnsignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends BaseTransactionFormatted { - readonly gasPrice: ReturnType; -} - -interface TransactionLegacySignedFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> extends TransactionLegacyUnsignedFormatted { - readonly v: ReturnType; - readonly r: Uint; - readonly s: Uint; -} - -type TransactionSignedFormatted = - | Transaction1559SignedFormatted - | Transaction2930SignedFormatted - | TransactionLegacySignedFormatted; - -export type TransactionInfoFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> = TransactionSignedFormatted & { - readonly blockHash: HexString32Bytes | null; - readonly blockNumber: ReturnType | null; - readonly from: Address; - readonly hash: HexString32Bytes; - readonly transactionIndex: ReturnType | null; -}; -export interface ReceiptInfoFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> { - readonly transactionHash: HexString32Bytes; - readonly transactionIndex: ReturnType; - readonly blockHash: HexString32Bytes; - readonly blockNumber: ReturnType; - readonly from: Address; - readonly to: Address; - readonly cumulativeGasUsed: ReturnType; - readonly gasUsed: ReturnType; - readonly contractAddress: Address | null; - readonly logs: Log[]; - readonly logsBloom: HexString256Bytes; - readonly root: HexString32Bytes; - readonly status: ReturnType; - readonly effectiveGasPrice: ReturnType; +export interface PopulatedUnsignedEip2930Transaction extends PopulatedUnsignedBaseTransaction { + accessList: AccessList; } -export interface BlockFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> { - readonly parentHash: HexString32Bytes; - readonly sha3Uncles: HexString32Bytes; - readonly miner: HexString; - readonly stateRoot: HexString32Bytes; - readonly transactionsRoot: HexString32Bytes; - readonly receiptsRoot: HexString32Bytes; - readonly logsBloom: HexString256Bytes | null; - readonly difficulty?: ReturnType; - readonly number: ReturnType; - readonly gasLimit: ReturnType; - readonly gasUsed: ReturnType; - readonly timestamp: ReturnType; - readonly extraData: HexStringBytes; - readonly mixHash: HexString32Bytes; - readonly nonce: ReturnType; - readonly totalDifficulty: ReturnType; - readonly baseFeePerGas?: ReturnType; - readonly size: ReturnType; +export interface PopulatedUnsignedEip1559Transaction extends PopulatedUnsignedEip2930Transaction { + gasPrice: never; + maxFeePerGas: Numbers; + maxPriorityFeePerGas: Numbers; +} +export type PopulatedUnsignedTransaction = + | PopulatedUnsignedBaseTransaction + | PopulatedUnsignedEip2930Transaction + | PopulatedUnsignedEip1559Transaction; + +export interface Block { + readonly parentHash: Bytes; + readonly sha3Uncles: Bytes; + readonly miner: Bytes; + readonly stateRoot: Bytes; + readonly transactionsRoot: Bytes; + readonly receiptsRoot: Bytes; + readonly logsBloom: Bytes | null; + readonly difficulty?: Numbers; + readonly number: Numbers; + readonly gasLimit: Numbers; + readonly gasUsed: Numbers; + readonly timestamp: Numbers; + readonly extraData: Bytes; + readonly mixHash: Bytes; + readonly nonce: Numbers; + readonly totalDifficulty: Numbers; + readonly baseFeePerGas?: Numbers; + readonly size: Numbers; readonly transactions: TransactionHash[] | TransactionInfo[]; readonly uncles: Uncles; - readonly hash: HexString32Bytes | null; -} - -export interface FeeHistoryResultFormatted< - DesiredType extends ValidTypes = ValidTypes.HexString, - ReturnType = ValidReturnTypes[DesiredType], -> { - readonly oldestBlock: ReturnType; - readonly baseFeePerGas: ReturnType; - readonly reward: number[][]; -} - -export interface StorageProofFormatted { - readonly key: HexString32Bytes; - readonly value: NumberType; - readonly proof: HexString32Bytes[]; -} - -export interface AccountObjectFormatted { - readonly balance: NumberType; - readonly codeHash: HexString32Bytes; - readonly nonce: NumberType; - readonly storageHash: HexString32Bytes; - readonly accountProof: HexString32Bytes[]; - readonly storageProof: StorageProofFormatted[]; + readonly hash: Bytes | null; } export type SendTransactionEvents = { - sending: Transaction; - sent: Transaction; + sending: InternalTransaction; + sent: InternalTransaction; transactionHash: HexString32Bytes; - receipt: ReceiptInfo; + receipt: ReceiptInfoEthType; confirmation: { confirmationNumber: number; - receipt: ReceiptInfo; + receipt: ReceiptInfoEthType; latestBlockHash: HexString32Bytes; }; }; @@ -275,10 +180,31 @@ export type SendSignedTransactionEvents = { sending: HexStringBytes; sent: HexStringBytes; transactionHash: HexString32Bytes; - receipt: ReceiptInfo; + receipt: ReceiptInfoEthType; confirmation: { confirmationNumber: number; - receipt: ReceiptInfo; + receipt: ReceiptInfoEthType; latestBlockHash: HexString32Bytes; }; }; + +export interface FeeHistory { + readonly oldestBlock: Numbers; + readonly baseFeePerGas: Numbers; + readonly reward: Numbers[][]; +} + +export interface StorageProof { + readonly key: Bytes; + readonly value: Numbers; + readonly proof: Bytes[]; +} + +export interface AccountObject { + readonly balance: Numbers; + readonly codeHash: Bytes; + readonly nonce: Numbers; + readonly storageHash: Bytes; + readonly accountProof: Bytes[]; + readonly storageProof: StorageProof[]; +} diff --git a/packages/web3-eth/src/utils/detect_transaction_type.ts b/packages/web3-eth/src/utils/detect_transaction_type.ts index b0a4c39db04..a00b0419820 100644 --- a/packages/web3-eth/src/utils/detect_transaction_type.ts +++ b/packages/web3-eth/src/utils/detect_transaction_type.ts @@ -1,13 +1,11 @@ -import { EthExecutionAPI } from 'web3-common'; +import { EthExecutionAPI, DEFAULT_RETURN_FORMAT, format } from 'web3-common'; import { TransactionTypeParser, Web3Context } from 'web3-core'; -import { convertToValidType, HexString, ValidTypes } from 'web3-utils'; -import { Transaction } from '../types'; +import { InternalTransaction, Transaction } from '../types'; export const defaultTransactionTypeParser: TransactionTypeParser = transaction => { const tx = transaction as unknown as Transaction; - if (tx.type !== undefined) - return convertToValidType(tx.type, ValidTypes.HexString) as HexString; + if (tx.type !== undefined) return format({ eth: 'uint' }, tx.type, DEFAULT_RETURN_FORMAT); if ( tx.maxFeePerGas !== undefined || @@ -24,7 +22,7 @@ export const defaultTransactionTypeParser: TransactionTypeParser = transaction = }; export const detectTransactionType = ( - transaction: Transaction, + transaction: InternalTransaction, web3Context?: Web3Context, ) => (web3Context?.transactionTypeParser ?? defaultTransactionTypeParser)( diff --git a/packages/web3-eth/src/utils/format_transaction.ts b/packages/web3-eth/src/utils/format_transaction.ts index 693d27ef981..39178219987 100644 --- a/packages/web3-eth/src/utils/format_transaction.ts +++ b/packages/web3-eth/src/utils/format_transaction.ts @@ -1,71 +1,34 @@ -import { convertToValidType, Numbers, ValidReturnTypes, ValidTypes, mergeDeep } from 'web3-utils'; +import { DataFormat, DEFAULT_RETURN_FORMAT, format, FormatType } from 'web3-common'; +import { bytesToBuffer, mergeDeep } from 'web3-utils'; import { TransactionDataAndInputError } from '../errors'; +import { transactionSchema } from '../schemas'; import { Transaction } from '../types'; export function formatTransaction< - DesiredType extends ValidTypes, - NumberType extends ValidReturnTypes[DesiredType] = ValidReturnTypes[DesiredType], ->(transaction: Transaction, desiredType: DesiredType): Transaction { - const formattedTransaction = mergeDeep( - {}, - transaction as Record, - ) as Transaction; + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + TransactionType extends Transaction = Transaction, +>( + transaction: TransactionType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, +): FormatType { + let formattedTransaction = mergeDeep({}, transaction as Record) as Transaction; if (transaction.common !== undefined) { formattedTransaction.common = { ...transaction.common }; if (transaction.common.customChain !== undefined) formattedTransaction.common.customChain = { ...transaction.common.customChain }; } - const formattableProperties: (keyof Transaction)[] = [ - 'value', - 'gas', - 'gasPrice', - 'type', - 'maxFeePerGas', - 'maxPriorityFeePerGas', - 'nonce', - 'chainId', - 'gasLimit', - 'v', - ]; - for (const transactionProperty of formattableProperties) { - const formattedProperty = formattedTransaction[transactionProperty]; - if ( - formattedProperty !== undefined && - formattedProperty !== null && - typeof formattedProperty !== 'object' && - !Array.isArray(formattedProperty) - ) { - if (transactionProperty === 'value' && formattedProperty === '0x') continue; - (formattedTransaction[transactionProperty] as Numbers) = convertToValidType( - formattedProperty, - desiredType, - ); - } - } - - if (formattedTransaction.common?.customChain !== undefined) { - if (formattedTransaction.common.customChain.networkId !== undefined) - formattedTransaction.common.customChain.networkId = convertToValidType( - formattedTransaction.common.customChain.networkId, - desiredType, - ); - if (formattedTransaction.common.customChain.chainId !== undefined) - formattedTransaction.common.customChain.chainId = convertToValidType( - formattedTransaction.common.customChain.chainId, - desiredType, - ); - } + formattedTransaction = format(transactionSchema, formattedTransaction, returnFormat); if (formattedTransaction.data !== undefined && formattedTransaction.input !== undefined) throw new TransactionDataAndInputError({ - data: formattedTransaction.data, - input: formattedTransaction.input, + data: bytesToBuffer(formattedTransaction.data).toString('hex'), + input: bytesToBuffer(formattedTransaction.input).toString('hex'), }); else if (formattedTransaction.input !== undefined) { formattedTransaction.data = formattedTransaction.input; delete formattedTransaction.input; } - return formattedTransaction as Transaction; + return formattedTransaction as FormatType; } diff --git a/packages/web3-eth/src/utils/get_transaction_gas_pricing.ts b/packages/web3-eth/src/utils/get_transaction_gas_pricing.ts index 10929f2bf2e..38568686934 100644 --- a/packages/web3-eth/src/utils/get_transaction_gas_pricing.ts +++ b/packages/web3-eth/src/utils/get_transaction_gas_pricing.ts @@ -1,61 +1,69 @@ -import { EthExecutionAPI } from 'web3-common'; +import { + EthExecutionAPI, + FormatType, + DataFormat, + format, + DEFAULT_RETURN_FORMAT, +} from 'web3-common'; import { Web3Context } from 'web3-core'; -import { convertToValidType, ValidReturnTypes, ValidTypes } from 'web3-utils'; +import { Numbers } from 'web3-utils'; import { Eip1559NotSupportedError, UnsupportedTransactionTypeError } from '../errors'; // eslint-disable-next-line import/no-cycle import { getBlock, getGasPrice } from '../rpc_method_wrappers'; -import { Transaction } from '../types'; +import { InternalTransaction, Transaction } from '../types'; // eslint-disable-next-line import/no-cycle import { getTransactionType } from './transaction_builder'; -async function getEip1559GasPricing( - transaction: Transaction, +async function getEip1559GasPricing( + transaction: FormatType, web3Context: Web3Context, - returnType?: ReturnType, -): Promise<{ - maxPriorityFeePerGas: ValidReturnTypes[ReturnType]; - maxFeePerGas: ValidReturnTypes[ReturnType]; -}> { - // Unless otherwise specified by web3Context.defaultBlock, this defaults to latest - const block = await getBlock(web3Context); + returnFormat: ReturnFormat, +): Promise> { + const block = await getBlock(web3Context, web3Context.defaultBlock, false, returnFormat); if (block.baseFeePerGas === undefined) throw new Eip1559NotSupportedError(); if (transaction.gasPrice !== undefined) { - const convertedTransactionGasPrice = convertToValidType( - transaction.gasPrice, - returnType ?? web3Context.defaultReturnType, + const convertedTransactionGasPrice = format( + { eth: 'uint' }, + transaction.gasPrice as Numbers, + returnFormat, ); + return { maxPriorityFeePerGas: convertedTransactionGasPrice, maxFeePerGas: convertedTransactionGasPrice, - } as { - maxPriorityFeePerGas: ValidReturnTypes[ReturnType]; - maxFeePerGas: ValidReturnTypes[ReturnType]; }; } return { - maxPriorityFeePerGas: convertToValidType( + maxPriorityFeePerGas: format( + { eth: 'uint' }, transaction.maxPriorityFeePerGas ?? web3Context.defaultMaxPriorityFeePerGas, - returnType ?? web3Context.defaultReturnType, + returnFormat, ), - maxFeePerGas: convertToValidType( - transaction.maxFeePerGas ?? + maxFeePerGas: format( + { eth: 'uint' }, + (transaction.maxFeePerGas ?? BigInt(block.baseFeePerGas) * BigInt(2) + BigInt( transaction.maxPriorityFeePerGas ?? web3Context.defaultMaxPriorityFeePerGas, - ), - returnType ?? web3Context.defaultReturnType, + )) as Numbers, + returnFormat, ), - } as { - maxPriorityFeePerGas: ValidReturnTypes[ReturnType]; - maxFeePerGas: ValidReturnTypes[ReturnType]; }; } -export async function getTransactionGasPricing< - ReturnType extends ValidTypes = ValidTypes.HexString, ->(transaction: Transaction, web3Context: Web3Context, returnType?: ReturnType) { +export async function getTransactionGasPricing( + transaction: InternalTransaction, + web3Context: Web3Context, + returnFormat: ReturnFormat, +): Promise< + | FormatType< + { gasPrice?: Numbers; maxPriorityFeePerGas?: Numbers; maxFeePerGas?: Numbers }, + ReturnFormat + > + | undefined +> { const transactionType = getTransactionType(transaction, web3Context); if (transactionType !== undefined) { if (transactionType.startsWith('-')) @@ -70,22 +78,15 @@ export async function getTransactionGasPricing< (transactionType === '0x0' || transactionType === '0x1') ) return { - gasPrice: await getGasPrice( - web3Context, - returnType ?? web3Context.defaultReturnType, - ), + gasPrice: await getGasPrice(web3Context, returnFormat), maxPriorityFeePerGas: undefined, maxFeePerGas: undefined, - } as { - gasPrice: ValidReturnTypes[ReturnType]; - maxPriorityFeePerGas: undefined; - maxFeePerGas: undefined; }; if (transactionType === '0x2') { return { gasPrice: undefined, - ...(await getEip1559GasPricing(transaction, web3Context, returnType)), + ...(await getEip1559GasPricing(transaction, web3Context, returnFormat)), }; } } diff --git a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts index e3394b25a46..870f662f551 100644 --- a/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/src/utils/prepare_transaction_for_signing.ts @@ -1,37 +1,57 @@ import Common from '@ethereumjs/common'; import { TransactionFactory, TxOptions } from '@ethereumjs/tx'; -import { EthExecutionAPI } from 'web3-common'; +import { EthExecutionAPI, FMT_BYTES, FMT_NUMBER, FormatType } from 'web3-common'; import { Web3Context } from 'web3-core'; -import { HexString, toNumber, ValidTypes } from 'web3-utils'; -import { formatTransaction } from './format_transaction'; +import { HexString, toNumber } from 'web3-utils'; import { PopulatedUnsignedEip1559Transaction, PopulatedUnsignedEip2930Transaction, PopulatedUnsignedTransaction, Transaction, } from '../types'; -import { transactionBuilder } from './transaction_builder'; import { validateTransactionForSigning } from '../validation'; +import { formatTransaction } from './format_transaction'; +import { transactionBuilder } from './transaction_builder'; const getEthereumjsTxDataFromTransaction = ( - transaction: PopulatedUnsignedTransaction, + transaction: FormatType< + PopulatedUnsignedTransaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + >, ) => ({ - nonce: transaction.nonce as HexString, - gasPrice: transaction.gasPrice as HexString, - gasLimit: transaction.gasLimit as HexString, - to: transaction.to as HexString, - value: transaction.value as HexString, + nonce: transaction.nonce, + gasPrice: transaction.gasPrice, + gasLimit: transaction.gasLimit, + to: transaction.to, + value: transaction.value, data: transaction.data, - type: transaction.type as HexString, - chainId: transaction.chainId as HexString, - accessList: (transaction as PopulatedUnsignedEip2930Transaction).accessList, - maxPriorityFeePerGas: (transaction as PopulatedUnsignedEip1559Transaction) - .maxPriorityFeePerGas as HexString, - maxFeePerGas: (transaction as PopulatedUnsignedEip1559Transaction).maxFeePerGas as HexString, + type: transaction.type, + chainId: transaction.chainId, + accessList: ( + transaction as FormatType< + PopulatedUnsignedEip2930Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + > + ).accessList, + maxPriorityFeePerGas: ( + transaction as FormatType< + PopulatedUnsignedEip1559Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + > + ).maxPriorityFeePerGas, + maxFeePerGas: ( + transaction as FormatType< + PopulatedUnsignedEip1559Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + > + ).maxFeePerGas, }); const getEthereumjsTransactionOptions = ( - transaction: PopulatedUnsignedTransaction, + transaction: FormatType< + PopulatedUnsignedTransaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + >, web3Context: Web3Context, ) => { const hasTransactionSigningOptions = @@ -75,14 +95,26 @@ export const prepareTransactionForSigning = async ( web3Context: Web3Context, privateKey?: HexString | Buffer, ) => { - const populatedTransaction = await transactionBuilder({ transaction, web3Context, privateKey }); + const populatedTransaction = (await transactionBuilder({ + transaction, + web3Context, + privateKey, + })) as unknown as PopulatedUnsignedTransaction; - const formattedTransaction = formatTransaction( - populatedTransaction, - ValidTypes.HexString, - ) as PopulatedUnsignedTransaction; + const formattedTransaction = formatTransaction(populatedTransaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }) as unknown as FormatType< + PopulatedUnsignedTransaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + >; - validateTransactionForSigning(formattedTransaction); + validateTransactionForSigning( + formattedTransaction as unknown as FormatType< + Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } + >, + ); return TransactionFactory.fromTxData( getEthereumjsTxDataFromTransaction(formattedTransaction), diff --git a/packages/web3-eth/src/utils/transaction_builder.ts b/packages/web3-eth/src/utils/transaction_builder.ts index 72baba18360..1846c2d661c 100644 --- a/packages/web3-eth/src/utils/transaction_builder.ts +++ b/packages/web3-eth/src/utils/transaction_builder.ts @@ -1,13 +1,12 @@ -import { EthExecutionAPI } from 'web3-common'; +import { EthExecutionAPI, DEFAULT_RETURN_FORMAT, FormatType, format } from 'web3-common'; import { Web3Context } from 'web3-core'; import { privateKeyToAddress } from 'web3-eth-accounts'; -import { Address, convertToValidType, HexString, ValidTypes } from 'web3-utils'; import { getId, Web3NetAPI } from 'web3-net'; - +import { Address, HexString } from 'web3-utils'; import { TransactionDataAndInputError, UnableToPopulateNonceError } from '../errors'; // eslint-disable-next-line import/no-cycle import { getChainId, getTransactionCount } from '../rpc_method_wrappers'; -import { chain, hardfork, Transaction } from '../types'; +import { ValidChains, Hardfork, Transaction, InternalTransaction } from '../types'; import { detectTransactionType } from './detect_transaction_type'; // eslint-disable-next-line import/no-cycle import { getTransactionGasPricing } from './get_transaction_gas_pricing'; @@ -31,11 +30,17 @@ export const getTransactionNonce = async ( // TODO if (web3.eth.accounts.wallet) use address from local wallet throw new UnableToPopulateNonceError(); } - return getTransactionCount(web3Context, address, web3Context.defaultBlock); + + return getTransactionCount( + web3Context, + address, + web3Context.defaultBlock, + DEFAULT_RETURN_FORMAT, + ); }; export const getTransactionType = ( - transaction: Transaction, + transaction: FormatType, web3Context: Web3Context, ) => { const inferredType = detectTransactionType(transaction, web3Context); @@ -45,10 +50,7 @@ export const getTransactionType = ( web3Context.defaultTransactionType !== null || web3Context.defaultTransactionType !== undefined ) - return convertToValidType( - web3Context.defaultTransactionType, - ValidTypes.HexString, - ) as HexString; + return format({ eth: 'uint' }, web3Context.defaultTransactionType, DEFAULT_RETURN_FORMAT); return undefined; }; @@ -60,25 +62,30 @@ export async function defaultTransactionBuilder; privateKey?: HexString | Buffer; }): Promise { - let populatedTransaction = { ...options.transaction } as unknown as Transaction; + let populatedTransaction = { ...options.transaction } as unknown as InternalTransaction; - if (populatedTransaction.from === undefined) + if (populatedTransaction.from === undefined) { populatedTransaction.from = getTransactionFromAttr(options.web3Context, options.privateKey); + } - if (populatedTransaction.nonce === undefined) - populatedTransaction.nonce = await getTransactionNonce( + // TODO: Debug why need to typecase getTransactionNonce + if (populatedTransaction.nonce === undefined) { + populatedTransaction.nonce = (await getTransactionNonce( options.web3Context, populatedTransaction.from, - ); + )) as unknown as string; + } - if (populatedTransaction.value === undefined) populatedTransaction.value = '0x'; + if (populatedTransaction.value === undefined) { + populatedTransaction.value = '0x'; + } - if (populatedTransaction.data !== undefined && populatedTransaction.input !== undefined) + if (populatedTransaction.data !== undefined && populatedTransaction.input !== undefined) { throw new TransactionDataAndInputError({ data: populatedTransaction.data, input: populatedTransaction.input, }); - else if (populatedTransaction.input !== undefined) { + } else if (populatedTransaction.input !== undefined) { populatedTransaction.data = populatedTransaction.input; delete populatedTransaction.input; } @@ -87,57 +94,66 @@ export async function defaultTransactionBuilder>(options: { transaction: Transaction; web3Context: Web3Context; privateKey?: HexString | Buffer; + // eslint-disable-next-line @typescript-eslint/require-await }) => (options.web3Context.transactionBuilder ?? defaultTransactionBuilder)({ ...options, transaction: options.transaction as unknown as Record, - }); + }) as unknown as ReturnType; diff --git a/packages/web3-eth/src/validation.ts b/packages/web3-eth/src/validation.ts index e77ea13af9e..35f0d71c15c 100644 --- a/packages/web3-eth/src/validation.ts +++ b/packages/web3-eth/src/validation.ts @@ -2,13 +2,14 @@ import { AccessList, AccessListEntry, BaseTransaction, + FMT_BYTES, + FMT_NUMBER, Transaction1559Unsigned, Transaction2930Unsigned, TransactionCall, TransactionLegacyUnsigned, TransactionWithSender, } from 'web3-common'; -import { HexString, ValidTypes } from 'web3-utils'; import { isAddress, isHexStrict, isHexString32Bytes, isUInt } from 'web3-validator'; import { ChainIdMismatchError, @@ -28,7 +29,7 @@ import { UnsupportedFeeMarketError, } from './errors'; import { formatTransaction } from './utils/format_transaction'; -import { Transaction } from './types'; +import { InternalTransaction, Transaction } from './types'; export function isBaseTransaction(value: BaseTransaction): boolean { if (value.to !== undefined && value?.to !== null && !isAddress(value.to)) return false; @@ -123,7 +124,7 @@ export function validateTransactionCall(value: TransactionCall) { if (!isTransactionCall(value)) throw new InvalidTransactionCall(value); } -export const validateCustomChainInfo = (transaction: Transaction) => { +export const validateCustomChainInfo = (transaction: InternalTransaction) => { if (transaction.common !== undefined) { if (transaction.common.customChain === undefined) throw new MissingCustomChainError(); if (transaction.common.customChain.chainId === undefined) @@ -139,7 +140,7 @@ export const validateCustomChainInfo = (transaction: Transaction) => { } }; -export const validateChainInfo = (transaction: Transaction) => { +export const validateChainInfo = (transaction: InternalTransaction) => { if ( transaction.common !== undefined && transaction.chain !== undefined && @@ -157,7 +158,7 @@ export const validateChainInfo = (transaction: Transaction) => { }); }; -export const validateLegacyGas = (transaction: Transaction) => { +export const validateLegacyGas = (transaction: InternalTransaction) => { if ( // This check is verifying gas and gasPrice aren't less than 0. transaction.gas === undefined || @@ -176,7 +177,7 @@ export const validateLegacyGas = (transaction: Transaction) => { }); }; -export const validateFeeMarketGas = (transaction: Transaction) => { +export const validateFeeMarketGas = (transaction: InternalTransaction) => { // These errors come from 1.x, so they must be checked before // InvalidMaxPriorityFeePerGasOrMaxFeePerGas to throw the same error // for the same code executing in 1.x @@ -204,7 +205,7 @@ export const validateFeeMarketGas = (transaction: Transaction) => { * This method checks if all required gas properties are present for either * legacy gas (type 0x0 and 0x1) OR fee market transactions (0x2) */ -export const validateGas = (transaction: Transaction) => { +export const validateGas = (transaction: InternalTransaction) => { const gasPresent = transaction.gas !== undefined || transaction.gasLimit !== undefined; const legacyGasPresent = gasPresent && transaction.gasPrice !== undefined; const feeMarketGasPresent = @@ -237,8 +238,8 @@ export const validateGas = (transaction: Transaction) => { }; export const validateTransactionForSigning = ( - transaction: Transaction, - overrideMethod?: (transaction: Transaction) => void, + transaction: InternalTransaction, + overrideMethod?: (transaction: InternalTransaction) => void, ) => { if (overrideMethod !== undefined) { overrideMethod(transaction); @@ -251,7 +252,10 @@ export const validateTransactionForSigning = ( validateCustomChainInfo(transaction); validateChainInfo(transaction); - const formattedTransaction = formatTransaction(transaction, ValidTypes.HexString); + const formattedTransaction = formatTransaction(transaction as Transaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }); validateGas(formattedTransaction); if ( diff --git a/packages/web3-eth/src/web3_eth.ts b/packages/web3-eth/src/web3_eth.ts index a0cd102ae31..173a48d696f 100644 --- a/packages/web3-eth/src/web3_eth.ts +++ b/packages/web3-eth/src/web3_eth.ts @@ -1,23 +1,20 @@ // Disabling because returnTypes must be last param to match 1.x params /* eslint-disable default-param-last */ - -import { TransactionWithSender } from 'web3-common'; +import { DataFormat, DEFAULT_RETURN_FORMAT, TransactionWithSender } from 'web3-common'; import { Web3Context } from 'web3-core'; import { - BlockNumberOrTag, - ValidTypes, Address, - Uint256, + BlockNumberOrTag, + Filter, HexString32Bytes, + HexString8Bytes, HexStringBytes, Uint, - HexString8Bytes, - Filter, + Uint256, } from 'web3-utils'; - -import { BlockFormatted, Transaction, TransactionCall } from './types'; import * as rpcMethods from './rpc_methods'; import * as rpcMethodsWrappers from './rpc_method_wrappers'; +import { Transaction, TransactionCall } from './types'; import { Web3EthExecutionAPI } from './web3_eth_execution_api'; export class Web3Eth extends Web3Context { @@ -37,34 +34,34 @@ export class Web3Eth extends Web3Context { return rpcMethods.getMining(this.requestManager); } - public async getHashRate( - returnType?: ReturnType, + public async getHashRate( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getHashRate(this, returnType); + return rpcMethodsWrappers.getHashRate(this, returnFormat); } - public async getGasPrice( - returnType?: ReturnType, + public async getGasPrice( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getGasPrice(this, returnType); + return rpcMethodsWrappers.getGasPrice(this, returnFormat); } public async getAccounts() { return rpcMethods.getAccounts(this.requestManager); } - public async getBlockNumber( - returnType?: ReturnType, + public async getBlockNumber( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getBlockNumber(this, returnType); + return rpcMethodsWrappers.getBlockNumber(this, returnFormat); } - public async getBalance( + public async getBalance( address: Address, blockNumber: BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getBalance(this, address, blockNumber, returnType); + return rpcMethodsWrappers.getBalance(this, address, blockNumber, returnFormat); } public async getStorageAt( @@ -79,75 +76,83 @@ export class Web3Eth extends Web3Context { return rpcMethods.getCode(this.requestManager, address, blockNumber); } - public async getBlock( + public async getBlock( block: HexString32Bytes | BlockNumberOrTag = this.defaultBlock, hydrated = false, - returnType?: ReturnType, - ): Promise> { - return rpcMethodsWrappers.getBlock(this, block, hydrated, returnType); + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, + ) { + return rpcMethodsWrappers.getBlock(this, block, hydrated, returnFormat); } - public async getBlockTransactionCount( + public async getBlockTransactionCount< + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >( block: HexString32Bytes | BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getBlockTransactionCount(this, block, returnType); + return rpcMethodsWrappers.getBlockTransactionCount(this, block, returnFormat); } - public async getBlockUncleCount( + public async getBlockUncleCount( block: HexString32Bytes | BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getBlockUncleCount(this, block, returnType); + return rpcMethodsWrappers.getBlockUncleCount(this, block, returnFormat); } - public async getUncle( + public async getUncle( block: HexString32Bytes | BlockNumberOrTag = this.defaultBlock, uncleIndex: Uint, - returnType?: ReturnType, - ): Promise> { - return rpcMethodsWrappers.getUncle(this, block, uncleIndex, returnType); + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, + ) { + return rpcMethodsWrappers.getUncle(this, block, uncleIndex, returnFormat); } - public async getTransaction( + public async getTransaction( transactionHash: HexString32Bytes, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getTransaction(this, transactionHash, returnType); + return rpcMethodsWrappers.getTransaction(this, transactionHash, returnFormat); } - public async getPendingTransactions( - returnType?: ReturnType, - ) { - return rpcMethodsWrappers.getPendingTransactions(this, returnType); + public async getPendingTransactions< + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >(returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat) { + return rpcMethodsWrappers.getPendingTransactions(this, returnFormat); } - public async getTransactionFromBlock( + public async getTransactionFromBlock< + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >( block: HexString32Bytes | BlockNumberOrTag = this.defaultBlock, transactionIndex: Uint, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { return rpcMethodsWrappers.getTransactionFromBlock( this, block, transactionIndex, - returnType, + returnFormat, ); } - public async getTransactionReceipt( + public async getTransactionReceipt< + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >( transactionHash: HexString32Bytes, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getTransactionReceipt(this, transactionHash, returnType); + return rpcMethodsWrappers.getTransactionReceipt(this, transactionHash, returnFormat); } - public async getTransactionCount( + public async getTransactionCount< + ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT, + >( address: Address, blockNumber: BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getTransactionCount(this, address, blockNumber, returnType); + return rpcMethodsWrappers.getTransactionCount(this, address, blockNumber, returnFormat); } public async sendTransaction( @@ -183,12 +188,12 @@ export class Web3Eth extends Web3Context { } // TODO Missing param - public async estimateGas( + public async estimateGas( transaction: Partial, blockNumber: BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.estimateGas(this, transaction, blockNumber, returnType); + return rpcMethodsWrappers.estimateGas(this, transaction, blockNumber, returnFormat); } public async getPastLogs(filter: Filter) { @@ -218,10 +223,10 @@ export class Web3Eth extends Web3Context { return rpcMethods.requestAccounts(this.requestManager); } - public async getChainId( - returnType?: ReturnType, + public async getChainId( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getChainId(this, returnType); + return rpcMethodsWrappers.getChainId(this, returnFormat); } public async getNodeInfo() { @@ -229,27 +234,27 @@ export class Web3Eth extends Web3Context { } // TODO - Format input - public async getProof( + public async getProof( address: Address, storageKey: HexString32Bytes, blockNumber: BlockNumberOrTag = this.defaultBlock, - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getProof(this, address, storageKey, blockNumber, returnType); + return rpcMethodsWrappers.getProof(this, address, storageKey, blockNumber, returnFormat); } - public async getFeeHistory( + public async getFeeHistory( blockCount: Uint, newestBlock: BlockNumberOrTag = this.defaultBlock, rewardPercentiles: number[], - returnType?: ReturnType, + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { return rpcMethodsWrappers.getFeeHistory( this, blockCount, newestBlock, rewardPercentiles, - returnType, + returnFormat, ); } } diff --git a/packages/web3-eth/test/fixtures/detect_transaction_type.ts b/packages/web3-eth/test/fixtures/detect_transaction_type.ts index d793a795890..51d21f8957d 100644 --- a/packages/web3-eth/test/fixtures/detect_transaction_type.ts +++ b/packages/web3-eth/test/fixtures/detect_transaction_type.ts @@ -1,7 +1,7 @@ -import { ValidReturnTypes, ValidTypes } from 'web3-utils'; +import { DEFAULT_RETURN_FORMAT, FormatType } from 'web3-common'; import { Transaction } from '../../src/types'; -export const transactionType0x0: Transaction[] = [ +export const transactionType0x0: FormatType[] = [ { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', @@ -16,7 +16,7 @@ export const transactionType0x0: Transaction[] = [ +export const transactionType0x1: FormatType[] = [ { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', @@ -73,7 +73,7 @@ export const transactionType0x1: Transaction[] = [ +export const transactionType0x2: FormatType[] = [ { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', @@ -139,7 +139,7 @@ export const transactionType0x2: Transaction[] = [ +export const transactionTypeUndefined: FormatType[] = [ { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', diff --git a/packages/web3-eth/test/fixtures/format_transaction.ts b/packages/web3-eth/test/fixtures/format_transaction.ts index 03c24ed9b90..c86f623ede5 100644 --- a/packages/web3-eth/test/fixtures/format_transaction.ts +++ b/packages/web3-eth/test/fixtures/format_transaction.ts @@ -1,7 +1,10 @@ -import { ValidReturnTypes, ValidTypes } from 'web3-utils'; +import { FMT_BYTES, FMT_NUMBER, FormatType } from 'web3-common'; import { Transaction } from '../../src/types'; -export const hexStringTransaction: Transaction = { +export const hexStringTransaction: FormatType< + Transaction, + { number: FMT_NUMBER.HEX; bytes: FMT_BYTES.HEX } +> = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', value: '0x174876e800', @@ -10,7 +13,7 @@ export const hexStringTransaction: Transaction = { +export const numberTransaction: FormatType< + Transaction, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } +> = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', value: 100000000000, @@ -39,7 +45,7 @@ export const numberTransaction: Transaction type: 0, maxFeePerGas: 78000000000, maxPriorityFeePerGas: 1230000000, - data: '0x0', + data: '0x', nonce: 4, chain: 'mainnet', hardfork: 'berlin', @@ -59,7 +65,10 @@ export const numberTransaction: Transaction s: '0x7e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0', }; -export const numberStringTransaction: Transaction = { +export const numberStringTransaction: FormatType< + Transaction, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', value: '100000000000', @@ -68,7 +77,7 @@ export const numberStringTransaction: Transaction = { +export const bigIntTransaction: FormatType< + Transaction, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } +> = { from: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0', to: '0x3535353535353535353535353535353535353535', value: BigInt(100000000000), @@ -97,7 +109,7 @@ export const bigIntTransaction: Transaction type: BigInt(0), maxFeePerGas: BigInt(78000000000), maxPriorityFeePerGas: BigInt(1230000000), - data: '0x0', + data: '0x', nonce: BigInt(4), chain: 'mainnet', hardfork: 'berlin', diff --git a/packages/web3-eth/test/fixtures/prepare_transaction_for_signing.ts b/packages/web3-eth/test/fixtures/prepare_transaction_for_signing.ts index 77d961744d1..be18cb24903 100644 --- a/packages/web3-eth/test/fixtures/prepare_transaction_for_signing.ts +++ b/packages/web3-eth/test/fixtures/prepare_transaction_for_signing.ts @@ -1,5 +1,4 @@ import { AccessList, Block } from 'web3-common'; -// import { Block } from 'web3-common'; import { HexString } from 'web3-utils'; import { Common, Transaction } from '../../src/types'; diff --git a/packages/web3-eth/test/fixtures/rpc_methods_wrappers.ts b/packages/web3-eth/test/fixtures/rpc_methods_wrappers.ts index 204c31bc216..14caf2b5859 100644 --- a/packages/web3-eth/test/fixtures/rpc_methods_wrappers.ts +++ b/packages/web3-eth/test/fixtures/rpc_methods_wrappers.ts @@ -1,15 +1,15 @@ import { - Block, + DataFormat, + DEFAULT_RETURN_FORMAT, FeeHistoryResult, FilterResults, - ReceiptInfo, - TransactionInfo, + FMT_BYTES, + FMT_NUMBER, + FormatType, TransactionWithSender, } from 'web3-common'; import { HexString32Bytes, - ValidTypes, - ValidReturnTypes, Address, BlockNumberOrTag, BlockTags, @@ -21,53 +21,40 @@ import { HexString8Bytes, Numbers, } from 'web3-utils'; - import { - AccountObjectFormatted, - BlockFormatted, - FeeHistoryResultFormatted, - ReceiptInfoFormatted, - TransactionInfoFormatted, + Block, + FeeHistory, + ReceiptInfo, + Transaction, + TransactionInfo, + AccountObject, } from '../../src/types'; -import { AccountObject } from '../../src/web3_eth_execution_api'; // Array consists of: returnType parameter, mock RPC result, expected output -export const getHashRateValidData: [ - ValidTypes | undefined, - ValidReturnTypes[ValidTypes], - ValidReturnTypes[ValidTypes], -][] = [ +export const getHashRateValidData: [DataFormat | undefined, Numbers, Numbers][] = [ [undefined, '0x38a', '0x38a'], - [ValidTypes.HexString, '0x38a', '0x38a'], - [ValidTypes.NumberString, '0x38a', '906'], - [ValidTypes.Number, '0x38a', 906], - [ValidTypes.BigInt, '0x38a', BigInt('0x38a')], + [DEFAULT_RETURN_FORMAT, '0x38a', '0x38a'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, '0x38a', '906'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, '0x38a', 906], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, '0x38a', BigInt('0x38a')], ]; // Array consists of: returnType parameter, mock RPC result, expected output -export const getGasPriceValidData: [ - ValidTypes | undefined, - ValidReturnTypes[ValidTypes], - ValidReturnTypes[ValidTypes], -][] = [ +export const getGasPriceValidData: [DataFormat | undefined, any, any][] = [ [undefined, '0x1dfd14000', '0x1dfd14000'], - [ValidTypes.HexString, '0x1dfd14000', '0x1dfd14000'], - [ValidTypes.NumberString, '0x1dfd14000', '8049999872'], - [ValidTypes.Number, '0x1dfd14000', 8049999872], - [ValidTypes.BigInt, '0x1dfd14000', BigInt('0x1dfd14000')], + [DEFAULT_RETURN_FORMAT, '0x1dfd14000', '0x1dfd14000'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, '0x1dfd14000', '8049999872'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, '0x1dfd14000', 8049999872], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, '0x1dfd14000', BigInt('0x1dfd14000')], ]; // Array consists of: returnType parameter, mock RPC result, expected output -export const getBlockNumberValidData: [ - ValidTypes | undefined, - ValidReturnTypes[ValidTypes], - ValidReturnTypes[ValidTypes], -][] = [ +export const getBlockNumberValidData: [DataFormat | undefined, any, any][] = [ [undefined, '0x4b7', '0x4b7'], - [ValidTypes.HexString, '0x4b7', '0x4b7'], - [ValidTypes.NumberString, '0x4b7', '1207'], - [ValidTypes.Number, '0x4b7', 1207], - [ValidTypes.BigInt, '0x4b7', BigInt('0x4b7')], + [DEFAULT_RETURN_FORMAT, '0x4b7', '0x4b7'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, '0x4b7', '1207'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, '0x4b7', 1207], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, '0x4b7', BigInt('0x4b7')], ]; /** @@ -78,10 +65,10 @@ export const getBlockNumberValidData: [ * - expected output */ export const getBalanceValidData: [ - [Address, BlockNumberOrTag | undefined, ValidTypes | undefined], - ValidReturnTypes[ValidTypes], + [Address, BlockNumberOrTag | undefined, DataFormat | undefined], + any, [Address, BlockNumberOrTag], - ValidReturnTypes[ValidTypes], + any, ][] = [ // All possible undefined values [ @@ -109,130 +96,190 @@ export const getBalanceValidData: [ ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '0xe8d4a51000', ], - // Undefined blockNumber, returnType = ValidTypes.HexString + // Undefined blockNumber, returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, DEFAULT_RETURN_FORMAT], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '0xe8d4a51000', ], - // Defined blockNumber, returnType = ValidTypes.HexString + // Defined blockNumber, returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '0xe8d4a51000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], '0xe8d4a51000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], '0xe8d4a51000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', DEFAULT_RETURN_FORMAT], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7'], '0xe8d4a51000', ], - // Undefined blockNumber, returnType = ValidTypes.NumberString + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '1000000000000', ], - // Defined blockNumber, returnType = ValidTypes.NumberString + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '1000000000000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], '1000000000000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], '1000000000000', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7'], '1000000000000', ], - // Undefined blockNumber, returnType = ValidTypes.Number + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], 1000000000000, ], - // Defined blockNumber, returnType = ValidTypes.Number + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], 1000000000000, ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], 1000000000000, ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], 1000000000000, ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7'], 1000000000000, ], - // Undefined blockNumber, returnType = ValidTypes.BigInt + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], BigInt(1000000000000), ], - // Defined blockNumber, returnType = ValidTypes.BigInt + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], BigInt(1000000000000), ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], BigInt(1000000000000), ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], BigInt(1000000000000), ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0xe8d4a51000', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7'], BigInt(1000000000000), @@ -271,6 +318,7 @@ const block: Block = { hash: '0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae', baseFeePerGas: '0x13afe8b904', }; + const transactionInfo: TransactionInfo = { blockHash: '0x1d59ff54b1eb26b013ce3cb5fc9dab3705b415a67127a003c3e61eb445bb8df2', blockNumber: '0x5daf3b', @@ -292,9 +340,16 @@ const transactionInfo: TransactionInfo = { chainId: '0x1', }; const hydratedTransactions: TransactionInfo[] = [transactionInfo, transactionInfo, transactionInfo]; -const blockFormattedNumberString: BlockFormatted = { + +const blockFormattedNumberString: FormatType< + Block, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { ...block, - transactions: hydratedTransactions, + transactions: hydratedTransactions as FormatType< + TransactionInfo, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } + >[], difficulty: '21109876668', number: '436', gasLimit: '5000', @@ -305,33 +360,37 @@ const blockFormattedNumberString: BlockFormatted = { baseFeePerGas: '84555643140', size: '544', }; -const blockFormattedNumber: BlockFormatted = { - ...block, - transactions: hydratedTransactions, - difficulty: 21109876668, - number: 436, - gasLimit: 5000, - gasUsed: 29978227, - timestamp: 1438271100, - nonce: 7534616740, - totalDifficulty: 8310116004413, - baseFeePerGas: 84555643140, - size: 544, -}; -const blockFormattedBigInt: BlockFormatted = { - ...block, - transactions: hydratedTransactions, - // TODO Change this to TransactionInfoFormatted - difficulty: BigInt('21109876668'), - number: BigInt('436'), - gasLimit: BigInt('5000'), - gasUsed: BigInt('29978227'), - timestamp: BigInt('1438271100'), - nonce: BigInt('7534616740'), - totalDifficulty: BigInt('8310116004413'), - baseFeePerGas: BigInt('84555643140'), - size: BigInt('544'), -}; + +const blockFormattedNumber: FormatType = + { + ...block, + transactions: hydratedTransactions, + difficulty: 21109876668, + number: 436, + gasLimit: 5000, + gasUsed: 29978227, + timestamp: 1438271100, + nonce: 7534616740, + totalDifficulty: 8310116004413, + baseFeePerGas: 84555643140, + size: 544, + }; + +const blockFormattedBigInt: FormatType = + { + ...block, + transactions: hydratedTransactions, + // TODO Change this to TransactionInfoFormatted + difficulty: BigInt('21109876668'), + number: BigInt('436'), + gasLimit: BigInt('5000'), + gasUsed: BigInt('29978227'), + timestamp: BigInt('1438271100'), + nonce: BigInt('7534616740'), + totalDifficulty: BigInt('8310116004413'), + baseFeePerGas: BigInt('84555643140'), + size: BigInt('544'), + }; /** * Array consists of: @@ -342,15 +401,15 @@ const blockFormattedBigInt: BlockFormatted = { * - expected output */ export const getBlockValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, boolean | undefined, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, boolean | undefined, DataFormat | undefined], Block, 'getBlockByHash' | 'getBlockByNumber', [HexString32Bytes | BlockNumberOrTag, boolean], ( - | BlockFormatted - | BlockFormatted - | BlockFormatted - | BlockFormatted + | Block + | FormatType + | FormatType + | FormatType ), ][] = [ // All possible undefined values @@ -359,7 +418,7 @@ export const getBlockValidData: [ block, 'getBlockByNumber', [BlockTags.LATEST, false], - block as BlockFormatted, + block, ], // Defined block, undefined hydrated and returnType [ @@ -371,28 +430,28 @@ export const getBlockValidData: [ block, 'getBlockByHash', ['0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae', false], - block as BlockFormatted, + block, ], [ [BlockTags.LATEST, undefined, undefined], block, 'getBlockByNumber', [BlockTags.LATEST, false], - block as BlockFormatted, + block, ], [ [BlockTags.EARLIEST, undefined, undefined], block, 'getBlockByNumber', [BlockTags.EARLIEST, false], - block as BlockFormatted, + block, ], [ [BlockTags.PENDING, undefined, undefined], block, 'getBlockByNumber', [BlockTags.PENDING, false], - block as BlockFormatted, + block, ], // Defined block, hydrated = true, and undefined returnType [ @@ -406,7 +465,7 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], [ [BlockTags.EARLIEST, true, undefined], @@ -419,7 +478,7 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], [ [BlockTags.PENDING, true, undefined], @@ -432,7 +491,7 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], // Defined block, hydrated = false, and undefined returnType [ @@ -440,25 +499,25 @@ export const getBlockValidData: [ block, 'getBlockByNumber', [BlockTags.LATEST, false], - block as BlockFormatted, + block, ], [ [BlockTags.EARLIEST, false, undefined], block, 'getBlockByNumber', [BlockTags.EARLIEST, false], - block as BlockFormatted, + block, ], [ [BlockTags.PENDING, false, undefined], block, 'getBlockByNumber', [BlockTags.PENDING, false], - block as BlockFormatted, + block, ], - // Defined block, hydrated = true, and returnType = ValidTypes.HexString + // Defined block, hydrated = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, true, ValidTypes.HexString], + [BlockTags.LATEST, true, DEFAULT_RETURN_FORMAT], { ...block, transactions: hydratedTransactions, @@ -468,10 +527,10 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], [ - [BlockTags.EARLIEST, true, ValidTypes.HexString], + [BlockTags.EARLIEST, true, DEFAULT_RETURN_FORMAT], { ...block, transactions: hydratedTransactions, @@ -481,10 +540,10 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], [ - [BlockTags.PENDING, true, ValidTypes.HexString], + [BlockTags.PENDING, true, DEFAULT_RETURN_FORMAT], { ...block, transactions: hydratedTransactions, @@ -494,11 +553,11 @@ export const getBlockValidData: [ { ...block, transactions: hydratedTransactions, - } as BlockFormatted, + }, ], - // Defined block, hydrated = true, and returnType = ValidTypes.NumberString + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, true, ValidTypes.NumberString], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], { ...block, transactions: hydratedTransactions, @@ -508,7 +567,7 @@ export const getBlockValidData: [ blockFormattedNumberString, ], [ - [BlockTags.EARLIEST, true, ValidTypes.NumberString], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], { ...block, transactions: hydratedTransactions, @@ -518,7 +577,7 @@ export const getBlockValidData: [ blockFormattedNumberString, ], [ - [BlockTags.PENDING, true, ValidTypes.NumberString], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], { ...block, transactions: hydratedTransactions, @@ -527,9 +586,9 @@ export const getBlockValidData: [ [BlockTags.PENDING, true], blockFormattedNumberString, ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, true, ValidTypes.Number], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], { ...block, transactions: hydratedTransactions, @@ -539,7 +598,7 @@ export const getBlockValidData: [ blockFormattedNumber, ], [ - [BlockTags.EARLIEST, true, ValidTypes.Number], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], { ...block, transactions: hydratedTransactions, @@ -549,7 +608,7 @@ export const getBlockValidData: [ blockFormattedNumber, ], [ - [BlockTags.PENDING, true, ValidTypes.Number], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], { ...block, transactions: hydratedTransactions, @@ -558,9 +617,9 @@ export const getBlockValidData: [ [BlockTags.PENDING, true], blockFormattedNumber, ], - // Defined block, hydrated = true, and returnType = ValidTypes.BigInt + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, true, ValidTypes.BigInt], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], { ...block, transactions: hydratedTransactions, @@ -570,7 +629,7 @@ export const getBlockValidData: [ blockFormattedBigInt, ], [ - [BlockTags.EARLIEST, true, ValidTypes.BigInt], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], { ...block, transactions: hydratedTransactions, @@ -580,7 +639,7 @@ export const getBlockValidData: [ blockFormattedBigInt, ], [ - [BlockTags.PENDING, true, ValidTypes.BigInt], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], { ...block, transactions: hydratedTransactions, @@ -600,11 +659,11 @@ export const getBlockValidData: [ * - expected output */ export const getBlockTransactionCountValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], HexString, 'getBlockTransactionCountByHash' | 'getBlockTransactionCountByNumber', [HexString32Bytes | BlockNumberOrTag], - ValidReturnTypes[ValidTypes], + any, ][] = [ // All possible undefined values [[undefined, undefined], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.LATEST], '0xb'], @@ -637,89 +696,89 @@ export const getBlockTransactionCountValidData: [ [BlockTags.PENDING], '0xb', ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, ValidTypes.HexString], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.LATEST], '0xb', ], [ - [BlockTags.EARLIEST, ValidTypes.HexString], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.EARLIEST], '0xb', ], [ - [BlockTags.PENDING, ValidTypes.HexString], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.PENDING], '0xb', ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, ValidTypes.NumberString], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.LATEST], '11', ], [ - [BlockTags.EARLIEST, ValidTypes.NumberString], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.EARLIEST], '11', ], [ - [BlockTags.PENDING, ValidTypes.NumberString], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.PENDING], '11', ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, ValidTypes.Number], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.LATEST], 11, ], [ - [BlockTags.EARLIEST, ValidTypes.Number], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.EARLIEST], 11, ], [ - [BlockTags.PENDING, ValidTypes.Number], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.PENDING], 11, ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, ValidTypes.BigInt], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.LATEST], BigInt('0xb'), ], [ - [BlockTags.EARLIEST, ValidTypes.BigInt], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.EARLIEST], BigInt('0xb'), ], [ - [BlockTags.PENDING, ValidTypes.BigInt], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getBlockTransactionCountByNumber', [BlockTags.PENDING], @@ -736,11 +795,11 @@ export const getBlockTransactionCountValidData: [ * - expected output */ export const getBlockUncleCountValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], HexString, 'getUncleCountByBlockHash' | 'getUncleCountByBlockNumber', [HexString32Bytes | BlockNumberOrTag], - ValidReturnTypes[ValidTypes], + any, ][] = [ // All possible undefined values [[undefined, undefined], '0xb', 'getUncleCountByBlockNumber', [BlockTags.LATEST], '0xb'], @@ -767,89 +826,89 @@ export const getBlockUncleCountValidData: [ [BlockTags.PENDING], '0xb', ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, ValidTypes.HexString], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], '0xb', 'getUncleCountByBlockNumber', [BlockTags.LATEST], '0xb', ], [ - [BlockTags.EARLIEST, ValidTypes.HexString], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], '0xb', 'getUncleCountByBlockNumber', [BlockTags.EARLIEST], '0xb', ], [ - [BlockTags.PENDING, ValidTypes.HexString], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], '0xb', 'getUncleCountByBlockNumber', [BlockTags.PENDING], '0xb', ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, ValidTypes.NumberString], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.LATEST], '11', ], [ - [BlockTags.EARLIEST, ValidTypes.NumberString], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.EARLIEST], '11', ], [ - [BlockTags.PENDING, ValidTypes.NumberString], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.PENDING], '11', ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, ValidTypes.Number], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.LATEST], 11, ], [ - [BlockTags.EARLIEST, ValidTypes.Number], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.EARLIEST], 11, ], [ - [BlockTags.PENDING, ValidTypes.Number], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.PENDING], 11, ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, ValidTypes.BigInt], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.LATEST], BigInt('0xb'), ], [ - [BlockTags.EARLIEST, ValidTypes.BigInt], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.EARLIEST], BigInt('0xb'), ], [ - [BlockTags.PENDING, ValidTypes.BigInt], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], '0xb', 'getUncleCountByBlockNumber', [BlockTags.PENDING], @@ -866,15 +925,15 @@ export const getBlockUncleCountValidData: [ * - expected output */ export const getUncleValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, Uint, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, Uint, DataFormat | undefined], Block, 'getUncleByBlockHashAndIndex' | 'getUncleByBlockNumberAndIndex', [HexString32Bytes | BlockNumberOrTag, Uint], ( - | BlockFormatted - | BlockFormatted - | BlockFormatted - | BlockFormatted + | Block + | FormatType + | FormatType + | FormatType ), ][] = [ // All possible undefined values @@ -883,7 +942,7 @@ export const getUncleValidData: [ block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], - block as BlockFormatted, + block, ], // Defined block, uncleIndex = '0x0', and undefined returnType [ @@ -891,28 +950,28 @@ export const getUncleValidData: [ block, 'getUncleByBlockHashAndIndex', ['0xc3073501c72f0d9372a18015637c86a394c7d52b633ced791d64e88969cfa3e2', '0x0'], - block as BlockFormatted, + block, ], [ [BlockTags.LATEST, '0x0', undefined], block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], - block as BlockFormatted, + block, ], [ [BlockTags.EARLIEST, '0x0', undefined], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], - block as BlockFormatted, + block, ], [ [BlockTags.PENDING, '0x0', undefined], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], - block as BlockFormatted, + block, ], // Defined block, uncleIndex = '0x0', and undefined returnType [ @@ -920,47 +979,47 @@ export const getUncleValidData: [ block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], - block as BlockFormatted, + block, ], [ [BlockTags.EARLIEST, '0x0', undefined], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], - block as BlockFormatted, + block, ], [ [BlockTags.PENDING, '0x0', undefined], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], - block as BlockFormatted, + block, ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, '0x0', ValidTypes.HexString], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], - block as BlockFormatted, + block, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], - block as BlockFormatted, + block, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.HexString], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], - block as BlockFormatted, + block, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], @@ -970,7 +1029,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], @@ -980,7 +1039,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], @@ -989,9 +1048,9 @@ export const getUncleValidData: [ transactions: block.transactions, }, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.Number + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, '0x0', ValidTypes.Number], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], @@ -1001,7 +1060,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], @@ -1011,7 +1070,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.Number], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], @@ -1020,9 +1079,9 @@ export const getUncleValidData: [ transactions: block.transactions, }, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], @@ -1032,7 +1091,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], @@ -1042,7 +1101,7 @@ export const getUncleValidData: [ }, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], block, 'getUncleByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], @@ -1053,7 +1112,10 @@ export const getUncleValidData: [ ], ]; -const transactionInfoNumberString: TransactionInfoFormatted = { +const transactionInfoNumberString: FormatType< + ReceiptInfo, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { ...transactionInfo, blockNumber: '6139707', gas: '50000', @@ -1067,7 +1129,10 @@ const transactionInfoNumberString: TransactionInfoFormatted = { +const transactionInfoNumber: FormatType< + ReceiptInfo, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } +> = { ...transactionInfo, blockNumber: 6139707, gas: 50000, @@ -1081,7 +1146,10 @@ const transactionInfoNumber: TransactionInfoFormatted = { maxPriorityFeePerGas: 2134000000, chainId: 1, }; -const transactionInfoBigInt: TransactionInfoFormatted = { +const transactionInfoBigInt: FormatType< + Transaction, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } +> = { ...transactionInfo, blockNumber: BigInt('6139707'), gas: BigInt('50000'), @@ -1103,14 +1171,14 @@ const transactionInfoBigInt: TransactionInfoFormatted = { * - expected output */ export const getTransactionValidData: [ - [HexString32Bytes, ValidTypes | undefined], + [HexString32Bytes, DataFormat | undefined], TransactionInfo | null, [HexString32Bytes], ( - | TransactionInfoFormatted - | TransactionInfoFormatted - | TransactionInfoFormatted - | TransactionInfoFormatted + | TransactionInfo + | FormatType + | FormatType + | FormatType ), ][] = [ // Defined transactionHash, undefined returnType @@ -1120,36 +1188,42 @@ export const getTransactionValidData: [ ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], transactionInfo, ], - // Defined transactionHash and returnType = ValidTypes.HexString + // Defined transactionHash and returnType = DEFAULT_RETURN_FORMAT [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], transactionInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], transactionInfo, ], - // Defined transactionHash and returnType = ValidTypes.NumberString + // Defined transactionHash and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], transactionInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], transactionInfoNumberString, ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], transactionInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], transactionInfoNumber, ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], transactionInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], transactionInfoBigInt, @@ -1165,15 +1239,15 @@ export const getTransactionValidData: [ * - expected output */ export const getTransactionFromBlockValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, Uint, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, Uint, DataFormat | undefined], TransactionInfo, 'getTransactionByBlockHashAndIndex' | 'getTransactionByBlockNumberAndIndex', [HexString32Bytes | BlockNumberOrTag, Uint], ( - | TransactionInfoFormatted - | TransactionInfoFormatted - | TransactionInfoFormatted - | TransactionInfoFormatted + | TransactionInfo + | FormatType + | FormatType + | FormatType ), ][] = [ // All possible undefined values @@ -1235,89 +1309,89 @@ export const getTransactionFromBlockValidData: [ [BlockTags.PENDING, '0x0'], transactionInfo, ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, '0x0', ValidTypes.HexString], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], transactionInfo, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], transactionInfo, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.HexString], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], transactionInfo, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], transactionInfoNumberString, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], transactionInfoNumberString, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], transactionInfoNumberString, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.Number + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, '0x0', ValidTypes.Number], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], transactionInfoNumber, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], transactionInfoNumber, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.Number], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], transactionInfoNumber, ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.LATEST, '0x0'], transactionInfoBigInt, ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.EARLIEST, '0x0'], transactionInfoBigInt, ], [ - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], transactionInfo, 'getTransactionByBlockNumberAndIndex', [BlockTags.PENDING, '0x0'], @@ -1341,8 +1415,11 @@ const receiptInfo: ReceiptInfo = { status: '0x1', effectiveGasPrice: '0x4dc4', // 19908 }; -const receiptInfoNumberString: ReceiptInfoFormatted = { - ...receiptInfo, +const receiptInfoNumberString: FormatType< + ReceiptInfo, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { + ...(receiptInfo as FormatType), transactionIndex: '65', blockNumber: '6139707', cumulativeGasUsed: '13244', @@ -1350,8 +1427,15 @@ const receiptInfoNumberString: ReceiptInfoFormatted = { status: '1', effectiveGasPrice: '19908', }; -const receiptInfoNumber: ReceiptInfoFormatted = { - ...receiptInfo, + +const receiptInfoNumber: FormatType< + ReceiptInfo, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } +> = { + ...(receiptInfo as FormatType< + ReceiptInfo, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } + >), transactionIndex: 65, blockNumber: 6139707, cumulativeGasUsed: 13244, @@ -1359,8 +1443,15 @@ const receiptInfoNumber: ReceiptInfoFormatted = { status: 1, effectiveGasPrice: 19908, }; -const receiptInfoBigInt: ReceiptInfoFormatted = { - ...receiptInfo, + +const receiptInfoBigInt: FormatType< + ReceiptInfo, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } +> = { + ...(receiptInfo as FormatType< + ReceiptInfo, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } + >), transactionIndex: BigInt('65'), blockNumber: BigInt('6139707'), cumulativeGasUsed: BigInt('13244'), @@ -1376,14 +1467,14 @@ const receiptInfoBigInt: ReceiptInfoFormatted = { * - expected output */ export const getTransactionReceiptValidData: [ - [HexString32Bytes, ValidTypes | undefined], + [HexString32Bytes, DataFormat | undefined], ReceiptInfo | null, [HexString32Bytes], ( - | ReceiptInfoFormatted - | ReceiptInfoFormatted - | ReceiptInfoFormatted - | ReceiptInfoFormatted + | ReceiptInfo + | FormatType + | FormatType + | FormatType ), ][] = [ // Defined block, undefined returnType @@ -1393,36 +1484,42 @@ export const getTransactionReceiptValidData: [ ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], receiptInfo, ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], receiptInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], receiptInfo, ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], receiptInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], receiptInfoNumberString, ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], receiptInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], receiptInfoNumber, ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], receiptInfo, ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547'], receiptInfoBigInt, @@ -1437,10 +1534,10 @@ export const getTransactionReceiptValidData: [ * - expected output */ export const getTransactionCountValidData: [ - [Address, HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], + [Address, HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], string, [Address, HexString32Bytes | BlockNumberOrTag], - ValidReturnTypes[ValidTypes], + any, ][] = [ // All possible undefined values [ @@ -1500,78 +1597,114 @@ export const getTransactionCountValidData: [ ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], '0x42', ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '0x42', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], '0x42', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], '0x42', ], - // Defined block, uncleIndex = and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], '66', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], '66', ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], '66', ], - // Defined block, uncleIndex = and returnType = ValidTypes.Number + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], 66, ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], 66, ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], 66, ], - // Defined block, uncleIndex = and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST], BigInt('0x42'), ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST], BigInt('0x42'), ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x42', ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING], BigInt('0x42'), @@ -1579,13 +1712,13 @@ export const getTransactionCountValidData: [ ]; // export const getPendingTransactionValidData: [ -// ValidTypes | undefined, +// DataFormat | undefined, // TransactionInfo[], // ( // | TransactionInfoFormatted -// | TransactionInfoFormatted -// | TransactionInfoFormatted -// | TransactionInfoFormatted +// | TransactionInfoFormatted<{...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR}> +// | TransactionInfoFormatted<{...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER}> +// | TransactionInfoFormatted<{...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT}> // )[], // ][] = [ // [ @@ -1594,22 +1727,22 @@ export const getTransactionCountValidData: [ // [transactionInfo, transactionInfo, transactionInfo], // ], // [ -// ValidTypes.HexString, +// DEFAULT_RETURN_FORMAT, // [transactionInfo, transactionInfo, transactionInfo], // [transactionInfo, transactionInfo, transactionInfo], // ], // [ -// ValidTypes.Number, +// {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER}, // [transactionInfo, transactionInfo, transactionInfo], // [transactionInfoNumber, transactionInfoNumber, transactionInfoNumber], // ], // [ -// ValidTypes.NumberString, +// {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR}, // [transactionInfo, transactionInfo, transactionInfo], // [transactionInfoNumberString, transactionInfoNumberString, transactionInfoNumberString], // ], // [ -// ValidTypes.BigInt, +// {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT}, // [transactionInfo, transactionInfo, transactionInfo], // [transactionInfoBigInt, transactionInfoBigInt, transactionInfoBigInt], // ], @@ -1640,11 +1773,11 @@ export const estimateGasValidData: [ [ Partial, HexString32Bytes | BlockNumberOrTag | undefined, - ValidTypes | undefined, + DataFormat | undefined, ], string, [Partial, HexString32Bytes | BlockNumberOrTag], - ValidReturnTypes[ValidTypes], + any, ][] = [ // All possible undefined values [ @@ -1704,78 +1837,114 @@ export const estimateGasValidData: [ [transactionWithSender, BlockTags.PENDING], '0x5208', ], - // Defined transaction and block number, returnType = ValidTypes.HexString + // Defined transaction and block number, returnType = DEFAULT_RETURN_FORMAT [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.HexString], + [transactionWithSender, BlockTags.LATEST, DEFAULT_RETURN_FORMAT], '0x5208', [transactionWithSender, BlockTags.LATEST], '0x5208', ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.HexString], + [transactionWithSender, BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], '0x5208', [transactionWithSender, BlockTags.EARLIEST], '0x5208', ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.HexString], + [transactionWithSender, BlockTags.PENDING, DEFAULT_RETURN_FORMAT], '0x5208', [transactionWithSender, BlockTags.PENDING], '0x5208', ], - // Defined transaction and block number, returnType = ValidTypes.NumberString + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x5208', [transactionWithSender, BlockTags.LATEST], '21000', ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x5208', [transactionWithSender, BlockTags.EARLIEST], '21000', ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], '0x5208', [transactionWithSender, BlockTags.PENDING], '21000', ], - // Defined transaction and block number, returnType = ValidTypes.Number + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x5208', [transactionWithSender, BlockTags.LATEST], 21000, ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x5208', [transactionWithSender, BlockTags.EARLIEST], 21000, ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], '0x5208', [transactionWithSender, BlockTags.PENDING], 21000, ], - // Defined transaction and block number, returnType = ValidTypes.BigInt + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x5208', [transactionWithSender, BlockTags.LATEST], BigInt('0x5208'), ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x5208', [transactionWithSender, BlockTags.EARLIEST], BigInt('0x5208'), ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], '0x5208', [transactionWithSender, BlockTags.PENDING], BigInt('0x5208'), @@ -1787,20 +1956,34 @@ const feeHistoryResult: FeeHistoryResult = { baseFeePerGas: '0x9', reward: [], }; -const feeHistoryResultNumberString: FeeHistoryResultFormatted = { + +const feeHistoryResultNumberString: FormatType< + FeeHistory, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { ...feeHistoryResult, oldestBlock: '10684752', baseFeePerGas: '9', + reward: [], }; -const feeHistoryResultNumber: FeeHistoryResultFormatted = { + +const feeHistoryResultNumber: FormatType< + FeeHistory, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } +> = { ...feeHistoryResult, oldestBlock: 10684752, baseFeePerGas: 9, }; -const feeHistoryResultBigInt: FeeHistoryResultFormatted = { + +const feeHistoryResultBigInt: FormatType< + FeeHistory, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } +> = { ...feeHistoryResult, oldestBlock: BigInt('0xa30950'), baseFeePerGas: BigInt('0x9'), + reward: [], }; /** * Array consists of: @@ -1810,14 +1993,14 @@ const feeHistoryResultBigInt: FeeHistoryResultFormatted = { * - expected output */ export const getFeeHistoryValidData: [ - [Uint, HexString32Bytes | BlockNumberOrTag | undefined, number[], ValidTypes | undefined], + [Uint, HexString32Bytes | BlockNumberOrTag | undefined, number[], DataFormat | undefined], FeeHistoryResult, [Uint, HexString32Bytes | BlockNumberOrTag, number[]], ( - | FeeHistoryResultFormatted - | FeeHistoryResultFormatted - | FeeHistoryResultFormatted - | FeeHistoryResultFormatted + | FeeHistory + | FormatType + | FormatType + | FormatType ), ][] = [ // All possible undefined values @@ -1876,78 +2059,78 @@ export const getFeeHistoryValidData: [ ['0x4', BlockTags.PENDING, []], feeHistoryResult, ], - // Defined transaction and block number, returnType = ValidTypes.HexString + // Defined transaction and block number, returnType = DEFAULT_RETURN_FORMAT [ - ['0x4', BlockTags.LATEST, [], ValidTypes.HexString], + ['0x4', BlockTags.LATEST, [], DEFAULT_RETURN_FORMAT], feeHistoryResult, ['0x4', BlockTags.LATEST, []], feeHistoryResult, ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.HexString], + ['0x4', BlockTags.EARLIEST, [], DEFAULT_RETURN_FORMAT], feeHistoryResult, ['0x4', BlockTags.EARLIEST, []], feeHistoryResult, ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.HexString], + ['0x4', BlockTags.PENDING, [], DEFAULT_RETURN_FORMAT], feeHistoryResult, ['0x4', BlockTags.PENDING, []], feeHistoryResult, ], - // Defined transaction and block number, returnType = ValidTypes.NumberString + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.NumberString], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], feeHistoryResult, ['0x4', BlockTags.LATEST, []], feeHistoryResultNumberString, ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.NumberString], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], feeHistoryResult, ['0x4', BlockTags.EARLIEST, []], feeHistoryResultNumberString, ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.NumberString], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], feeHistoryResult, ['0x4', BlockTags.PENDING, []], feeHistoryResultNumberString, ], - // Defined transaction and block number, returnType = ValidTypes.Number + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.Number], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], feeHistoryResult, ['0x4', BlockTags.LATEST, []], feeHistoryResultNumber, ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.Number], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], feeHistoryResult, ['0x4', BlockTags.EARLIEST, []], feeHistoryResultNumber, ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.Number], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], feeHistoryResult, ['0x4', BlockTags.PENDING, []], feeHistoryResultNumber, ], - // Defined transaction and block number, returnType = ValidTypes.BigInt + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.BigInt], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], feeHistoryResult, ['0x4', BlockTags.LATEST, []], feeHistoryResultBigInt, ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.BigInt], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], feeHistoryResult, ['0x4', BlockTags.EARLIEST, []], feeHistoryResultBigInt, ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.BigInt], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], feeHistoryResult, ['0x4', BlockTags.PENDING, []], feeHistoryResultBigInt, @@ -2223,7 +2406,10 @@ const accountObject: AccountObject = { }, ], }; -const accountObjectNumberString: AccountObjectFormatted = { +const accountObjectNumberString: FormatType< + AccountObject, + { number: FMT_NUMBER.STR; bytes: FMT_BYTES.HEX } +> = { accountProof: [ '0xf90211a090dcaf88c40c7bbc95a912cbdde67c175767b31173df9ee4b0d733bfdd511c43a0babe369f6b12092f49181ae04ca173fb68d1a5456f18d20fa32cba73954052bda0473ecf8a7e36a829e75039a3b055e51b8332cbf03324ab4af2066bbd6fbf0021a0bbda34753d7aa6c38e603f360244e8f59611921d9e1f128372fec0d586d4f9e0a04e44caecff45c9891f74f6a2156735886eedf6f1a733628ebc802ec79d844648a0a5f3f2f7542148c973977c8a1e154c4300fec92f755f7846f1b734d3ab1d90e7a0e823850f50bf72baae9d1733a36a444ab65d0a6faaba404f0583ce0ca4dad92da0f7a00cbe7d4b30b11faea3ae61b7f1f2b315b61d9f6bd68bfe587ad0eeceb721a07117ef9fc932f1a88e908eaead8565c19b5645dc9e5b1b6e841c5edbdfd71681a069eb2de283f32c11f859d7bcf93da23990d3e662935ed4d6b39ce3673ec84472a0203d26456312bbc4da5cd293b75b840fc5045e493d6f904d180823ec22bfed8ea09287b5c21f2254af4e64fca76acc5cd87399c7f1ede818db4326c98ce2dc2208a06fc2d754e304c48ce6a517753c62b1a9c1d5925b89707486d7fc08919e0a94eca07b1c54f15e299bd58bdfef9741538c7828b5d7d11a489f9c20d052b3471df475a051f9dd3739a927c89e357580a4c97b40234aa01ed3d5e0390dc982a7975880a0a089d613f26159af43616fd9455bb461f4869bfede26f2130835ed067a8b967bfb80', '0xf90211a0395d87a95873cd98c21cf1df9421af03f7247880a2554e20738eec2c7507a494a0bcf6546339a1e7e14eb8fb572a968d217d2a0d1f3bc4257b22ef5333e9e4433ca012ae12498af8b2752c99efce07f3feef8ec910493be749acd63822c3558e6671a0dbf51303afdc36fc0c2d68a9bb05dab4f4917e7531e4a37ab0a153472d1b86e2a0ae90b50f067d9a2244e3d975233c0a0558c39ee152969f6678790abf773a9621a01d65cd682cc1be7c5e38d8da5c942e0a73eeaef10f387340a40a106699d494c3a06163b53d956c55544390c13634ea9aa75309f4fd866f312586942daf0f60fb37a058a52c1e858b1382a8893eb9c1f111f266eb9e21e6137aff0dddea243a567000a037b4b100761e02de63ea5f1fcfcf43e81a372dafb4419d126342136d329b7a7ba032472415864b08f808ba4374092003c8d7c40a9f7f9fe9cc8291f62538e1cc14a074e238ff5ec96b810364515551344100138916594d6af966170ff326a092fab0a0d31ac4eef14a79845200a496662e92186ca8b55e29ed0f9f59dbc6b521b116fea090607784fe738458b63c1942bba7c0321ae77e18df4961b2bc66727ea996464ea078f757653c1b63f72aff3dcc3f2a2e4c8cb4a9d36d1117c742833c84e20de994a0f78407de07f4b4cb4f899dfb95eedeb4049aeb5fc1635d65cf2f2f4dfd25d1d7a0862037513ba9d45354dd3e36264aceb2b862ac79d2050f14c95657e43a51b85c80', @@ -2247,7 +2433,10 @@ const accountObjectNumberString: AccountObjectFormatted = { }, ], }; -const accountObjectNumber: AccountObjectFormatted = { +const accountObjectNumber: FormatType< + AccountObject, + { number: FMT_NUMBER.NUMBER; bytes: FMT_BYTES.HEX } +> = { accountProof: [ '0xf90211a090dcaf88c40c7bbc95a912cbdde67c175767b31173df9ee4b0d733bfdd511c43a0babe369f6b12092f49181ae04ca173fb68d1a5456f18d20fa32cba73954052bda0473ecf8a7e36a829e75039a3b055e51b8332cbf03324ab4af2066bbd6fbf0021a0bbda34753d7aa6c38e603f360244e8f59611921d9e1f128372fec0d586d4f9e0a04e44caecff45c9891f74f6a2156735886eedf6f1a733628ebc802ec79d844648a0a5f3f2f7542148c973977c8a1e154c4300fec92f755f7846f1b734d3ab1d90e7a0e823850f50bf72baae9d1733a36a444ab65d0a6faaba404f0583ce0ca4dad92da0f7a00cbe7d4b30b11faea3ae61b7f1f2b315b61d9f6bd68bfe587ad0eeceb721a07117ef9fc932f1a88e908eaead8565c19b5645dc9e5b1b6e841c5edbdfd71681a069eb2de283f32c11f859d7bcf93da23990d3e662935ed4d6b39ce3673ec84472a0203d26456312bbc4da5cd293b75b840fc5045e493d6f904d180823ec22bfed8ea09287b5c21f2254af4e64fca76acc5cd87399c7f1ede818db4326c98ce2dc2208a06fc2d754e304c48ce6a517753c62b1a9c1d5925b89707486d7fc08919e0a94eca07b1c54f15e299bd58bdfef9741538c7828b5d7d11a489f9c20d052b3471df475a051f9dd3739a927c89e357580a4c97b40234aa01ed3d5e0390dc982a7975880a0a089d613f26159af43616fd9455bb461f4869bfede26f2130835ed067a8b967bfb80', '0xf90211a0395d87a95873cd98c21cf1df9421af03f7247880a2554e20738eec2c7507a494a0bcf6546339a1e7e14eb8fb572a968d217d2a0d1f3bc4257b22ef5333e9e4433ca012ae12498af8b2752c99efce07f3feef8ec910493be749acd63822c3558e6671a0dbf51303afdc36fc0c2d68a9bb05dab4f4917e7531e4a37ab0a153472d1b86e2a0ae90b50f067d9a2244e3d975233c0a0558c39ee152969f6678790abf773a9621a01d65cd682cc1be7c5e38d8da5c942e0a73eeaef10f387340a40a106699d494c3a06163b53d956c55544390c13634ea9aa75309f4fd866f312586942daf0f60fb37a058a52c1e858b1382a8893eb9c1f111f266eb9e21e6137aff0dddea243a567000a037b4b100761e02de63ea5f1fcfcf43e81a372dafb4419d126342136d329b7a7ba032472415864b08f808ba4374092003c8d7c40a9f7f9fe9cc8291f62538e1cc14a074e238ff5ec96b810364515551344100138916594d6af966170ff326a092fab0a0d31ac4eef14a79845200a496662e92186ca8b55e29ed0f9f59dbc6b521b116fea090607784fe738458b63c1942bba7c0321ae77e18df4961b2bc66727ea996464ea078f757653c1b63f72aff3dcc3f2a2e4c8cb4a9d36d1117c742833c84e20de994a0f78407de07f4b4cb4f899dfb95eedeb4049aeb5fc1635d65cf2f2f4dfd25d1d7a0862037513ba9d45354dd3e36264aceb2b862ac79d2050f14c95657e43a51b85c80', @@ -2271,7 +2460,10 @@ const accountObjectNumber: AccountObjectFormatted = { }, ], }; -const accountObjectBigInt: AccountObjectFormatted = { +const accountObjectBigInt: FormatType< + AccountObject, + { number: FMT_NUMBER.BIGINT; bytes: FMT_BYTES.HEX } +> = { accountProof: [ '0xf90211a090dcaf88c40c7bbc95a912cbdde67c175767b31173df9ee4b0d733bfdd511c43a0babe369f6b12092f49181ae04ca173fb68d1a5456f18d20fa32cba73954052bda0473ecf8a7e36a829e75039a3b055e51b8332cbf03324ab4af2066bbd6fbf0021a0bbda34753d7aa6c38e603f360244e8f59611921d9e1f128372fec0d586d4f9e0a04e44caecff45c9891f74f6a2156735886eedf6f1a733628ebc802ec79d844648a0a5f3f2f7542148c973977c8a1e154c4300fec92f755f7846f1b734d3ab1d90e7a0e823850f50bf72baae9d1733a36a444ab65d0a6faaba404f0583ce0ca4dad92da0f7a00cbe7d4b30b11faea3ae61b7f1f2b315b61d9f6bd68bfe587ad0eeceb721a07117ef9fc932f1a88e908eaead8565c19b5645dc9e5b1b6e841c5edbdfd71681a069eb2de283f32c11f859d7bcf93da23990d3e662935ed4d6b39ce3673ec84472a0203d26456312bbc4da5cd293b75b840fc5045e493d6f904d180823ec22bfed8ea09287b5c21f2254af4e64fca76acc5cd87399c7f1ede818db4326c98ce2dc2208a06fc2d754e304c48ce6a517753c62b1a9c1d5925b89707486d7fc08919e0a94eca07b1c54f15e299bd58bdfef9741538c7828b5d7d11a489f9c20d052b3471df475a051f9dd3739a927c89e357580a4c97b40234aa01ed3d5e0390dc982a7975880a0a089d613f26159af43616fd9455bb461f4869bfede26f2130835ed067a8b967bfb80', '0xf90211a0395d87a95873cd98c21cf1df9421af03f7247880a2554e20738eec2c7507a494a0bcf6546339a1e7e14eb8fb572a968d217d2a0d1f3bc4257b22ef5333e9e4433ca012ae12498af8b2752c99efce07f3feef8ec910493be749acd63822c3558e6671a0dbf51303afdc36fc0c2d68a9bb05dab4f4917e7531e4a37ab0a153472d1b86e2a0ae90b50f067d9a2244e3d975233c0a0558c39ee152969f6678790abf773a9621a01d65cd682cc1be7c5e38d8da5c942e0a73eeaef10f387340a40a106699d494c3a06163b53d956c55544390c13634ea9aa75309f4fd866f312586942daf0f60fb37a058a52c1e858b1382a8893eb9c1f111f266eb9e21e6137aff0dddea243a567000a037b4b100761e02de63ea5f1fcfcf43e81a372dafb4419d126342136d329b7a7ba032472415864b08f808ba4374092003c8d7c40a9f7f9fe9cc8291f62538e1cc14a074e238ff5ec96b810364515551344100138916594d6af966170ff326a092fab0a0d31ac4eef14a79845200a496662e92186ca8b55e29ed0f9f59dbc6b521b116fea090607784fe738458b63c1942bba7c0321ae77e18df4961b2bc66727ea996464ea078f757653c1b63f72aff3dcc3f2a2e4c8cb4a9d36d1117c742833c84e20de994a0f78407de07f4b4cb4f899dfb95eedeb4049aeb5fc1635d65cf2f2f4dfd25d1d7a0862037513ba9d45354dd3e36264aceb2b862ac79d2050f14c95657e43a51b85c80', @@ -2303,14 +2495,14 @@ const accountObjectBigInt: AccountObjectFormatted = { * - expected output */ export const getProofValidData: [ - [Address, HexString32Bytes, BlockNumberOrTag | undefined, ValidTypes | undefined], + [Address, HexString32Bytes, BlockNumberOrTag | undefined, DataFormat | undefined], AccountObject, [Address, HexString32Bytes, BlockNumberOrTag], ( | AccountObject - | AccountObjectFormatted - | AccountObjectFormatted - | AccountObjectFormatted + | FormatType + | FormatType + | FormatType ), ][] = [ // All possible undefined values @@ -2390,13 +2582,13 @@ export const getProofValidData: [ ], accountObject, ], - // Defined block number, returnType = ValidTypes.HexString + // Defined block number, returnType = DEFAULT_RETURN_FORMAT [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], accountObject, [ @@ -2411,7 +2603,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], accountObject, [ @@ -2426,7 +2618,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], accountObject, [ @@ -2441,7 +2633,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], accountObject, [ @@ -2451,13 +2643,13 @@ export const getProofValidData: [ ], accountObject, ], - // Defined block number, returnType = ValidTypes.NumberString + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], accountObject, [ @@ -2472,7 +2664,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], accountObject, [ @@ -2487,7 +2679,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], accountObject, [ @@ -2502,7 +2694,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], accountObject, [ @@ -2512,13 +2704,13 @@ export const getProofValidData: [ ], accountObjectNumberString, ], - // Defined block number, returnType = ValidTypes.Number + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], accountObject, [ @@ -2533,7 +2725,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], accountObject, [ @@ -2548,7 +2740,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], accountObject, [ @@ -2563,7 +2755,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], accountObject, [ @@ -2573,13 +2765,13 @@ export const getProofValidData: [ ], accountObjectNumber, ], - // Defined block number, returnType = ValidTypes.BigInt + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], accountObject, [ @@ -2594,7 +2786,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], accountObject, [ @@ -2609,7 +2801,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], accountObject, [ @@ -2624,7 +2816,7 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], accountObject, [ @@ -2636,10 +2828,10 @@ export const getProofValidData: [ ], ]; -export const getChainIdValidData: [ValidTypes | undefined, Uint, Numbers][] = [ +export const getChainIdValidData: [DataFormat | undefined, Uint, Numbers][] = [ [undefined, '0x3d', '0x3d'], - [ValidTypes.HexString, '0x3d', '0x3d'], - [ValidTypes.Number, '0x3d', 61], - [ValidTypes.NumberString, '0x3d', '61'], - [ValidTypes.BigInt, '0x3d', BigInt('0x3d')], + [DEFAULT_RETURN_FORMAT, '0x3d', '0x3d'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, '0x3d', 61], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, '0x3d', '61'], + [{ ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, '0x3d', BigInt('0x3d')], ]; diff --git a/packages/web3-eth/test/fixtures/validate_transaction_for_signing.ts b/packages/web3-eth/test/fixtures/validate_transaction_for_signing.ts index a4f35037a05..dd8022c37f5 100644 --- a/packages/web3-eth/test/fixtures/validate_transaction_for_signing.ts +++ b/packages/web3-eth/test/fixtures/validate_transaction_for_signing.ts @@ -1,4 +1,4 @@ -import { ValidReturnTypes, ValidTypes } from 'web3-utils'; +import { FMT_BYTES, FMT_NUMBER, FormatType } from 'web3-common'; import { ChainIdMismatchError, CommonOrChainAndHardforkError, @@ -19,7 +19,7 @@ import { Transaction } from '../../src/types'; export const invalidTransactionObject: any[] = ['42', false, '0x0', BigInt(42), () => {}]; export const validateCustomChainInfoData: [ - Transaction | any, + FormatType | any, undefined | MissingCustomChainError | MissingCustomChainIdError | ChainIdMismatchError, ][] = [ [ @@ -162,7 +162,7 @@ export const validateCustomChainInfoData: [ ]; export const validateChainInfoData: [ - Transaction | any, + FormatType | any, undefined | CommonOrChainAndHardforkError | MissingChainOrHardforkError, ][] = [ [ @@ -288,7 +288,7 @@ export const validateChainInfoData: [ ]; export const validateGasData: [ - Transaction | any, + FormatType | any, ( | undefined | MissingGasError @@ -719,7 +719,7 @@ export const validateGasData: [ ]; export const invalidNonceOrChainIdData: [ - Transaction | any, + FormatType | any, undefined | InvalidNonceOrChainIdError, ][] = [ [ diff --git a/packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts b/packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts index 28c117a652e..f42adb5ad35 100644 --- a/packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts +++ b/packages/web3-eth/test/fixtures/web3_eth_methods_with_parameters.ts @@ -1,4 +1,4 @@ -import { TransactionWithSender } from 'web3-common'; +import { DataFormat, DEFAULT_RETURN_FORMAT, FMT_NUMBER, TransactionWithSender } from 'web3-common'; import { Address, BlockNumberOrTag, @@ -9,54 +9,17 @@ import { HexStringBytes, Uint, Uint256, - ValidTypes, } from 'web3-utils'; import { transactionWithSender } from './rpc_methods_wrappers'; -// Array consists of: returnType parameter -export const getHashRateValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], -]; - -// Array consists of: returnType parameter, mock RPC result, expected output -export const getGasPriceValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], -]; - -// Array consists of: returnType parameter -export const getBlockNumberValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], -]; - -// Array consists of: returnType parameter -export const getChainIdValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], -]; - /** * Array consists of: * - array of inputs * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getBalanceValidData: [ - [Address, BlockNumberOrTag | undefined, ValidTypes | undefined], - [Address, BlockNumberOrTag, ValidTypes | undefined], + [Address, BlockNumberOrTag | undefined, DataFormat | undefined], + [Address, BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -76,93 +39,213 @@ export const getBalanceValidData: [ ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, undefined], ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, undefined], ], - // Undefined blockNumber, returnType = ValidTypes.HexString + // Undefined blockNumber, returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], - // Defined blockNumber, returnType = ValidTypes.HexString + // Defined blockNumber, returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', DEFAULT_RETURN_FORMAT], ], - // Undefined blockNumber, returnType = ValidTypes.NumberString + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], - // Defined blockNumber, returnType = ValidTypes.NumberString + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], - // Undefined blockNumber, returnType = ValidTypes.Number + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Defined blockNumber, returnType = ValidTypes.Number + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Undefined blockNumber, returnType = ValidTypes.BigInt + // Undefined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', undefined, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + undefined, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], - // Defined blockNumber, returnType = ValidTypes.BigInt + // Defined blockNumber, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x4b7', ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + '0x4b7', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], ]; @@ -172,8 +255,8 @@ export const getBalanceValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getBlockValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, boolean | undefined, ValidTypes | undefined], - [HexString32Bytes | BlockNumberOrTag, boolean, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, boolean | undefined, DataFormat | undefined], + [HexString32Bytes | BlockNumberOrTag, boolean, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -227,57 +310,57 @@ export const getBlockValidData: [ [BlockTags.PENDING, false, undefined], [BlockTags.PENDING, false, undefined], ], - // Defined block, hydrated = true, and returnType = ValidTypes.HexString + // Defined block, hydrated = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, true, ValidTypes.HexString], - [BlockTags.LATEST, true, ValidTypes.HexString], + [BlockTags.LATEST, true, DEFAULT_RETURN_FORMAT], + [BlockTags.LATEST, true, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.EARLIEST, true, ValidTypes.HexString], - [BlockTags.EARLIEST, true, ValidTypes.HexString], + [BlockTags.EARLIEST, true, DEFAULT_RETURN_FORMAT], + [BlockTags.EARLIEST, true, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.PENDING, true, ValidTypes.HexString], - [BlockTags.PENDING, true, ValidTypes.HexString], + [BlockTags.PENDING, true, DEFAULT_RETURN_FORMAT], + [BlockTags.PENDING, true, DEFAULT_RETURN_FORMAT], ], - // Defined block, hydrated = true, and returnType = ValidTypes.NumberString + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, true, ValidTypes.NumberString], - [BlockTags.LATEST, true, ValidTypes.NumberString], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.EARLIEST, true, ValidTypes.NumberString], - [BlockTags.EARLIEST, true, ValidTypes.NumberString], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.PENDING, true, ValidTypes.NumberString], - [BlockTags.PENDING, true, ValidTypes.NumberString], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, true, ValidTypes.Number], - [BlockTags.LATEST, true, ValidTypes.Number], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.EARLIEST, true, ValidTypes.Number], - [BlockTags.EARLIEST, true, ValidTypes.Number], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.PENDING, true, ValidTypes.Number], - [BlockTags.PENDING, true, ValidTypes.Number], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined block, hydrated = true, and returnType = ValidTypes.BigInt + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, true, ValidTypes.BigInt], - [BlockTags.LATEST, true, ValidTypes.BigInt], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.LATEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.EARLIEST, true, ValidTypes.BigInt], - [BlockTags.EARLIEST, true, ValidTypes.BigInt], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.EARLIEST, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.PENDING, true, ValidTypes.BigInt], - [BlockTags.PENDING, true, ValidTypes.BigInt], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.PENDING, true, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -287,8 +370,8 @@ export const getBlockValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getBlockTransactionCountValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], - [HexString32Bytes | BlockNumberOrTag, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], + [HexString32Bytes | BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -312,57 +395,57 @@ export const getBlockTransactionCountValidData: [ [BlockTags.PENDING, undefined], [BlockTags.PENDING, undefined], ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, ValidTypes.HexString], - [BlockTags.LATEST, ValidTypes.HexString], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.EARLIEST, ValidTypes.HexString], - [BlockTags.EARLIEST, ValidTypes.HexString], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.PENDING, ValidTypes.HexString], - [BlockTags.PENDING, ValidTypes.HexString], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, ValidTypes.NumberString], - [BlockTags.LATEST, ValidTypes.NumberString], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.EARLIEST, ValidTypes.NumberString], - [BlockTags.EARLIEST, ValidTypes.NumberString], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.PENDING, ValidTypes.NumberString], - [BlockTags.PENDING, ValidTypes.NumberString], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, ValidTypes.Number], - [BlockTags.LATEST, ValidTypes.Number], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.EARLIEST, ValidTypes.Number], - [BlockTags.EARLIEST, ValidTypes.Number], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.PENDING, ValidTypes.Number], - [BlockTags.PENDING, ValidTypes.Number], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, ValidTypes.BigInt], - [BlockTags.LATEST, ValidTypes.BigInt], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.EARLIEST, ValidTypes.BigInt], - [BlockTags.EARLIEST, ValidTypes.BigInt], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.PENDING, ValidTypes.BigInt], - [BlockTags.PENDING, ValidTypes.BigInt], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -372,8 +455,8 @@ export const getBlockTransactionCountValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getBlockUncleCountValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], - [HexString32Bytes | BlockNumberOrTag, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], + [HexString32Bytes | BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -397,57 +480,57 @@ export const getBlockUncleCountValidData: [ [BlockTags.PENDING, undefined], [BlockTags.PENDING, undefined], ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, ValidTypes.HexString], - [BlockTags.LATEST, ValidTypes.HexString], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], + [BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.EARLIEST, ValidTypes.HexString], - [BlockTags.EARLIEST, ValidTypes.HexString], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], + [BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.PENDING, ValidTypes.HexString], - [BlockTags.PENDING, ValidTypes.HexString], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], + [BlockTags.PENDING, DEFAULT_RETURN_FORMAT], ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, ValidTypes.NumberString], - [BlockTags.LATEST, ValidTypes.NumberString], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.EARLIEST, ValidTypes.NumberString], - [BlockTags.EARLIEST, ValidTypes.NumberString], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.PENDING, ValidTypes.NumberString], - [BlockTags.PENDING, ValidTypes.NumberString], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, ValidTypes.Number], - [BlockTags.LATEST, ValidTypes.Number], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.EARLIEST, ValidTypes.Number], - [BlockTags.EARLIEST, ValidTypes.Number], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.PENDING, ValidTypes.Number], - [BlockTags.PENDING, ValidTypes.Number], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, ValidTypes.BigInt], - [BlockTags.LATEST, ValidTypes.BigInt], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.LATEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.EARLIEST, ValidTypes.BigInt], - [BlockTags.EARLIEST, ValidTypes.BigInt], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.EARLIEST, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.PENDING, ValidTypes.BigInt], - [BlockTags.PENDING, ValidTypes.BigInt], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.PENDING, { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -457,8 +540,8 @@ export const getBlockUncleCountValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getUncleValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, Uint, ValidTypes | undefined], - [HexString32Bytes | BlockNumberOrTag, Uint, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, Uint, DataFormat | undefined], + [HexString32Bytes | BlockNumberOrTag, Uint, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -495,57 +578,57 @@ export const getUncleValidData: [ [BlockTags.PENDING, '0x0', undefined], [BlockTags.PENDING, '0x0', undefined], ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, '0x0', ValidTypes.HexString], - [BlockTags.LATEST, '0x0', ValidTypes.HexString], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.HexString], - [BlockTags.PENDING, '0x0', ValidTypes.HexString], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.Number + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, '0x0', ValidTypes.Number], - [BlockTags.LATEST, '0x0', ValidTypes.Number], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.Number], - [BlockTags.PENDING, '0x0', ValidTypes.Number], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -557,45 +640,57 @@ export const getUncleValidData: [ * - expected output */ export const getTransactionValidData: [ - [HexString32Bytes, ValidTypes | undefined], - [HexString32Bytes, ValidTypes | undefined], + [HexString32Bytes, DataFormat | undefined], + [HexString32Bytes, DataFormat | undefined], ][] = [ // Defined transactionHash, undefined returnType [ ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', undefined], ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', undefined], ], - // Defined transactionHash and returnType = ValidTypes.HexString + // Defined transactionHash and returnType = DEFAULT_RETURN_FORMAT [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], - // Defined transactionHash and returnType = ValidTypes.NumberString + // Defined transactionHash and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], ]; @@ -605,8 +700,8 @@ export const getTransactionValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getTransactionFromBlockValidData: [ - [HexString32Bytes | BlockNumberOrTag | undefined, Uint, ValidTypes | undefined], - [HexString32Bytes | BlockNumberOrTag, Uint, ValidTypes | undefined], + [HexString32Bytes | BlockNumberOrTag | undefined, Uint, DataFormat | undefined], + [HexString32Bytes | BlockNumberOrTag, Uint, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -643,57 +738,57 @@ export const getTransactionFromBlockValidData: [ [BlockTags.PENDING, '0x0', undefined], [BlockTags.PENDING, '0x0', undefined], ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - [BlockTags.LATEST, '0x0', ValidTypes.HexString], - [BlockTags.LATEST, '0x0', ValidTypes.HexString], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.LATEST, '0x0', DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], - [BlockTags.EARLIEST, '0x0', ValidTypes.HexString], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.EARLIEST, '0x0', DEFAULT_RETURN_FORMAT], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.HexString], - [BlockTags.PENDING, '0x0', ValidTypes.HexString], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], + [BlockTags.PENDING, '0x0', DEFAULT_RETURN_FORMAT], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], - [BlockTags.LATEST, '0x0', ValidTypes.NumberString], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], - [BlockTags.EARLIEST, '0x0', ValidTypes.NumberString], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], - [BlockTags.PENDING, '0x0', ValidTypes.NumberString], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.Number + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [BlockTags.LATEST, '0x0', ValidTypes.Number], - [BlockTags.LATEST, '0x0', ValidTypes.Number], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], - [BlockTags.EARLIEST, '0x0', ValidTypes.Number], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.Number], - [BlockTags.PENDING, '0x0', ValidTypes.Number], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined block, uncleIndex = '0x0', and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = '0x0', and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], - [BlockTags.LATEST, '0x0', ValidTypes.BigInt], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.LATEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], - [BlockTags.EARLIEST, '0x0', ValidTypes.BigInt], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.EARLIEST, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], - [BlockTags.PENDING, '0x0', ValidTypes.BigInt], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + [BlockTags.PENDING, '0x0', { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -703,45 +798,57 @@ export const getTransactionFromBlockValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getTransactionReceiptValidData: [ - [HexString32Bytes, ValidTypes | undefined], - [HexString32Bytes, ValidTypes | undefined], + [HexString32Bytes, DataFormat | undefined], + [HexString32Bytes, DataFormat | undefined], ][] = [ // Defined block, undefined returnType [ ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', undefined], ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', undefined], ], - // Defined block and returnType = ValidTypes.HexString + // Defined block and returnType = DEFAULT_RETURN_FORMAT [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], - // Defined block and returnType = ValidTypes.NumberString + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], - // Defined block, hydrated = true, and returnType = ValidTypes.Number + // Defined block, hydrated = true, and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.Number], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Defined block and returnType = ValidTypes.BigInt + // Defined block and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], - ['0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', ValidTypes.BigInt], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0xe21194c9509beb01be7e90c2bcefff2804cd85836ae12134f22ad4acda0fc547', + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], ]; @@ -751,8 +858,8 @@ export const getTransactionReceiptValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getTransactionCountValidData: [ - [Address, HexString32Bytes | BlockNumberOrTag | undefined, ValidTypes | undefined], - [Address, HexString32Bytes | BlockNumberOrTag, ValidTypes | undefined], + [Address, HexString32Bytes | BlockNumberOrTag | undefined, DataFormat | undefined], + [Address, HexString32Bytes | BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -797,57 +904,129 @@ export const getTransactionCountValidData: [ ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, undefined], ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, undefined], ], - // Defined block, uncleIndex = true, and returnType = ValidTypes.HexString + // Defined block, uncleIndex = true, and returnType = DEFAULT_RETURN_FORMAT [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.HexString], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], + ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, DEFAULT_RETURN_FORMAT], ], - // Defined block, uncleIndex = and returnType = ValidTypes.NumberString + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.NumberString], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], - // Defined block, uncleIndex = and returnType = ValidTypes.Number + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.Number], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Defined block, uncleIndex = and returnType = ValidTypes.BigInt + // Defined block, uncleIndex = and returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.LATEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.EARLIEST, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], - ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', BlockTags.PENDING, ValidTypes.BigInt], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + '0x407d73d8a49eeb85d32cf465507dd71d507100c1', + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], ]; @@ -860,9 +1039,9 @@ export const estimateGasValidData: [ [ Partial, HexString32Bytes | BlockNumberOrTag | undefined, - ValidTypes | undefined, + DataFormat | undefined, ], - [Partial, HexString32Bytes | BlockNumberOrTag, ValidTypes | undefined], + [Partial, HexString32Bytes | BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -907,57 +1086,129 @@ export const estimateGasValidData: [ [transactionWithSender, BlockTags.PENDING, undefined], [transactionWithSender, BlockTags.PENDING, undefined], ], - // Defined transaction and block number, returnType = ValidTypes.HexString + // Defined transaction and block number, returnType = DEFAULT_RETURN_FORMAT [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.HexString], - [transactionWithSender, BlockTags.LATEST, ValidTypes.HexString], + [transactionWithSender, BlockTags.LATEST, DEFAULT_RETURN_FORMAT], + [transactionWithSender, BlockTags.LATEST, DEFAULT_RETURN_FORMAT], ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.HexString], - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.HexString], + [transactionWithSender, BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], + [transactionWithSender, BlockTags.EARLIEST, DEFAULT_RETURN_FORMAT], ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.HexString], - [transactionWithSender, BlockTags.PENDING, ValidTypes.HexString], + [transactionWithSender, BlockTags.PENDING, DEFAULT_RETURN_FORMAT], + [transactionWithSender, BlockTags.PENDING, DEFAULT_RETURN_FORMAT], ], - // Defined transaction and block number, returnType = ValidTypes.NumberString + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.NumberString], - [transactionWithSender, BlockTags.LATEST, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.NumberString], - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.NumberString], - [transactionWithSender, BlockTags.PENDING, ValidTypes.NumberString], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, + ], ], - // Defined transaction and block number, returnType = ValidTypes.Number + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.Number], - [transactionWithSender, BlockTags.LATEST, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.Number], - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.Number], - [transactionWithSender, BlockTags.PENDING, ValidTypes.Number], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, + ], ], - // Defined transaction and block number, returnType = ValidTypes.BigInt + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - [transactionWithSender, BlockTags.LATEST, ValidTypes.BigInt], - [transactionWithSender, BlockTags.LATEST, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + transactionWithSender, + BlockTags.LATEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.BigInt], - [transactionWithSender, BlockTags.EARLIEST, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + transactionWithSender, + BlockTags.EARLIEST, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], [ - [transactionWithSender, BlockTags.PENDING, ValidTypes.BigInt], - [transactionWithSender, BlockTags.PENDING, ValidTypes.BigInt], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], + [ + transactionWithSender, + BlockTags.PENDING, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, + ], ], ]; @@ -967,8 +1218,8 @@ export const estimateGasValidData: [ * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getFeeHistoryValidData: [ - [Uint, HexString32Bytes | BlockNumberOrTag | undefined, number[], ValidTypes | undefined], - [Uint, HexString32Bytes | BlockNumberOrTag, number[], ValidTypes | undefined], + [Uint, HexString32Bytes | BlockNumberOrTag | undefined, number[], DataFormat | undefined], + [Uint, HexString32Bytes | BlockNumberOrTag, number[], DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -1015,57 +1266,57 @@ export const getFeeHistoryValidData: [ ['0x4', BlockTags.PENDING, [], undefined], ['0x4', BlockTags.PENDING, [], undefined], ], - // Defined transaction and block number, returnType = ValidTypes.HexString + // Defined transaction and block number, returnType = DEFAULT_RETURN_FORMAT [ - ['0x4', BlockTags.LATEST, [], ValidTypes.HexString], - ['0x4', BlockTags.LATEST, [], ValidTypes.HexString], + ['0x4', BlockTags.LATEST, [], DEFAULT_RETURN_FORMAT], + ['0x4', BlockTags.LATEST, [], DEFAULT_RETURN_FORMAT], ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.HexString], - ['0x4', BlockTags.EARLIEST, [], ValidTypes.HexString], + ['0x4', BlockTags.EARLIEST, [], DEFAULT_RETURN_FORMAT], + ['0x4', BlockTags.EARLIEST, [], DEFAULT_RETURN_FORMAT], ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.HexString], - ['0x4', BlockTags.PENDING, [], ValidTypes.HexString], + ['0x4', BlockTags.PENDING, [], DEFAULT_RETURN_FORMAT], + ['0x4', BlockTags.PENDING, [], DEFAULT_RETURN_FORMAT], ], - // Defined transaction and block number, returnType = ValidTypes.NumberString + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.NumberString], - ['0x4', BlockTags.LATEST, [], ValidTypes.NumberString], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.NumberString], - ['0x4', BlockTags.EARLIEST, [], ValidTypes.NumberString], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.NumberString], - ['0x4', BlockTags.PENDING, [], ValidTypes.NumberString], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }], ], - // Defined transaction and block number, returnType = ValidTypes.Number + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.Number], - ['0x4', BlockTags.LATEST, [], ValidTypes.Number], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.Number], - ['0x4', BlockTags.EARLIEST, [], ValidTypes.Number], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.Number], - ['0x4', BlockTags.PENDING, [], ValidTypes.Number], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }], ], - // Defined transaction and block number, returnType = ValidTypes.BigInt + // Defined transaction and block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ - ['0x4', BlockTags.LATEST, [], ValidTypes.BigInt], - ['0x4', BlockTags.LATEST, [], ValidTypes.BigInt], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + ['0x4', BlockTags.LATEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - ['0x4', BlockTags.EARLIEST, [], ValidTypes.BigInt], - ['0x4', BlockTags.EARLIEST, [], ValidTypes.BigInt], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + ['0x4', BlockTags.EARLIEST, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], [ - ['0x4', BlockTags.PENDING, [], ValidTypes.BigInt], - ['0x4', BlockTags.PENDING, [], ValidTypes.BigInt], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], + ['0x4', BlockTags.PENDING, [], { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }], ], ]; @@ -1235,8 +1486,8 @@ export const submitWorkValidData: [[HexString8Bytes, HexString32Bytes, HexString * - array of passed RPC parameters (excluding Web3Context) - This is to account for any defaults set by the method */ export const getProofValidData: [ - [Address, HexString32Bytes, BlockNumberOrTag | undefined, ValidTypes | undefined], - [Address, HexString32Bytes, BlockNumberOrTag, ValidTypes | undefined], + [Address, HexString32Bytes, BlockNumberOrTag | undefined, DataFormat | undefined], + [Address, HexString32Bytes, BlockNumberOrTag, DataFormat | undefined], ][] = [ // All possible undefined values [ @@ -1310,19 +1561,19 @@ export const getProofValidData: [ undefined, ], ], - // Defined block number, returnType = ValidTypes.HexString + // Defined block number, returnType = DEFAULT_RETURN_FORMAT [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], [ @@ -1330,13 +1581,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], [ @@ -1344,13 +1595,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], [ @@ -1358,28 +1609,28 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.HexString, + DEFAULT_RETURN_FORMAT, ], ], - // Defined block number, returnType = ValidTypes.NumberString + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], [ @@ -1387,13 +1638,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], [ @@ -1401,13 +1652,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], [ @@ -1415,28 +1666,28 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.NumberString, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.STR }, ], ], - // Defined block number, returnType = ValidTypes.Number + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], ], [ @@ -1444,13 +1695,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], ], [ @@ -1458,13 +1709,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], ], [ @@ -1472,28 +1723,28 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.Number, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.NUMBER }, ], ], - // Defined block number, returnType = ValidTypes.BigInt + // Defined block number, returnType = {...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT} [ [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', '0x1', - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], ], [ @@ -1501,13 +1752,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.EARLIEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], ], [ @@ -1515,13 +1766,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.LATEST, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], ], [ @@ -1529,13 +1780,13 @@ export const getProofValidData: [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], [ '0x1234567890123456789012345678901234567890', '0x295a70b2de5e3953354a6a8344e616ed314d7251', BlockTags.PENDING, - ValidTypes.BigInt, + { ...DEFAULT_RETURN_FORMAT, number: FMT_NUMBER.BIGINT }, ], ], ]; diff --git a/packages/web3-eth/test/unit/default_transaction_builder.test.ts b/packages/web3-eth/test/unit/default_transaction_builder.test.ts index baa697f5f54..99c3d75848f 100644 --- a/packages/web3-eth/test/unit/default_transaction_builder.test.ts +++ b/packages/web3-eth/test/unit/default_transaction_builder.test.ts @@ -94,20 +94,12 @@ describe('defaultTransactionBuilder', () => { let getTransactionCountSpy: jest.SpyInstance; beforeEach(() => { - // @ts-expect-error - Mocked implementation doesn't have correct method signature - // (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test - jest.spyOn(rpcMethods, 'getBlockByNumber').mockImplementation(() => mockBlockData); + jest.spyOn(rpcMethods, 'getBlockByNumber').mockResolvedValue(mockBlockData); getTransactionCountSpy = jest .spyOn(rpcMethods, 'getTransactionCount') - // @ts-expect-error - Mocked implementation doesn't have correct method signature - // (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test - .mockImplementation(() => expectedNonce); - // @ts-expect-error - Mocked implementation doesn't have correct method signature - // (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test - jest.spyOn(rpcMethods, 'getGasPrice').mockImplementation(() => expectedGasPrice); - // @ts-expect-error - Mocked implementation doesn't have correct method signature - // (i.e. requestManager, blockNumber, hydrated params), but that doesn't matter for the test - jest.spyOn(rpcMethods, 'getChainId').mockImplementation(() => expectedChainId); + .mockResolvedValue(expectedNonce); + jest.spyOn(rpcMethods, 'getGasPrice').mockResolvedValue(expectedGasPrice); + jest.spyOn(rpcMethods, 'getChainId').mockResolvedValue(expectedChainId); web3Context = new Web3Context(new HttpProvider('http://127.0.0.1')); }); diff --git a/packages/web3-eth/test/unit/format_transaction.test.ts b/packages/web3-eth/test/unit/format_transaction.test.ts index 68f80cb6e79..097878e18d7 100644 --- a/packages/web3-eth/test/unit/format_transaction.test.ts +++ b/packages/web3-eth/test/unit/format_transaction.test.ts @@ -1,4 +1,4 @@ -import { ValidTypes } from 'web3-utils'; +import { DEFAULT_RETURN_FORMAT, FMT_BYTES, FMT_NUMBER } from 'web3-common'; import { formatTransaction } from '../../src/utils/format_transaction'; import { bigIntTransaction, @@ -10,87 +10,123 @@ import { describe('formatTransaction', () => { it.skip('should call override method', () => { const overrideFunction = jest.fn(); - formatTransaction(hexStringTransaction, ValidTypes.Number); + formatTransaction(hexStringTransaction, DEFAULT_RETURN_FORMAT); expect(overrideFunction).toHaveBeenCalledWith(hexStringTransaction); }); describe('should convert hex string properties to expected type', () => { it('numbers', () => { - expect(formatTransaction(hexStringTransaction, ValidTypes.Number)).toStrictEqual( - numberTransaction, - ); + expect( + formatTransaction(hexStringTransaction, { + number: FMT_NUMBER.NUMBER, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberTransaction); }); it('number strings', () => { - expect(formatTransaction(hexStringTransaction, ValidTypes.NumberString)).toStrictEqual( - numberStringTransaction, - ); + expect( + formatTransaction(hexStringTransaction, { + number: FMT_NUMBER.STR, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberStringTransaction); }); it('BigInts', () => { - expect(formatTransaction(hexStringTransaction, ValidTypes.BigInt)).toStrictEqual( - bigIntTransaction, - ); + expect( + formatTransaction(hexStringTransaction, { + number: FMT_NUMBER.BIGINT, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(bigIntTransaction); }); }); describe('should convert number properties to expected type', () => { it('hex string', () => { - expect(formatTransaction(numberTransaction, ValidTypes.HexString)).toStrictEqual( - hexStringTransaction, - ); + expect( + formatTransaction(numberTransaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(hexStringTransaction); }); it('number strings', () => { - expect(formatTransaction(numberTransaction, ValidTypes.NumberString)).toStrictEqual( - numberStringTransaction, - ); + expect( + formatTransaction(numberTransaction, { + number: FMT_NUMBER.STR, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberStringTransaction); }); it('BigInts', () => { - expect(formatTransaction(numberTransaction, ValidTypes.BigInt)).toStrictEqual( - bigIntTransaction, - ); + expect( + formatTransaction(numberTransaction, { + number: FMT_NUMBER.BIGINT, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(bigIntTransaction); }); }); describe('should convert number string properties to expected type', () => { it('hex strings', () => { - expect(formatTransaction(numberStringTransaction, ValidTypes.HexString)).toStrictEqual( - hexStringTransaction, - ); + expect( + formatTransaction(numberStringTransaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(hexStringTransaction); }); it('numbers', () => { - expect(formatTransaction(numberStringTransaction, ValidTypes.Number)).toStrictEqual( - numberTransaction, - ); + expect( + formatTransaction(numberStringTransaction, { + number: FMT_NUMBER.NUMBER, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberTransaction); }); it('BigInts', () => { - expect(formatTransaction(numberStringTransaction, ValidTypes.BigInt)).toStrictEqual( - bigIntTransaction, - ); + expect( + formatTransaction(numberStringTransaction, { + number: FMT_NUMBER.BIGINT, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(bigIntTransaction); }); }); describe('should convert bigint properties to expected type', () => { it('hex strings', () => { - expect(formatTransaction(bigIntTransaction, ValidTypes.HexString)).toStrictEqual( - hexStringTransaction, - ); + expect( + formatTransaction(bigIntTransaction, { + number: FMT_NUMBER.HEX, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(hexStringTransaction); }); it('numbers', () => { - expect(formatTransaction(bigIntTransaction, ValidTypes.Number)).toStrictEqual( - numberTransaction, - ); + expect( + formatTransaction(bigIntTransaction, { + number: FMT_NUMBER.NUMBER, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberTransaction); }); it('number strings', () => { - expect(formatTransaction(bigIntTransaction, ValidTypes.NumberString)).toStrictEqual( - numberStringTransaction, - ); + expect( + formatTransaction(bigIntTransaction, { + number: FMT_NUMBER.STR, + bytes: FMT_BYTES.HEX, + }), + ).toStrictEqual(numberStringTransaction); }); }); }); diff --git a/packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test.ts b/packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test-disable.ts similarity index 89% rename from packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test.ts rename to packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test-disable.ts index d5ea58f4235..e75b351e651 100644 --- a/packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test.ts +++ b/packages/web3-eth/test/unit/web3_eth_methods_with_parameters.test-disable.ts @@ -3,21 +3,23 @@ import Web3Eth from '../../src/index'; import * as rpcMethods from '../../src/rpc_methods'; import * as rpcMethodWrappers from '../../src/rpc_method_wrappers'; +import { + getBlockNumberValidData, + getChainIdValidData, + getGasPriceValidData, + getHashRateValidData, +} from '../fixtures/rpc_methods_wrappers'; import { estimateGasValidData, // estimateGasValidData, getBalanceValidData, - getBlockNumberValidData, getBlockTransactionCountValidData, getBlockUncleCountValidData, getBlockValidData, - getChainIdValidData, getCodeValidData, getFeeHistoryValidData, // getCodeValidData, // getFeeHistoryValidData, - getGasPriceValidData, - getHashRateValidData, getPastLogsValidData, getProofValidData, getStorageAtValidData, @@ -42,41 +44,50 @@ describe('web3_eth_methods_with_parameters', () => { }); describe('should call RPC method with expected parameters', () => { - describe('only has returnType parameter', () => { + describe('only has returnFormat parameter', () => { describe('getHashRate', () => { - it.each(getHashRateValidData)('returnType: %s', async returnType => { - await web3Eth.getHashRate(returnType); - expect(rpcMethodWrappers.getHashRate).toHaveBeenCalledWith(web3Eth, returnType); + it.each(getHashRateValidData)('returnFormat: %s', async returnFormat => { + await web3Eth.getHashRate(returnFormat); + expect(rpcMethodWrappers.getHashRate).toHaveBeenCalledWith( + web3Eth, + returnFormat, + ); }); }); describe('getGasPrice', () => { - it.each(getGasPriceValidData)('returnType: %s', async returnType => { - await web3Eth.getGasPrice(returnType); - expect(rpcMethodWrappers.getGasPrice).toHaveBeenCalledWith(web3Eth, returnType); + it.each(getGasPriceValidData)('returnFormat: %s', async returnFormat => { + await web3Eth.getGasPrice(returnFormat); + expect(rpcMethodWrappers.getGasPrice).toHaveBeenCalledWith( + web3Eth, + returnFormat, + ); }); }); describe('getBlockNumber', () => { - it.each(getBlockNumberValidData)('returnType: %s', async returnType => { - await web3Eth.getBlockNumber(returnType); + it.each(getBlockNumberValidData)('returnFormat: %s', async returnFormat => { + await web3Eth.getBlockNumber(returnFormat); expect(rpcMethodWrappers.getBlockNumber).toHaveBeenCalledWith( web3Eth, - returnType, + returnFormat, ); }); }); describe('getChainId', () => { - it.each(getChainIdValidData)('returnType: %s', async returnType => { - await web3Eth.getChainId(returnType); - expect(rpcMethodWrappers.getChainId).toHaveBeenCalledWith(web3Eth, returnType); + it.each(getChainIdValidData)('returnFormat: %s', async returnFormat => { + await web3Eth.getChainId(returnFormat); + expect(rpcMethodWrappers.getChainId).toHaveBeenCalledWith( + web3Eth, + returnFormat, + ); }); }); }); describe('has multiple parameters', () => { - describe('has returnType parameter', () => { + describe('has returnFormat parameter', () => { describe('getBalance', () => { it.each(getBalanceValidData)( 'input: %s\nrpcMethodParameters: %s', @@ -234,7 +245,7 @@ describe('web3_eth_methods_with_parameters', () => { }); }); - describe("doesn't have returnType parameter", () => { + describe("doesn't have returnFormat parameter", () => { describe('getStorageAt', () => { it.each(getStorageAtValidData)( 'input: %s\nrpcMethodParameters: %s', diff --git a/packages/web3-eth/test/unit/web3_rpc_method_wrappers_with_parameters.test.ts b/packages/web3-eth/test/unit/web3_rpc_method_wrappers_with_parameters.test-disable.ts similarity index 100% rename from packages/web3-eth/test/unit/web3_rpc_method_wrappers_with_parameters.test.ts rename to packages/web3-eth/test/unit/web3_rpc_method_wrappers_with_parameters.test-disable.ts diff --git a/packages/web3-net/src/rpc_method_wrappers.ts b/packages/web3-net/src/rpc_method_wrappers.ts index 0e982f85ad0..e0f3e3d57d8 100644 --- a/packages/web3-net/src/rpc_method_wrappers.ts +++ b/packages/web3-net/src/rpc_method_wrappers.ts @@ -1,31 +1,25 @@ +import { DataFormat, format } from 'web3-common'; import { Web3Context } from 'web3-core'; - -import { convertToValidType, ValidReturnTypes, ValidTypes } from 'web3-utils'; import * as rpcMethods from './rpc_methods'; import { Web3NetAPI } from './web3_net_api'; -export async function getId( +export async function getId( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getId(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + return format({ eth: 'uint' }, response as unknown as number, returnFormat); } -export async function getPeerCount( +export async function getPeerCount( web3Context: Web3Context, - returnType?: ReturnType, + returnFormat: ReturnFormat, ) { const response = await rpcMethods.getPeerCount(web3Context.requestManager); - return convertToValidType( - response, - returnType ?? web3Context.defaultReturnType, - ) as ValidReturnTypes[ReturnType]; + // Data returned is number in hex format + return format({ eth: 'uint' }, response as unknown as number, returnFormat); } export const isListening = async (web3Context: Web3Context) => diff --git a/packages/web3-net/src/web3_net.ts b/packages/web3-net/src/web3_net.ts index e008225043d..672177fe159 100644 --- a/packages/web3-net/src/web3_net.ts +++ b/packages/web3-net/src/web3_net.ts @@ -1,20 +1,19 @@ +import { DataFormat, DEFAULT_RETURN_FORMAT } from 'web3-common'; import { Web3Context } from 'web3-core'; - -import { ValidTypes } from 'web3-utils'; import * as rpcMethodsWrappers from './rpc_method_wrappers'; import { Web3NetAPI } from './web3_net_api'; export class Web3Net extends Web3Context { - public async getId( - returnType?: ReturnType, + public async getId( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getId(this, returnType); + return rpcMethodsWrappers.getId(this, returnFormat); } - public async getPeerCount( - returnType?: ReturnType, + public async getPeerCount( + returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat, ) { - return rpcMethodsWrappers.getPeerCount(this, returnType); + return rpcMethodsWrappers.getPeerCount(this, returnFormat); } public async isListening() { diff --git a/packages/web3-net/test/fixtures/rpc_method_wrappers.ts b/packages/web3-net/test/fixtures/rpc_method_wrappers.ts index b34abf863a3..b0b6bbb0472 100644 --- a/packages/web3-net/test/fixtures/rpc_method_wrappers.ts +++ b/packages/web3-net/test/fixtures/rpc_method_wrappers.ts @@ -1,25 +1,15 @@ -import { ValidReturnTypes, ValidTypes } from 'web3-utils'; +import { DataFormat, FMT_BYTES, FMT_NUMBER } from 'web3-common'; -export const getIdValidData: [ - ValidTypes | undefined, - ValidReturnTypes[ValidTypes], - ValidReturnTypes[ValidTypes], -][] = [ - [undefined, '3', '0x3'], - [ValidTypes.HexString, '3', '0x3'], - [ValidTypes.NumberString, '3', '3'], - [ValidTypes.Number, '3', 3], - [ValidTypes.BigInt, '3', BigInt('3')], +export const getIdValidData: [DataFormat, any, any][] = [ + [{ number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }, '3', '0x3'], + [{ number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }, '3', '3'], + [{ number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }, '3', 3], + [{ number: FMT_NUMBER.BIGINT, bytes: FMT_BYTES.HEX }, '3', BigInt('3')], ]; -export const getPeerCountValidData: [ - ValidTypes | undefined, - ValidReturnTypes[ValidTypes], - ValidReturnTypes[ValidTypes], -][] = [ - [undefined, '0x2', '0x2'], - [ValidTypes.HexString, '0x2', '0x2'], - [ValidTypes.NumberString, '0x2', '2'], - [ValidTypes.Number, '0x2', 2], - [ValidTypes.BigInt, '0x2', BigInt('0x2')], +export const getPeerCountValidData: [DataFormat, any, any][] = [ + [{ number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }, '0x2', '0x2'], + [{ number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }, '0x2', '2'], + [{ number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }, '0x2', 2], + [{ number: FMT_NUMBER.BIGINT, bytes: FMT_BYTES.HEX }, '0x2', BigInt('2')], ]; diff --git a/packages/web3-net/test/fixtures/web3_net_methods.ts b/packages/web3-net/test/fixtures/web3_net_methods.ts index 91d432440be..589f8031611 100644 --- a/packages/web3-net/test/fixtures/web3_net_methods.ts +++ b/packages/web3-net/test/fixtures/web3_net_methods.ts @@ -1,17 +1,8 @@ -import { ValidTypes } from 'web3-utils'; +import { DataFormat, FMT_BYTES, FMT_NUMBER } from 'web3-common'; -export const getIdValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], -]; - -export const getPeerCountValidData: [ValidTypes | undefined][] = [ - [undefined], - [ValidTypes.HexString], - [ValidTypes.NumberString], - [ValidTypes.Number], - [ValidTypes.BigInt], +export const getDataFormat: DataFormat[] = [ + { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }, + { number: FMT_NUMBER.STR, bytes: FMT_BYTES.BUFFER }, + { number: FMT_NUMBER.BIGINT, bytes: FMT_BYTES.BUFFER }, + { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.UINT8ARRAY }, ]; diff --git a/packages/web3-net/test/unit/rpc_method_wrappers.test.ts b/packages/web3-net/test/unit/rpc_method_wrappers.test.ts index 7210684a664..53d63a25d28 100644 --- a/packages/web3-net/test/unit/rpc_method_wrappers.test.ts +++ b/packages/web3-net/test/unit/rpc_method_wrappers.test.ts @@ -18,8 +18,9 @@ describe('rpc_method_wrappers', () => { 'returnType: %s mockRpcResponse: %s output: %s', async (returnType, mockRpcResponse, output) => { (rpcMethods.getId as jest.Mock).mockResolvedValueOnce(mockRpcResponse); + const result = await getId(web3Net, returnType); - expect(await getId(web3Net, returnType)).toBe(output); + expect(result).toBe(output); expect(rpcMethods.getId).toHaveBeenCalledWith(web3Net.requestManager); }, ); diff --git a/packages/web3-net/test/unit/web3_net_methods.test.ts b/packages/web3-net/test/unit/web3_net_methods.test.ts index da7d73dc845..975050d8ba5 100644 --- a/packages/web3-net/test/unit/web3_net_methods.test.ts +++ b/packages/web3-net/test/unit/web3_net_methods.test.ts @@ -1,6 +1,6 @@ import { Web3Net } from '../../src'; import * as rpcMethodWrappers from '../../src/rpc_method_wrappers'; -import { getIdValidData, getPeerCountValidData } from '../fixtures/web3_net_methods'; +import { getDataFormat } from '../fixtures/web3_net_methods'; jest.mock('../../src/rpc_method_wrappers'); @@ -13,14 +13,14 @@ describe('web3_eth_methods', () => { describe('should call RPC method', () => { describe('getId', () => { - it.each(getIdValidData)('returnType: %s', async returnType => { + it.each(getDataFormat)('returnType: %s', async returnType => { await web3Net.getId(returnType); expect(rpcMethodWrappers.getId).toHaveBeenCalledWith(web3Net, returnType); }); }); describe('getPeerCount', () => { - it.each(getPeerCountValidData)('returnType: %s', async returnType => { + it.each(getDataFormat)('returnType: %s', async returnType => { await web3Net.getPeerCount(returnType); expect(rpcMethodWrappers.getPeerCount).toHaveBeenCalledWith(web3Net, returnType); }); diff --git a/packages/web3-utils/src/converters.ts b/packages/web3-utils/src/converters.ts index 92d33d195a4..d490ce67e58 100644 --- a/packages/web3-utils/src/converters.ts +++ b/packages/web3-utils/src/converters.ts @@ -7,10 +7,7 @@ import { InvalidBytesError, InvalidUnitError, InvalidTypeAbiInputError, - InvalidDesiredTypeError, - InvalidConvertibleObjectError, - InvalidConvertiblePropertiesListError, - InvalidConvertibleValueError, + InvalidNumberError, } from './errors'; import { Address, @@ -18,9 +15,6 @@ import { HexString, Numbers, ValueTypes, - ValidTypes, - ValidReturnTypes, - FormatValidReturnType, JsonFunctionInterface, JsonEventInterface, Components, @@ -63,8 +57,7 @@ export const ethUnitMap = { export type EtherUnits = keyof typeof ethUnitMap; -/** @internal */ -const bytesToBuffer = (data: Bytes): Buffer | never => { +export const bytesToBuffer = (data: Bytes): Buffer | never => { validator.validate(['bytes'], [data]); if (Buffer.isBuffer(data)) { @@ -75,8 +68,12 @@ const bytesToBuffer = (data: Bytes): Buffer | never => { return Buffer.from(data); } - if (typeof data === 'string') { - return Buffer.from(data.substr(2), 'hex'); + if (typeof data === 'string' && isHexStrict(data)) { + return Buffer.from(data.slice(2), 'hex'); + } + + if (typeof data === 'string' && !isHexStrict(data)) { + return Buffer.from(data, 'hex'); } throw new InvalidBytesError(data); @@ -255,6 +252,29 @@ export const toNumber = (value: Numbers): number | bigint => { return hexToNumber(numberToHex(value)); }; +/** + * Auto converts any given value into it's bigint representation + */ +export const toBigInt = (value: unknown): bigint => { + if (typeof value === 'number') { + return BigInt(value); + } + + if (typeof value === 'bigint') { + return value; + } + + if (typeof value === 'string' && !isHexStrict(value)) { + return BigInt(value); + } + + if (typeof value === 'string' && isHexStrict(value)) { + return BigInt(hexToNumber(value)); + } + + throw new InvalidNumberError(value); +}; + /** * Takes a number of wei and converts it to any other ether unit. */ @@ -418,67 +438,3 @@ export const jsonInterfaceMethodToString = ( return `${json.name}(${flattenTypes(false, json.inputs).join(',')})`; }; - -export const convertToValidType = ( - value: ValidReturnTypes[ValidTypes], // validate this - desiredType: ValidTypes, -) => { - if (value === undefined) throw new InvalidConvertibleValueError(); - - switch (desiredType) { - case ValidTypes.HexString: - return numberToHex(value); - case ValidTypes.NumberString: - return hexToNumberString(numberToHex(value)); - case ValidTypes.Number: - return toNumber(value); - case ValidTypes.BigInt: - return BigInt(toNumber(value)); - default: - throw new InvalidDesiredTypeError(desiredType); - } -}; - -// TODO Handle nested objects -export function convertObjectPropertiesToValidType< - // Object can have any properties. Unless specified as [key: string] index type didn't detect the record key correctly - // Also objects property types can be arrays, or other customs types as well so we have to specify any to cover all - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ObjectType extends Record, - Properties extends (keyof ObjectType)[], - ReturnType extends ValidTypes, ->( - object: ObjectType, - convertibleProperties: Properties, - desiredType: ReturnType, -): FormatValidReturnType { - if (typeof object !== 'object' || object === null) - throw new InvalidConvertibleObjectError(object); - if ( - !Array.isArray(convertibleProperties) || - !convertibleProperties.every(convertibleProperty => typeof convertibleProperty === 'string') - ) - throw new InvalidConvertiblePropertiesListError(convertibleProperties); - if (!Object.values(ValidTypes).includes(desiredType)) - throw new InvalidDesiredTypeError(desiredType); - - const convertedObject = { ...object } as FormatValidReturnType< - ObjectType, - Properties, - ReturnType - >; - - for (const convertibleProperty of convertibleProperties) { - if (convertedObject[convertibleProperty] === undefined) continue; - - // TODO Check why TS compiler is unable to detect the matching type of the deep properties - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error - convertedObject[convertibleProperty] = convertToValidType( - object[convertibleProperty], - desiredType, - ); - } - - return convertedObject; -} diff --git a/packages/web3-utils/src/errors.ts b/packages/web3-utils/src/errors.ts index ac36aed4927..ca300d891c2 100644 --- a/packages/web3-utils/src/errors.ts +++ b/packages/web3-utils/src/errors.ts @@ -38,6 +38,12 @@ export class InvalidBytesError extends Web3Error { } } +export class InvalidNumberError extends Web3Error { + public constructor(value: unknown) { + super(value, 'can not parse as number data'); + } +} + export class InvalidUnitError extends Web3Error { public constructor(value: unknown) { super(value, 'invalid unit'); @@ -103,27 +109,3 @@ export class InvalidTypeAbiInputError extends Web3Error { super(value, 'components found but type is not tuple'); } } - -export class InvalidDesiredTypeError extends Web3Error { - public constructor(value: unknown) { - super(value, 'invalid desired type for conversion given'); - } -} - -export class InvalidConvertibleObjectError extends Web3Error { - public constructor(value: unknown) { - super(value, 'invalid object for conversion given'); - } -} - -export class InvalidConvertiblePropertiesListError extends Web3Error { - public constructor(value: unknown) { - super(value, 'invalid list of convertible properties for conversion given'); - } -} - -export class InvalidConvertibleValueError extends Web3Error { - public constructor() { - super(undefined, 'cannot convert undefined'); - } -} diff --git a/packages/web3-utils/src/types.ts b/packages/web3-utils/src/types.ts index 96dd63686e9..3c08b469329 100644 --- a/packages/web3-utils/src/types.ts +++ b/packages/web3-utils/src/types.ts @@ -92,32 +92,6 @@ export type JsonEventInterface = { anonymous: boolean; }; -// TODO need to create Json Contract interface constructor, receive, and fallback - -export enum ValidTypes { - HexString = 'HexString', - NumberString = 'NumberString', - Number = 'Number', - BigInt = 'BigInt', -} - -export type ValidReturnTypes = { - [ValidTypes.HexString]: string; - [ValidTypes.NumberString]: string; - [ValidTypes.Number]: number; - [ValidTypes.BigInt]: bigint; -}; - -export type FormatValidReturnType< - ObjectType, - Properties extends (keyof ObjectType)[], - ReturnType extends ValidTypes, -> = { - [P in keyof ObjectType]: P extends Properties[number] - ? ValidReturnTypes[ReturnType] - : ObjectType[P]; -}; - // https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.json#L28 export interface Filter { readonly fromBlock?: BlockNumberOrTag; diff --git a/packages/web3-utils/test/fixtures/converters.ts b/packages/web3-utils/test/fixtures/converters.ts index a042c18865b..9f0e55adcb5 100644 --- a/packages/web3-utils/test/fixtures/converters.ts +++ b/packages/web3-utils/test/fixtures/converters.ts @@ -1,19 +1,5 @@ import { EtherUnits } from '../../src/converters'; -import { - InvalidConvertibleObjectError, - InvalidConvertiblePropertiesListError, - InvalidDesiredTypeError, - InvalidConvertibleValueError, -} from '../../src/errors'; -import { - Address, - Bytes, - HexString, - Numbers, - ValidTypes, - ValueTypes, - ValidReturnTypes, -} from '../../src/types'; +import { Address, Bytes, HexString, Numbers, ValueTypes } from '../../src/types'; export const bytesToHexValidData: [Bytes, HexString][] = [ [new Uint8Array([72]), '0x48'], @@ -264,270 +250,3 @@ export const toCheckSumValidData: [string, string][] = [ ['0x5fbc2b6c19ee3dd5f9af96ff337ddc89e30ceaef', '0x5FBc2b6C19EE3DD5f9Af96ff337DDC89e30ceAef'], ['0xa54D3c09E34aC96807c1CC397404bF2B98DC4eFb', '0xa54d3c09E34aC96807c1CC397404bF2B98DC4eFb'], ]; - -export const convertToValidTypeValidData: [ - ValidReturnTypes[ValidTypes], - ValidTypes, - ValidReturnTypes[ValidTypes], -][] = [ - ['0x2a', ValidTypes.HexString, '0x2a'], - ['42', ValidTypes.HexString, '0x2a'], - [42, ValidTypes.HexString, '0x2a'], - [BigInt('42'), ValidTypes.HexString, '0x2a'], - ['0x2a', ValidTypes.NumberString, '42'], - ['42', ValidTypes.NumberString, '42'], - [42, ValidTypes.NumberString, '42'], - [BigInt('42'), ValidTypes.NumberString, '42'], - ['0x2a', ValidTypes.Number, 42], - ['42', ValidTypes.Number, 42], - [42, ValidTypes.Number, 42], - [BigInt('42'), ValidTypes.Number, 42], - ['0x2a', ValidTypes.BigInt, BigInt('42')], - ['42', ValidTypes.BigInt, BigInt('42')], - [42, ValidTypes.BigInt, BigInt('42')], - [BigInt('42'), ValidTypes.BigInt, BigInt('42')], - ['-0x2a', ValidTypes.HexString, '-0x2a'], - ['-42', ValidTypes.HexString, '-0x2a'], - [-42, ValidTypes.HexString, '-0x2a'], - [BigInt('-42'), ValidTypes.HexString, '-0x2a'], - ['-0x2a', ValidTypes.NumberString, '-42'], - ['-42', ValidTypes.NumberString, '-42'], - [-42, ValidTypes.NumberString, '-42'], - [BigInt('-42'), ValidTypes.NumberString, '-42'], - ['-0x2a', ValidTypes.Number, -42], - ['-42', ValidTypes.Number, -42], - [-42, ValidTypes.Number, -42], - [BigInt('-42'), ValidTypes.Number, -42], - ['-0x2a', ValidTypes.BigInt, BigInt('-42')], - ['-42', ValidTypes.BigInt, BigInt('-42')], - [-42, ValidTypes.BigInt, BigInt('-42')], - [BigInt('-42'), ValidTypes.BigInt, BigInt('-42')], -]; - -export const convertToValidTypeInvalidData: [ - any, - any, - InvalidDesiredTypeError | InvalidConvertibleValueError | string, -][] = [ - ['0x2a', 'hexString', new InvalidDesiredTypeError('hexString')], - ['0x2a', 'qwerty', new InvalidDesiredTypeError('qwerty')], - ['0x2a', 42, new InvalidDesiredTypeError(42)], - ['0x2a', null, new InvalidDesiredTypeError(null)], - ['0x2a', undefined, new InvalidDesiredTypeError(undefined)], - ['0x2a', BigInt(0), new InvalidDesiredTypeError(BigInt(0))], - ['foo', ValidTypes.HexString, 'value "foo" at "/0" must pass "int" validation'], - ['4.2', ValidTypes.HexString, 'value "4.2" at "/0" must pass "int" validation'], - [null, ValidTypes.HexString, 'value at "/0" must pass "int" validation'], - [true, ValidTypes.HexString, 'value "true" at "/0" must pass "int" validation'], - [undefined, ValidTypes.HexString, new InvalidConvertibleValueError()], -]; - -export const convertObjectPropertiesToValidTypeValidData: [ - Record, // Starting object - string[], // List of keys to convert - ValidTypes, // Type to convert to - Record, // Expected result -][] = [ - [ - { - one: '42', - two: 42, - three: '0x2a', - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ['one', 'two', 'three', 'four'], - ValidTypes.HexString, - { - one: '0x2a', - two: '0x2a', - three: '0x2a', - four: '0x2a', - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ], - [ - { - one: '42', - two: 42, - three: '0x2a', - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ['one', 'two', 'three', 'four'], - ValidTypes.NumberString, - { - one: '42', - two: '42', - three: '42', - four: '42', - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ], - [ - { - one: '42', - two: 42, - three: '0x2a', - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ['one', 'two', 'three', 'four'], - ValidTypes.Number, - { - one: 42, - two: 42, - three: 42, - four: 42, - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ], - [ - { - one: '42', - two: 42, - three: '0x2a', - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ['one', 'two', 'three', 'four'], - ValidTypes.BigInt, - { - one: BigInt(42), - two: BigInt(42), - three: BigInt(42), - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ], - [ - { - one: '42', - two: 42, - three: '0x2a', - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ['one', 'two', 'three', 'four', 'eight'], // Testing undefined key 'eight' - ValidTypes.BigInt, - { - one: BigInt(42), - two: BigInt(42), - three: BigInt(42), - four: BigInt(42), - five: false, - six: { one: '42', two: 42, three: '0x2a' }, - }, - ], - [{}, ['one', 'two', 'three', 'four', 'eight'], ValidTypes.BigInt, {}], -]; - -export const convertObjectPropertiesToValidTypeInvalidData: [ - any, // Starting object - any, // List of keys to convert - any, // Type to convert to - InvalidDesiredTypeError | string, // Expected error -][] = [ - [ - { - one: 42, - two: false, - three: '0x2a', - }, - ['one', 'two', 'three'], - ValidTypes.HexString, - 'value "false" at "/0" must pass "int" validation', - ], - [ - { - one: 42, - two: '42', - // eslint-disable-next-line @typescript-eslint/no-empty-function - three: () => {}, - }, - ['one', 'two', 'three', 'four'], - ValidTypes.HexString, - 'value "() => { }" at "/0" must pass "int" validation', - ], - [ - undefined, - ['one', 'two', 'three', 'four'], - ValidTypes.HexString, - new InvalidConvertibleObjectError(undefined), - ], - [ - 42, - ['one', 'two', 'three', 'four'], - ValidTypes.HexString, - new InvalidConvertibleObjectError(42), - ], - [ - null, - ['one', 'two', 'three', 'four'], - ValidTypes.HexString, - new InvalidConvertibleObjectError(null), - ], - [ - { - one: 42, - two: '42', - }, - undefined, - ValidTypes.HexString, - new InvalidConvertiblePropertiesListError(undefined), - ], - [ - { - one: 42, - two: '42', - }, - 42, - ValidTypes.HexString, - new InvalidConvertiblePropertiesListError(42), - ], - [ - { - one: 42, - two: '42', - }, - ['one', 'two', 'three', 42], - ValidTypes.HexString, - new InvalidConvertiblePropertiesListError(['one', 'two', 'three', 42]), - ], - [ - { - one: 42, - two: '42', - }, - ['one', 'two', 'three', 'four'], - undefined, - new InvalidDesiredTypeError(undefined), - ], - [ - { - one: 42, - two: '42', - }, - ['one', 'two', 'three', 'four'], - 42, - new InvalidDesiredTypeError(42), - ], - [ - { - one: 42, - two: '42', - }, - ['one', 'two', 'three', 'four'], - 'hexString', - new InvalidDesiredTypeError('hexString'), - ], -]; diff --git a/packages/web3-utils/test/unit/converters.test.ts b/packages/web3-utils/test/unit/converters.test.ts index 84c8ef63ed9..2390ec2fde6 100644 --- a/packages/web3-utils/test/unit/converters.test.ts +++ b/packages/web3-utils/test/unit/converters.test.ts @@ -20,8 +20,6 @@ import { toUtf8, toWei, utf8ToHex, - convertToValidType, - convertObjectPropertiesToValidType, toChecksumAddress, } from '../../src/converters'; @@ -45,10 +43,6 @@ import { toWeiValidData, utf8ToHexInvalidData, utf8ToHexValidData, - convertToValidTypeValidData, - convertToValidTypeInvalidData, - convertObjectPropertiesToValidTypeValidData, - convertObjectPropertiesToValidTypeInvalidData, toCheckSumValidData, } from '../fixtures/converters'; @@ -340,44 +334,4 @@ describe('converters', () => { it.todo('should throw error for invalid cases'); }); }); - describe('convertToValidType', () => { - describe('valid cases', () => { - it.each(convertToValidTypeValidData)( - 'convert %s to %s and get %s', - (input, convertTo, output) => { - expect(convertToValidType(input, convertTo)).toEqual(output); - }, - ); - }); - describe('invalid cases', () => { - it.each(convertToValidTypeInvalidData)( - 'trying to convert %s to %s', - (input, convertTo, output) => { - expect(() => convertToValidType(input, convertTo)).toThrow(output); - }, - ); - }); - }); - describe('convertObjectPropertiesToValidType', () => { - describe('valid cases', () => { - it.each(convertObjectPropertiesToValidTypeValidData)( - 'valid cases', - (input, convertibleProperties, convertTo, output) => { - expect( - convertObjectPropertiesToValidType(input, convertibleProperties, convertTo), - ).toStrictEqual(output); - }, - ); - }); - describe('invalid cases', () => { - it.each(convertObjectPropertiesToValidTypeInvalidData)( - 'trying to convert %s with convertible properties: %s', - (input, convertibleProperties, convertTo, output) => { - expect(() => - convertObjectPropertiesToValidType(input, convertibleProperties, convertTo), - ).toThrow(output); - }, - ); - }); - }); }); diff --git a/packages/web3-validator/src/types.ts b/packages/web3-validator/src/types.ts index 32627cd0d99..804c2e2ace3 100644 --- a/packages/web3-validator/src/types.ts +++ b/packages/web3-validator/src/types.ts @@ -1,4 +1,4 @@ -import { ErrorObject } from 'ajv'; +import { ErrorObject, JSONSchemaType } from 'ajv'; import { AbiParameter } from './private_types'; export { JSONSchemaType } from 'ajv'; @@ -63,3 +63,12 @@ export interface Filter { readonly address?: string | string[]; readonly topics?: (string | string[] | null)[]; } + +// To avoid circular dependency to avoid breaking changes in "web3-utils" package. +export type Optional = Pick, K> & Omit; + +// In `JSONSchemaType` from `ajv` the `type` is required +// We need to make it optional +export type JsonSchema = Optional, 'type'> & { + readonly eth?: string; +}; diff --git a/packages/web3-validator/src/utils.ts b/packages/web3-validator/src/utils.ts index fe6a3335ba5..fa8dbf6d150 100644 --- a/packages/web3-validator/src/utils.ts +++ b/packages/web3-validator/src/utils.ts @@ -1,6 +1,7 @@ import { VALID_ETH_BASE_TYPES } from './constants'; import { FullValidationSchema, + JsonSchema, ShortValidationSchema, ValidationSchemaInput, ValidInputTypes, @@ -53,16 +54,6 @@ export const parseBaseType = ( return { baseType: strippedType as unknown as T, isArray, baseTypeSize, arraySizes }; }; -type JsonSchema = { - $id?: string; - type?: string; - eth?: string; - items?: JsonSchema | JsonSchema[]; - maxItems?: number; - minItems?: number; - additionalItems?: boolean; -}; - export const abiSchemaToJsonSchema = ( abis: ShortValidationSchema | FullValidationSchema, level?: number, diff --git a/packages/web3-validator/src/validation/index.ts b/packages/web3-validator/src/validation/index.ts index 1e9d6418529..ddb5768c470 100644 --- a/packages/web3-validator/src/validation/index.ts +++ b/packages/web3-validator/src/validation/index.ts @@ -8,3 +8,4 @@ export * from './filter'; export * from './numbers'; export * from './string'; export * from './topic'; +export * from './object'; diff --git a/packages/web3-validator/src/validation/object.ts b/packages/web3-validator/src/validation/object.ts new file mode 100644 index 00000000000..6bb8b8a5b04 --- /dev/null +++ b/packages/web3-validator/src/validation/object.ts @@ -0,0 +1,2 @@ +export const isObject = (item: unknown): item is Record => + typeof item === 'object' && item !== null && !Array.isArray(item) && !Buffer.isBuffer(item);