generated from web3/web3.js-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
203 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
packages/example-react-app/src/components/AccountDetail.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { type MutableRefObject, useContext, useEffect, useRef, useState } from 'react'; | ||
|
||
import { type IWeb3Context, Web3Context } from '../web3/Web3Context'; | ||
|
||
export function AccountDetail({ address }: { address: string }) { | ||
const web3Context: IWeb3Context = useContext(Web3Context); | ||
|
||
const [balance, setBalance] = useState<number>(NaN); | ||
const subscriptionId: MutableRefObject<string | undefined> = useRef(undefined); | ||
|
||
async function updateBalance(): Promise<void> { | ||
const newBalance = await web3Context.web3.eth.getBalance(address); | ||
|
||
setBalance(parseFloat(web3Context.web3.utils.fromWei(newBalance, 'ether'))); | ||
} | ||
|
||
useEffect(() => { | ||
async function subscribeToNewBlockHeaders() { | ||
const newBlockSubscription = | ||
await web3Context.web3.eth.subscribe('newBlockHeaders'); | ||
|
||
subscriptionId.current = newBlockSubscription.id; | ||
|
||
newBlockSubscription.on('data', () => { | ||
void updateBalance(); | ||
}); | ||
} | ||
|
||
void subscribeToNewBlockHeaders(); | ||
|
||
return () => { | ||
void web3Context.web3.eth.subscriptionManager.unsubscribe( | ||
({ id }) => subscriptionId.current === id, | ||
); | ||
}; | ||
}); | ||
|
||
return ( | ||
<> | ||
<div>{address}</div> | ||
<div>Balance in native token: {`${balance}`}</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
packages/example-react-app/src/components/ProviderButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useContext } from 'react'; | ||
import type { EIP6963ProviderDetail } from 'web3'; | ||
|
||
import type { IWeb3Context } from '../web3/Web3Context'; | ||
import { Web3Context } from '../web3/Web3Context'; | ||
|
||
import './ProviderButton.css'; | ||
|
||
export function ProviderButton({ provider }: { provider: EIP6963ProviderDetail }) { | ||
const web3Context: IWeb3Context = useContext(Web3Context); | ||
|
||
return ( | ||
<button type="button" onClick={() => web3Context.setCurrentProvider(provider)}> | ||
<img src={provider.info.icon} alt={provider.info.name} width="35" /> | ||
<span> {provider.info.name}</span> | ||
</button> | ||
); | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/example-react-app/src/wallet-components/AddEthereumChain.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { AddEthereumChainRequest } from 'web3-plugin-wallet-rpc'; | ||
import { Web3Context } from '../web3/Web3Context'; | ||
import { useContext, useState } from 'react'; | ||
|
||
const chains: Record<string, AddEthereumChainRequest> = { | ||
mantle: { | ||
chainId: 5000, | ||
blockExplorerUrls: ['https://mantlescan.xyz'], | ||
chainName: 'Mantle', | ||
iconUrls: ['https://icons.llamao.fi/icons/chains/rsz_mantle.jpg'], | ||
nativeCurrency: { | ||
name: 'Mantle', | ||
symbol: 'MNT', | ||
decimals: 18, | ||
}, | ||
rpcUrls: ['https://rpc.mantle.xyz'], | ||
}, | ||
scroll: { | ||
chainId: 534352, | ||
blockExplorerUrls: ['https://scrollscan.com'], | ||
chainName: 'Scroll', | ||
iconUrls: ['https://icons.llamao.fi/icons/chains/rsz_scroll.jpg'], | ||
nativeCurrency: { | ||
name: 'ETH', | ||
symbol: 'ETH', | ||
decimals: 18, | ||
}, | ||
rpcUrls: ['https://rpc.scroll.io'], | ||
}, | ||
}; | ||
|
||
function AddChainButton({ chainDetails }: { chainDetails: AddEthereumChainRequest }) { | ||
const { web3 } = useContext(Web3Context); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
|
||
const handleClick = () => { | ||
web3.walletRpc | ||
.addEthereumChain(chainDetails) | ||
.then((response) => { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
`Successfully added chain ${chainDetails.chainId} with response`, | ||
response, | ||
); | ||
}) | ||
.catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
|
||
if (e instanceof Error) { | ||
setError(e); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button type="button" onClick={handleClick}> | ||
{`Add new chain: ${chainDetails.chainName} (${chainDetails.chainId})`} | ||
</button> | ||
{error && <div>{error.message}</div>} | ||
</> | ||
); | ||
} | ||
|
||
export function AddEthereumChain() { | ||
return ( | ||
<div> | ||
<h4>Add EVM Chain</h4> | ||
<AddChainButton chainDetails={chains.mantle} /> | ||
<AddChainButton chainDetails={chains.scroll} /> | ||
</div> | ||
); | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/example-react-app/src/wallet-components/SwitchEthereumChain.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useContext, useState } from 'react'; | ||
|
||
import { Web3Context } from '../web3/Web3Context'; | ||
|
||
function SwitchChainButton({ chainId }: { chainId: number }) { | ||
const { web3 } = useContext(Web3Context); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
|
||
const handleClick = () => { | ||
web3.walletRpc | ||
.switchEthereumChain(chainId) | ||
.then((response) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`Successfully switched to chain ${chainId} with response`, response); | ||
}) | ||
.catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
|
||
if (e instanceof Error) { | ||
setError(e); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button type="button" onClick={handleClick}> | ||
Switch to chain {chainId} | ||
</button> | ||
{error && <div>{error.message}</div>} | ||
</> | ||
); | ||
} | ||
|
||
export function SwitchEthereumChain() { | ||
return ( | ||
<div> | ||
<h4>Switch Chain (must be known by the wallet)</h4> | ||
<SwitchChainButton chainId={137} /> | ||
<SwitchChainButton chainId={1} /> | ||
<SwitchChainButton chainId={5000} /> | ||
</div> | ||
); | ||
} |