Skip to content

Commit

Permalink
Merge branch 'events-guide-6741' of https://github.com/web3/web3.js i…
Browse files Browse the repository at this point in the history
…nto events-guide-6741
  • Loading branch information
SantiagoDevRel committed Apr 18, 2024
2 parents a9e4290 + 4e732db commit 19ecdec
Show file tree
Hide file tree
Showing 53 changed files with 1,333 additions and 352 deletions.
103 changes: 87 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
#### web3-eth

- Ensure provider.supportsSubscriptions exists before watching by subscription (#6440)
- Fixed param sent to `checkRevertBeforeSending` in `sendSignedTransaction`
- Fixed param sent to `checkRevertBeforeSending` in `sendSignedTransaction`
- Fixed `defaultTransactionBuilder` for value issue (#6509)

#### web3-eth-abi
Expand All @@ -2129,7 +2129,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-eth-accounts

- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344)
- Fixed `recover` function, `v` will be normalized to value 0,1 (#6344)

#### web3-providers-http

Expand Down Expand Up @@ -2252,27 +2252,26 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-eth-contract

- 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.3.0]

### Changed

#### web3-core

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

#### web3-eth-contracts

- 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)

### Added


#### web3-utils

- `SocketProvider` now contains public function `getPendingRequestQueueSize`, `getSentRequestsQueueSize` and `clearQueues` (#6479)
- Added `safeDisconnect` as a `SocketProvider` method to disconnect only when request queue size and send request queue size is 0 (#6479)
- Added `safeDisconnect` as a `SocketProvider` method to disconnect only when request queue size and send request queue size is 0 (#6479)
- Add `isContractInitOptions` method (#6555)

#### web3
Expand Down Expand Up @@ -2305,7 +2304,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-eth-contract

- 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)

#### web3

Expand All @@ -2324,8 +2323,8 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-eth-contract

- 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)

#### web3-utils

Expand All @@ -2347,7 +2346,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

- Adds missing exported type `AbiItem` from 1.x to v4 for compatabiltiy (#6678)

### Changed
### Changed

#### web3

Expand All @@ -2359,7 +2358,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3

- Added EIP-6963 utility function `requestEIP6963Providers` for multi provider discovery
- Added EIP-6963 utility function `requestEIP6963Providers` for multi provider discovery

#### web3-eth

Expand Down Expand Up @@ -2393,16 +2392,88 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

#### web3-eth-ens

- Added function getText and getName in ENS and resolver classes (#6914)
- Added function getText and getName in ENS and resolver classes (#6914)

### fixed

#### web3-validator

- Multi-dimensional arrays(with a fix length) are now handled properly when parsing ABIs (#6798)
- Multi-dimensional arrays(with a fix length) are now handled properly when parsing ABIs (#6798)

#### web3-utils

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

## [4.8.0]

### Changed

#### web3-eth-abi

- Dependencies updated

#### web3-eth-accounts

- Dependencies updated

### Fixed

#### web3-eth-contract

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

#### web3-utils

- fixed toHex incorrectly hexing Uint8Arrays and Buffer (#6957)
- fixed isUint8Array not returning true for Buffer (#6957)

### Added

#### web3-eth-contract

- 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)

#### web3-eth

- method `getBlock` now includes properties of eip 4844, 4895, 4788 when returning block (#6933)
- update type `withdrawalsSchema`, `blockSchema` and `blockHeaderSchema` schemas to include properties of eip 4844, 4895, 4788 (#6933)

#### web3-types

- Added `signature` to type `AbiFunctionFragment` (#6922)
- update type `Withdrawals`, `block` and `BlockHeaderOutput` to include properties of eip 4844, 4895, 4788 (#6933)

## [Unreleased]

### Added

#### 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)
- update type `Withdrawals`, `block` and `BlockHeaderOutput` to include properties of eip 4844, 4895, 4788 (#6933)

### Fixed

#### web3-validator

## [Unreleased]
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,
...
*/
```
```


### 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',
}
```
3 changes: 3 additions & 0 deletions docs/docs/guides/web3_upgrade_guide/1.x/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ It will not have:
- `givenProvider` default value is `undefined` instead of `null`
- `currentProvider` default value is `undefined` instead of `null` (if web3 is instantiated without a provider)

:::warning
In version 4.x, all numbers return as BigInt instead of string or number, which constitutes a breaking change for users accustomed to handling numbers as string or number in their code. For instance, web3.eth.getBalance will now return BigInt instead of string or number. If you wish to retain numbers as number or string, you can refer to [this guide](/guides/web3_config/#defaultreturnformat) on how to set returning types in web3js 4.x.
:::
### Web3 BatchRequest

```ts
Expand Down
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)
17 changes: 16 additions & 1 deletion packages/web3-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 { HexString, Transaction } from 'web3-types';
import { HexString, JsonRpcResponse, Transaction, Web3APIMethod, Web3APIRequest, Web3APIReturnType } from 'web3-types';

export type TransactionTypeParser = (
transaction: Transaction,
Expand All @@ -30,3 +30,18 @@ export interface ExtensionObject {
property?: string;
methods: Method[];
}

export interface RequestManagerMiddleware<API> {
processRequest<
AnotherMethod extends Web3APIMethod<API>
>(
request: Web3APIRequest<API, AnotherMethod>,
options?: { [key: string]: unknown }): Promise<Web3APIRequest<API, AnotherMethod>>;

processResponse<
AnotherMethod extends Web3APIMethod<API>,
ResponseType = Web3APIReturnType<API, AnotherMethod>>
(
response: JsonRpcResponse<ResponseType>,
options?: { [key: string]: unknown }): Promise<JsonRpcResponse<ResponseType>>;
}
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
10 changes: 9 additions & 1 deletion packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { isNullish } from 'web3-utils';
import { BaseTransaction, TransactionFactory } from 'web3-eth-accounts';
import { isSupportedProvider } from './utils.js';
// eslint-disable-next-line import/no-cycle
import { ExtensionObject } from './types.js';
import { ExtensionObject, RequestManagerMiddleware } from './types.js';
import { Web3BatchRequest } from './web3_batch_request.js';
// eslint-disable-next-line import/no-cycle
import { Web3Config, Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js';
Expand Down Expand Up @@ -65,6 +65,7 @@ export type Web3ContextInitOptions<
registeredSubscriptions?: RegisteredSubs;
accountProvider?: Web3AccountProvider<Web3BaseWalletAccount>;
wallet?: Web3BaseWallet<Web3BaseWalletAccount>;
requestManagerMiddleware?: RequestManagerMiddleware<API>;
};

// eslint-disable-next-line no-use-before-define
Expand Down Expand Up @@ -129,6 +130,7 @@ export class Web3Context<
registeredSubscriptions,
accountProvider,
wallet,
requestManagerMiddleware
} = providerOrContext as Web3ContextInitOptions<API, RegisteredSubs>;

this.setConfig(config ?? {});
Expand All @@ -138,6 +140,7 @@ export class Web3Context<
new Web3RequestManager<API>(
provider,
config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout,
requestManagerMiddleware
);

if (subscriptionManager) {
Expand Down Expand Up @@ -352,6 +355,11 @@ export class Web3Context<
this.provider = provider;
return true;
}

public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>){
this.requestManager.setMiddleware(requestManagerMiddleware);
}

/**
* Will return the {@link Web3BatchRequest} constructor.
*/
Expand Down
Loading

0 comments on commit 19ecdec

Please sign in to comment.