Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default return format to the context #6947

Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2405,4 +2405,39 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

- fixed erroneous parsing of big numbers in the `toNumber(...)` function (#6880)

## [Unreleased]
## [Unreleased]

#### web3-core

- `defaultReturnFormat` was added to the configuration options. (#6947)

#### web3-eth

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

#### web3-eth-contract

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

#### web3-eth-ens

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

#### web3-net

- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)

#### web3-types

- Added `signature` to type `AbiFunctionFragment` (#6922)

### Fixed

#### web3-eth-contract


### Changed

#### web3-utils

- Method `format` was changed. Now it has default value `DEFAULT_RETURN_FORMAT` for `returnFormat` parameter (#6947)
45 changes: 44 additions & 1 deletion docs/docs/guides/web3_config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ There is list of configuration params that can be set for modifying behavior of
- [defaultHardfork](/api/web3-core/class/Web3Config#defaultHardfork)
- [defaultCommon](/api/web3-core/class/Web3Config#defaultCommon)
- [defaultTransactionType](/api/web3-core/class/Web3Config#defaultTransactionType)
- [defaultReturnFormat](/api/web3-core/class/Web3Config#defaultReturnFormat)

## Global level Config

Expand Down Expand Up @@ -198,4 +199,46 @@ console.log(web3.getContextObject().config)
transactionPollingTimeout: 750000,
...
*/
```
```


avkos marked this conversation as resolved.
Show resolved Hide resolved
### defaultReturnFormat
The `defaultReturnFormat` allows users to specify the format in which certain types of data should be returned by default. It is a configuration parameter that can be set at the global level and affects how data is returned across the entire library.
```ts
import { Web3, FMT_NUMBER, FMT_BYTES } from 'web3';

web3.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
};

```
:::info
The `defaultReturnFormat` can be configured both globally and at the package level:
```ts
import { Web3Eth, FMT_NUMBER, FMT_BYTES } from 'web3-eth';

const eth = new Web3Eth()
eth.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
};

```
:::
#### All available choices for numeric data:
```ts
export enum FMT_NUMBER {
NUMBER = 'NUMBER_NUMBER',
HEX = 'NUMBER_HEX',
STR = 'NUMBER_STR',
BIGINT = 'NUMBER_BIGINT',
}
```
#### All available choices for bytes data:
```ts
export enum FMT_BYTES {
HEX = 'BYTES_HEX',
UINT8ARRAY = 'BYTES_UINT8ARRAY',
}
```
5 changes: 4 additions & 1 deletion packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,7 @@ Documentation:

- Web3config `contractDataInputFill` has been defaulted to `data`, istead of `input`. (#6622)

## [Unreleased]
## [Unreleased]

### Added
- `defaultReturnFormat` was added to the configuration options. (#6947)
20 changes: 19 additions & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { Numbers, HexString, BlockNumberOrTag, Common } from 'web3-types';
import {
Numbers,
HexString,
BlockNumberOrTag,
Common,
DEFAULT_RETURN_FORMAT,
DataFormat,
} from 'web3-types';
import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors';
import { isNullish, toHex } from 'web3-utils';
import { TransactionTypeParser } from './types.js';
Expand Down Expand Up @@ -52,6 +59,7 @@ export interface Web3ConfigOptions {
};
transactionBuilder?: TransactionBuilder;
transactionTypeParser?: TransactionTypeParser;
defaultReturnFormat: DataFormat;
}

type ConfigEvent<T, P extends keyof T = keyof T> = P extends unknown
Expand Down Expand Up @@ -93,6 +101,7 @@ export abstract class Web3Config
},
transactionBuilder: undefined,
transactionTypeParser: undefined,
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
};

public constructor(options?: Partial<Web3ConfigOptions>) {
Expand Down Expand Up @@ -348,6 +357,15 @@ export abstract class Web3Config
this.config.maxListenersWarningThreshold = val;
}

public get defaultReturnFormat() {
return this.config.defaultReturnFormat;
}
public set defaultReturnFormat(val) {
this._triggerConfigChange('defaultReturnFormat', val);

this.config.defaultReturnFormat = val;
}

public get defaultNetworkId() {
return this.config.defaultNetworkId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ exports[`Web3Context getContextObject should return correct context object 1`] =
"defaultHardfork": "london",
"defaultMaxPriorityFeePerGas": "0x9502f900",
"defaultNetworkId": undefined,
"defaultReturnFormat": {
"bytes": "BYTES_HEX",
"number": "NUMBER_BIGINT",
},
"defaultTransactionType": "0x2",
"enableExperimentalFeatures": {
"useRpcCallSpecification": false,
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-core/test/unit/web3_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { toHex } from 'web3-utils';
import { DEFAULT_RETURN_FORMAT } from 'web3-types';
import { Web3Config, Web3ConfigEvent } from '../../src/web3_config';

class MyConfigObject extends Web3Config {}
Expand Down Expand Up @@ -44,6 +45,7 @@ const defaultConfig = {
transactionConfirmationPollingInterval: undefined,
defaultTransactionType: '0x2',
defaultMaxPriorityFeePerGas: toHex(2500000000),
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
};
const setValue = {
string: 'newValue',
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/test/config/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ require('jest-extended');

process.env.NODE_ENV = 'test';

const jestTimeout = 10000;
const jestTimeout = 15000;

jest.setTimeout(jestTimeout);
19 changes: 10 additions & 9 deletions packages/web3-eth-contract/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ Documentation:
- Added `dataInputFill` as a ContractInitOption, allowing users to have the choice using property `data`, `input` or `both` for contract methods to be sent to the RPC provider. (#6355)
- Added to `Web3Config` property `contractDataInputFill` allowing users to have the choice using property `data`, `input` or `both` for contract methods to be sent to the RPC provider when creating contracts. (#6377)


## [4.1.1]

### Changed
Expand All @@ -345,24 +344,24 @@ Documentation:

### Fixed

- Will populate `data` for transactions in contract for metamask provider instead of `input` (#6534)
- Will populate `data` for transactions in contract for metamask provider instead of `input` (#6534)

## [4.1.4]

### Changed

- By default, contracts will fill `data` instead of `input` within method calls (#6622)
- By default, contracts will fill `data` instead of `input` within method calls (#6622)

## [4.2.0]

### Changed

- Allow the `deploy` function to accept parameters, even when no ABI was provided to the `Contract`(#6635)
- Allow the `deploy` function to accept parameters, even when no ABI was provided to the `Contract`(#6635)

### Fixed

- Fix and error that happen when trying to get past events by calling `contract.getPastEvents` or `contract.events.allEvents()`, if there is no matching events. (#6647)
- Fixed: The Contract is not using the context wallet passed if context was passed at constructor. (#6661)
- Fix and error that happen when trying to get past events by calling `contract.getPastEvents` or `contract.events.allEvents()`, if there is no matching events. (#6647)
- Fixed: The Contract is not using the context wallet passed if context was passed at constructor. (#6661)

## [4.3.0]

Expand All @@ -374,9 +373,11 @@ Documentation:

### Fixed

- Fix an issue with smart contract function overloading (#6922)
- Fix an issue with smart contract function overloading (#6922)

### Added

- Added a console warning in case of an ambiguous call to a solidity method with parameter overloading (#6942)
- Added contract.deploy(...).decodeData(...) and contract.decodeMethodData(...) that decode data based on the ABI (#6950)
- `defaultReturnFormat` was added to all methods that have `ReturnType` param. (#6947)
- Add a console warning in case of an ambiguous call to a solidity method with parameter overloading (#6942)
- Added a console warning in case of an ambiguous call to a solidity method with parameter overloading (#6942)
- Added contract.deploy(...).decodeData(...) and contract.decodeMethodData(...) that decode data based on the ABI (#6950)
Loading
Loading