Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit bb5ac86

Browse files
authored
Merge branch '4.x' into fix-native
2 parents 111bcfb + ca31f6a commit bb5ac86

File tree

11 files changed

+154
-97
lines changed

11 files changed

+154
-97
lines changed

.github/CONTRIBUTING.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Contributing
22

3-
Thank you for contributing to web3.js! We appreciate your interest and welcome any contributions that can help improve our documentation, functionality, addressing bugs, or any other aspects that can contribute to the overall improvement of our project. Before you start contributing, please take a moment to review the guidelines below.
3+
Thank you for contributing to Web3.js! We appreciate your interest and welcome any contributions that can help improve our documentation, functionality, address bugs, or any other aspects that can contribute to the overall improvement of our project. Before you start contributing, please take a moment to review the guidelines below.
44

55
## Help and Support
66

7-
If you face any issues while contributing, or you want any type of support, we encourage you to join our [Discord Community](https://discord.com/invite/3shNX8cqGR) and ask any question in the `#web3js-general` channel, and/or submit a [New Issue](https://github.com/web3/web3.js/issues/new).
7+
If you face any issues while contributing or want any type of support, we encourage you to join our [Discord Community](https://discord.com/invite/3shNX8cqGR), ask any question in the `#web3js-general` channel, and/or submit a [new issue](https://github.com/web3/web3.js/issues/new).
88

99
## Prerequisites
1010

@@ -15,12 +15,12 @@ If you face any issues while contributing, or you want any type of support, we e
1515

1616
1. **Fork the docs:** Start by forking our repository to your GitHub account.
1717

18-
2. **Clone the repo:** Clone the forked repository to your local machine using the following command
18+
2. **Clone the repo:** Clone the forked repository to your local machine using the following command:
1919
```bash
2020
git clone https://github.com/your-username/web3.js.git
2121
```
2222
3. **Create a Branch:** Create a new branch for your changes with a descriptive name.
23-
**NOTE: The branch name must include the issue number (if there is no issue created for your contribution, please create one)**
23+
**NOTE: The branch name must include the issue number (if there is no issue created for your contribution, please create one).**
2424
```bash
2525
git checkout -b issue-name-1234
2626
```
@@ -32,7 +32,7 @@ yarn
3232
```
3333
6. **Make your changes:**...
3434

35-
7. Check changes in local environment: Run the command `yarn start` and you'll see a local environment in `localhost:3000` with the docs
35+
7. Check changes in the local environment: Run the command `yarn start` and you'll see a local environment in `localhost:3000` with the documents.
3636

3737
8. **Commit your changes:** `git add .` and `git commit -m 'descriptive msg'`
3838

@@ -41,9 +41,9 @@ yarn
4141
git push origin branch-name
4242
```
4343

44-
10. **Open a Pull request(PR):** Provide a detailed description of your changes, the problem you are solving, and any additional context(you can use the PR template).
44+
10. **Open a Pull Request (PR):** Provide a detailed description of your changes, the problem you are solving, and any additional context (you can use the PR template).
4545

46-
11. **Wait for review**: Before merging any branch into the main branch, it must be approved by 2 devs, after succesfully approved, you can `Squash and merge` your branch, Please be responsive to any feedback on your pull request and make necessary changes based on the review.
46+
11. **Wait for review**: Before merging any branch into the main branch, it must be approved by two devs, after it is successfully approved, you can `Squash and merge` your branch, Please be responsive to any feedback on your pull request and make necessary changes based on the review.
4747

4848
## Guidelines for Pull Requests and Releases (Web3 4.x)
4949

@@ -54,25 +54,25 @@ that **Web3 not break**.
5454
### Pull Requests for substantive changes (e.g. everything except comments and docs)
5555

5656
1. Any PR that introduces a logic change should include tests. (In many cases, the tests will take more time to write than the actual code).
57-
1. All PRs should sit for 72 hours with the `pleasereview` tag in order to garner feedback.
57+
1. All PRs should sit for 72 hours with the `please review` tag to garner feedback.
5858
1. No PR should be merged until it has been reviewed, passes CI, and all reviews' comments are
5959
addressed.
6060
1. PRs should:
6161
1. have a narrow, well-defined focus.
6262
1. make the smallest set of changes possible to achieve their goal.
6363
1. include a clear description in the opening comment.
6464
1. preserve the conventions and stylistic consistency of any files they modify.
65-
1. Given the choice between a conservative change that mostly works and an adventurous change which seems better but introduces uncertainty - prefer the conservative change.
65+
1. Given the choice between a conservative change that mostly works and an adventurous change that seems better but introduces uncertainty - prefer the conservative change.
6666

6767
### Reviews
6868

69-
The end goal of review is to suggest useful improvements to the author. Reviews should finish with approval unless there are issues that would result in:
69+
The end goal of the review is to suggest useful improvements to the author. Reviews should finish with approval unless there are issues that would result in:
7070

71-
1. Buggy behaviour.
71+
1. Buggy behavior.
7272
1. Undue maintenance burden.
73-
1. Pessimisation (i.e. speed reduction / meaningful build-size increases).
73+
1. Pessimisation (i.e. speed reduction or meaningful build-size increases).
7474
1. Feature reduction (i.e. it removes some aspect of functionality that users rely on).
75-
1. Avoidable risk (i.e it's difficult to test or hard to anticipate the implications of, without
75+
1. Avoidable risk (i.e. it's difficult to test or hard to anticipate the implications of, without
7676
being strictly necessary to fix something broken).
7777

7878
Read more in [Review Guidelines](./REVIEW.md).
@@ -85,9 +85,9 @@ Read more in [Review Guidelines](./REVIEW.md).
8585
1. Changes should trigger a new `rc` release and set the release clock back enough that reviewers have the time they need to test new changes.
8686
1. Regular maintainers should manually test the `rc` against a Node project and the published
8787
minified bundle in a browser context. An external reviewer should verify they've done the same.
88-
1. A release PR must be approved at least by two known contributors of the web3.js project.
88+
1. A release PR must be approved at least by two known contributors to the web3.js project.
8989

90-
Read more in [Release Guidelines](./RELEASE.md).
90+
Read more in the [Release Guidelines](./RELEASE.md).
9191

9292
### Emergencies
9393

.github/workflows/build.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ jobs:
8989
uses: actions/cache/restore@v3
9090
with:
9191
path: packages/web3/dist/4.x.json
92-
key: web3-bundle-stats-4x-${{github.sha}}
92+
key: web3-bundle-stats-4x-${{github.event.pull_request.base.sha}}
9393
- run: yarn build:web:analyze
9494
env:
9595
STATS_FILE: ${{ github.ref_name }}.json
9696
- name: Compare bundle stats
97-
uses: github/webpack-bundlesize-compare-action@v1
98-
if: github.event_name != 'push'
97+
uses: github/webpack-bundlesize-compare-action@v1.8.2
9998
continue-on-error: true
10099
with:
101100
github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -105,9 +104,10 @@ jobs:
105104
uses: actions/cache/save@v3
106105
if: github.event_name == 'push' && github.ref == 'refs/heads/4.x'
107106
with:
108-
path: packages/web3/dist/4.x.json
107+
path: packages/web3/dist/${{ github.ref_name }}.json
109108
key: web3-bundle-stats-4x-${{github.sha}}
110109

110+
111111
unit:
112112
name: Unit Tests
113113
needs: build
@@ -239,7 +239,7 @@ jobs:
239239
path: ./
240240
key: web3-18-${{github.sha}}
241241
# @octokit/core not supported on node 16, so I can't add it to the package.json
242-
- run: npm install --no-package-lock --no-save --force @octokit/core
242+
- run: npm install --no-package-lock --no-save --force @octokit/core@5.1.0
243243
- name: Restore main branch benchmark data
244244
uses: actions/cache/restore@v3
245245
with:

docs/docs/guides/web3_providers_guide/eip6963.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ sidebar_label: 'EIP-6963: Multi Injected Provider Discovery'
99

1010
EIP-6963 proposes the "Multi Injected Provider Discovery" standard, which aims to enhance the discoverability and interaction with multiple injected Ethereum providers in a browser environment. Injected providers refer to browser extensions or other injected scripts that provide access to an Ethereum provider within the context of a web application.
1111

12-
Web3.js library has utility function for discovery of injected providers using `requestEIP6963Providers()` function. When `requestEIP6963Providers()` is used it returns `eip6963Providers` Map object. This Map object is in global scope so every time `requestEIP6963Providers()` function is called it will update Map object and return it.
12+
Web3.js library has utility functions for discovery of injected providers using `requestEIP6963Providers()` and `onNewProviderDiscovered(eventDetails)`.
1313

14-
`eip6963Providers` Map object has provider's `UUID` as keys and `EIP6963ProviderDetail` as values. `EIP6963ProviderDetail` is:
14+
`onNewProviderDiscovered(eventDetails)` can be used to subscribe to events of provider discovery & providers map update and `requestEIP6963Providers()` returns Promise object that resolves to `Map<string, EIP6963ProviderDetail>` object containing list of providers. For updated providers `eip6963:providersMapUpdated` event is emitted and it has updated Map object. This event can be subscribed as mentioned earlier using `onNewProviderDiscovered(eventDetails)`
15+
16+
`eip6963ProvidersMap` object has provider's `UUID` as keys and `EIP6963ProviderDetail` as values. `EIP6963ProviderDetail` is:
1517

1618
```ts
1719
export interface EIP6963ProviderDetail {
@@ -40,8 +42,15 @@ Following code snippet demonstrates usage of `requestEIP6963Providers()` functio
4042

4143
import { Web3 } from 'web3';
4244

43-
const providers = Web3.requestEIP6963Providers();
45+
// Following will subscribe to event that will be triggered when providers map is updated.
46+
47+
Web3.onNewProviderDiscovered((provider) => {
48+
console.log(provider.detail); // This will log the populated providers map object, provider.detail has Map of all providers yet discovered
49+
// add logic here for updating UI of your DApp
50+
});
4451

52+
// Call the function and wait for the promise to resolve
53+
let providers = await Web3.requestEIP6963Providers();
4554
for (const [key, value] of providers) {
4655
console.log(value);
4756

docs/docs/guides/web3_upgrade_guide/1.x/accounts_migration_guide.md

+11
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ const wallet = web3.eth.accounts.wallet.create(1, 'optionalEntropy'); // entropy
3434
const account = web3.eth.accounts.wallet.create(1, 'optionalEntropy'); // will result in an error
3535
const account = web3.eth.accounts.wallet.create(); // correct way
3636
```
37+
38+
## stripHexPrefix method
39+
40+
In 1.x `stripHexPrefix` method is located in the `web3-utils` package, in 4.x this has been moved to `web3-eth-accounts`
41+
42+
```typescript
43+
import { stripHexPrefix } from 'web3-eth-accounts';
44+
45+
console.log(stripHexPrefix('0x123')); // "123"
46+
47+
```

docs/docs/guides/web3_upgrade_guide/1.x/web3_utils_migration_guide.md

+10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ isHex('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `fal
7979

8080
isHexStrict('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `false`
8181
// `false`
82+
```
83+
## stripHexPrefix method
84+
85+
In 1.x `stripHexPrefix` method is located in the `web3-utils` package, in 4.x this has been moved to `web3-eth-accounts`
86+
87+
```typescript
88+
import { stripHexPrefix } from 'web3-eth-accounts';
89+
90+
console.log(stripHexPrefix('0x123')); // "123"
91+
8292
```
8393

8494
## Other functions

packages/web3-utils/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export * from './web3_eip1193_provider.js';
3333
export * from './socket_provider.js';
3434
export * from './uint8array.js';
3535
// for backwards compatibility with v1
36-
export {AbiItem} from 'web3-types';
36+
export { AbiItem } from 'web3-types';

packages/web3-utils/src/objects.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const mergeDeep = (
3636
destination: Record<string, unknown>,
3737
...sources: Record<string, unknown>[]
3838
): Record<string, unknown> => {
39-
const result = destination; // clone deep here
39+
const result = { ...destination }; // clone deep here
4040
if (!isIterable(result)) {
4141
return result;
4242
}

packages/web3/src/providers.exports.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export { Eip1193Provider, SocketProvider } from 'web3-utils';
1919

2020
export * as http from 'web3-providers-http';
2121
export * as ws from 'web3-providers-ws';
22+
export * from './web3_eip6963.js';

packages/web3/src/web3.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import abi from './abi.js';
4444
import { initAccountsForContext } from './accounts.js';
4545
import { Web3EthInterface } from './types.js';
4646
import { Web3PkgInfo } from './version.js';
47-
import { requestEIP6963Providers } from './web3_eip6963.js';
47+
import { onNewProviderDiscovered, requestEIP6963Providers } from './web3_eip6963.js';
4848

4949
export class Web3<
5050
CustomRegisteredSubscription extends {
@@ -54,6 +54,7 @@ export class Web3<
5454
public static version = Web3PkgInfo.version;
5555
public static utils = utils;
5656
public static requestEIP6963Providers = requestEIP6963Providers;
57+
public static onNewProviderDiscovered = onNewProviderDiscovered;
5758
public static modules = {
5859
Web3Eth,
5960
Iban,

packages/web3/src/web3_eip6963.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,48 @@ export interface EIP6963RequestProviderEvent extends Event {
4444
type: Eip6963EventName.eip6963requestProvider;
4545
}
4646

47-
export const eip6963Providers: Map<string, EIP6963ProviderDetail> = new Map();
47+
export const eip6963ProvidersMap: Map<string, EIP6963ProviderDetail> = new Map();
4848

49-
export const requestEIP6963Providers = () => {
49+
export const web3ProvidersMapUpdated = "web3:providersMapUpdated";
50+
export interface EIP6963ProvidersMapUpdateEvent extends CustomEvent {
51+
type: string;
52+
detail: Map<string, EIP6963ProviderDetail>;
53+
}
5054

51-
if (typeof window === 'undefined')
52-
throw new Error(
53-
"window object not available, EIP-6963 is intended to be used within a browser"
54-
);
55+
export const requestEIP6963Providers = async () =>
56+
new Promise((resolve, reject) => {
57+
if (typeof window === 'undefined') {
58+
reject(new Error("window object not available, EIP-6963 is intended to be used within a browser"));
59+
}
5560

5661
window.addEventListener(
5762
Eip6963EventName.eip6963announceProvider as any,
5863
(event: EIP6963AnnounceProviderEvent) => {
5964

60-
eip6963Providers.set(
65+
eip6963ProvidersMap.set(
6166
event.detail.info.uuid,
6267
event.detail);
68+
69+
const newEvent: EIP6963ProvidersMapUpdateEvent = new CustomEvent(
70+
web3ProvidersMapUpdated,
71+
{ detail: eip6963ProvidersMap }
72+
);
73+
74+
window.dispatchEvent(newEvent);
75+
resolve(eip6963ProvidersMap);
76+
6377
}
6478
);
6579

6680
window.dispatchEvent(new Event(Eip6963EventName.eip6963requestProvider));
6781

68-
return eip6963Providers;
69-
}
82+
});
7083

7184

85+
export const onNewProviderDiscovered = (callback: (providerEvent: EIP6963AnnounceProviderEvent) => void) => {
86+
if (typeof window === 'undefined') {
87+
throw new Error("window object not available, EIP-6963 is intended to be used within a browser");
88+
}
89+
window.addEventListener(web3ProvidersMapUpdated as any, callback );
90+
}
91+

0 commit comments

Comments
 (0)