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
18 changed files
with
391 additions
and
102 deletions.
There are no files selected for viewing
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 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 { useContext, useState } from 'react'; | ||
import { AddEthereumChainRequest } from 'web3-plugin-wallet-rpc'; | ||
import { Web3Context } from '../web3/Web3Context'; | ||
|
||
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> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/example-react-app/src/wallet-components/GetPermissions.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,38 @@ | ||
import { useContext, useState } from 'react'; | ||
import { Permission } from 'web3-plugin-wallet-rpc'; | ||
import { Web3Context } from '../web3/Web3Context'; | ||
|
||
export function GetPermissions() { | ||
const { web3 } = useContext(Web3Context); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
const [permissions, setPermissions] = useState<Permission[] | undefined>(undefined); | ||
|
||
const handleClick = () => { | ||
web3.walletRpc | ||
.getPermissions() | ||
.then((response) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`Successfully got permissions with response`, response); | ||
|
||
setPermissions(response); | ||
}) | ||
.catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
|
||
if (e instanceof Error) { | ||
setError(e); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button type="button" onClick={handleClick}> | ||
Get permissions | ||
</button> | ||
{permissions && <pre>{JSON.stringify(permissions, null, 2)}</pre>} | ||
{error && <div>{error.message}</div>} | ||
</> | ||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/example-react-app/src/wallet-components/Permissions.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,14 @@ | ||
import { GetPermissions } from './GetPermissions'; | ||
import { RequestPermissions } from './RequestPermissions'; | ||
import { RevokePermissions } from './RevokePermissions'; | ||
|
||
export function Permissions() { | ||
return ( | ||
<div> | ||
<h4>Request, get and revoke permissions</h4> | ||
<RequestPermissions /> | ||
<GetPermissions /> | ||
<RevokePermissions /> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.