Skip to content

Commit

Permalink
Update Intermediate dApp Guide (EIP-6963 Types no Longer in Namespace) (
Browse files Browse the repository at this point in the history
  • Loading branch information
danforbes authored Oct 23, 2024
1 parent 4ca66af commit 70352cd
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions docs/docs/guides/07_dapps/intermediate-dapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,16 @@ Create a `src/useProviders.ts` file and add the following code:

```ts
import { useSyncExternalStore } from 'react';
import { providers, Web3 } from 'web3';
import {
type EIP6963ProviderDetail,
type EIP6963ProviderResponse,
type EIP6963ProvidersMapUpdateEvent,
Web3,
web3ProvidersMapUpdated,
} from 'web3';

// initial empty list of providers
let providerList: providers.EIP6963ProviderDetail[] = [];
let providerList: EIP6963ProviderDetail[] = [];

/**
* External store for subscribing to EIP-6963 providers
Expand All @@ -172,9 +178,9 @@ const providerStore = {
// subscribe to EIP-6963 provider events
subscribe: (callback: () => void) => {
// update the list of providers
function setProviders(response: providers.EIP6963ProviderResponse) {
function setProviders(response: EIP6963ProviderResponse) {
providerList = [];
response.forEach((provider: providers.EIP6963ProviderDetail) => {
response.forEach((provider: EIP6963ProviderDetail) => {
providerList.push(provider);
});

Expand All @@ -186,16 +192,15 @@ const providerStore = {
Web3.requestEIP6963Providers().then(setProviders);

// handler for newly discovered providers
function updateProviders(providerEvent: providers.EIP6963ProvidersMapUpdateEvent) {
function updateProviders(providerEvent: EIP6963ProvidersMapUpdateEvent) {
setProviders(providerEvent.detail);
}

// register handler for newly discovered providers with Web3.js helper function
Web3.onNewProviderDiscovered(updateProviders);

// return a function that unsubscribes from the created event listener
return () =>
window.removeEventListener(providers.web3ProvidersMapUpdated as any, updateProviders);
return () => window.removeEventListener(web3ProvidersMapUpdated as any, updateProviders);
},
};

Expand All @@ -209,7 +214,7 @@ This file exports a single member - a React [`useSyncExternalStore` hook](https:
Replace the contents of the `src/App.tsx` file with the following:

```tsx
import type { providers } from 'web3';
import type { EIP6963ProviderDetail } from 'web3';

import { useProviders } from './useProviders';

Expand All @@ -219,7 +224,7 @@ function App() {

return (
<>
{providers.map((provider: providers.EIP6963ProviderDetail) => {
{providers.map((provider: EIP6963ProviderDetail) => {
// list available providers
return (
<div key={provider.info.uuid}>
Expand All @@ -244,7 +249,7 @@ Replace the contents of the `src/App.tsx` file with the following:

```tsx
import { useEffect, useState } from 'react';
import { type providers, Web3 } from 'web3';
import { type EIP6963ProviderDetail, Web3 } from 'web3';

import { useProviders } from './useProviders';

Expand All @@ -258,7 +263,7 @@ function App() {
const [balances, setBalances] = useState<Map<string, number>>(new Map());

// click-handler for provider buttons
function setProvider(provider: providers.EIP6963ProviderDetail) {
function setProvider(provider: EIP6963ProviderDetail) {
const web3: Web3 = new Web3(provider.provider);
setWeb3(web3);
web3.eth.requestAccounts().then(setAccounts);
Expand Down Expand Up @@ -300,7 +305,7 @@ function App() {
<>
{web3 === undefined
? // no provider set, display list of available providers
providers.map((provider: providers.EIP6963ProviderDetail) => {
providers.map((provider: EIP6963ProviderDetail) => {
// for each provider, display a button to connect to that provider
return (
<div key={provider.info.uuid}>
Expand Down Expand Up @@ -456,7 +461,7 @@ Replace the contents of the `src/App.tsx` file with the following:

```tsx
import { useEffect, useState } from 'react';
import { type providers, Web3 } from 'web3';
import { type EIP6963ProviderDetail, Web3 } from 'web3';

// highlight-next-line
import TransferForm from './TransferForm';
Expand All @@ -472,7 +477,7 @@ function App() {
const [balances, setBalances] = useState<Map<string, number>>(new Map());

// click-handler for provider buttons
function setProvider(provider: providers.EIP6963ProviderDetail) {
function setProvider(provider: EIP6963ProviderDetail) {
const web3: Web3 = new Web3(provider.provider);
setWeb3(web3);
web3.eth.requestAccounts().then(setAccounts);
Expand Down Expand Up @@ -514,7 +519,7 @@ function App() {
<>
{web3 === undefined
? // no provider set, display list of available providers
providers.map((provider: providers.EIP6963ProviderDetail) => {
providers.map((provider: EIP6963ProviderDetail) => {
// for each provider, display a button to connect to that provider
return (
<div key={provider.info.uuid}>
Expand Down

1 comment on commit 70352cd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 70352cd Previous: 4ca66af Ratio
processingTx 23423 ops/sec (±6.45%) 22612 ops/sec (±6.67%) 0.97
processingContractDeploy 39870 ops/sec (±7.56%) 37950 ops/sec (±8.62%) 0.95
processingContractMethodSend 15457 ops/sec (±9.40%) 15758 ops/sec (±8.57%) 1.02
processingContractMethodCall 28186 ops/sec (±6.12%) 27523 ops/sec (±7.75%) 0.98
abiEncode 43507 ops/sec (±7.37%) 44232 ops/sec (±7.00%) 1.02
abiDecode 30457 ops/sec (±8.39%) 29126 ops/sec (±7.29%) 0.96
sign 1491 ops/sec (±3.95%) 1529 ops/sec (±1.12%) 1.03
verify 359 ops/sec (±0.70%) 356 ops/sec (±1.00%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.