-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Upgrade wagmi to v1 * Show warning message when we detect that the storage is full * Add TextEncoder to jest environment * Bump viem to latest, and upgrade Typescript to v5
- Loading branch information
Showing
15 changed files
with
632 additions
and
425 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 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 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,4 @@ | ||
import { TextEncoder, TextDecoder } from 'util'; | ||
|
||
global.TextEncoder = TextEncoder; | ||
global.TextDecoder = TextDecoder; |
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 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 |
---|---|---|
@@ -1,51 +1,35 @@ | ||
/** | ||
* Note: https://github.com/tmm/testing-wagmi/tree/main/test was used as the starting point for this code | ||
*/ | ||
import { createClient, CreateClientConfig } from 'wagmi'; | ||
import { createConfig, CreateConfigParameters, WalletClient } from 'wagmi'; | ||
import { MockConnector } from 'wagmi/dist/connectors/mock'; | ||
import { hardhat } from 'wagmi/chains'; | ||
import { providers, Wallet } from 'ethers'; | ||
import { createPublicClient, createWalletClient, http } from 'viem'; | ||
|
||
type Config = Partial<CreateClientConfig> & { signer?: WalletSigner }; | ||
type Config = Partial<CreateConfigParameters> & { walletClient?: WalletClient }; | ||
|
||
export function createMockClient({ signer = getSigners()[0]!, ...config }: Config = {}) { | ||
return createClient({ | ||
connectors: [new MockConnector({ options: { signer } })], | ||
provider: () => getHardhatProvider(), | ||
export function createMockClient({ walletClient = getMockWalletClient(), ...config }: Config = {}) { | ||
return createConfig({ | ||
connectors: [new MockConnector({ options: { walletClient } })], | ||
publicClient: () => getPublicClient(), | ||
...config, | ||
}); | ||
} | ||
|
||
function getHardhatProvider() { | ||
const rpcUrl = hardhat.rpcUrls.default.http[0]; | ||
return new providers.StaticJsonRpcProvider(rpcUrl, { | ||
chainId: hardhat.id, | ||
name: hardhat.name, | ||
ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', | ||
export const getMockWalletClient = () => | ||
// Default Hardhat account | ||
createWalletClient({ | ||
transport: http(hardhat.rpcUrls.default.http[0]), | ||
chain: hardhat, | ||
account: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', | ||
key: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', | ||
pollingInterval: 100, | ||
}); | ||
} | ||
|
||
function getSigners() { | ||
const provider = getHardhatProvider(); | ||
return accounts.map((acc) => new WalletSigner(acc.privateKey, provider)); | ||
} | ||
|
||
class WalletSigner extends Wallet { | ||
connectUnchecked(): providers.JsonRpcSigner { | ||
return (this.provider as providers.StaticJsonRpcProvider).getUncheckedSigner(this.address); | ||
} | ||
} | ||
|
||
// Default Hardhat accounts | ||
const accounts = [ | ||
{ | ||
// Account #0: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 | ||
privateKey: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', | ||
balance: '10000000000000000000000', | ||
}, | ||
{ | ||
// Account #1: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 | ||
privateKey: '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d', | ||
balance: '10000000000000000000000', | ||
}, | ||
]; | ||
export const getPublicClient = () => { | ||
return createPublicClient({ | ||
transport: http(hardhat.rpcUrls.default.http[0]), | ||
chain: hardhat, | ||
pollingInterval: 100, | ||
}); | ||
}; |
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 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,51 @@ | ||
/** | ||
* Source: https://wagmi.sh/react/ethers-adapters | ||
*/ | ||
import { type PublicClient, usePublicClient, type WalletClient, useWalletClient } from 'wagmi'; | ||
import { providers } from 'ethers'; | ||
import { type HttpTransport } from 'viem'; | ||
import { useMemo } from 'react'; | ||
|
||
export function publicClientToProvider(publicClient: PublicClient) { | ||
const { chain, transport } = publicClient; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
if (transport.type === 'fallback') | ||
return new providers.FallbackProvider( | ||
(transport.transports as ReturnType<HttpTransport>[]).map( | ||
({ value }) => new providers.JsonRpcProvider(value?.url, network) | ||
) | ||
); | ||
return new providers.JsonRpcProvider(transport.url, network); | ||
} | ||
|
||
/** | ||
* Hook to convert a viem Public Client to an ethers.js Provider. | ||
*/ | ||
export function useEthersProvider({ chainId }: { chainId?: number } = {}) { | ||
const publicClient = usePublicClient({ chainId }); | ||
return useMemo(() => publicClientToProvider(publicClient), [publicClient]); | ||
} | ||
|
||
export function walletClientToSigner(walletClient: WalletClient) { | ||
const { account, chain, transport } = walletClient; | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
}; | ||
const provider = new providers.Web3Provider(transport, network); | ||
const signer = provider.getSigner(account.address); | ||
return signer; | ||
} | ||
|
||
/** | ||
* Hook to convert a viem Wallet Client to an ethers.js Signer. | ||
*/ | ||
export function useEthersSigner({ chainId }: { chainId?: number } = {}) { | ||
const { data: walletClient } = useWalletClient({ chainId }); | ||
return useMemo(() => (walletClient ? walletClientToSigner(walletClient) : undefined), [walletClient]); | ||
} |
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 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
36 changes: 36 additions & 0 deletions
36
src/components/notifications/storage-full-notification.module.scss
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,36 @@ | ||
@import '../../styles/variables.module.scss'; | ||
|
||
.buttonRow { | ||
display: flex; | ||
flex-direction: column-reverse; | ||
justify-content: flex-end; | ||
gap: 20px; | ||
margin-top: 20px; | ||
|
||
@media (min-width: $min-sm) { | ||
flex-direction: row; | ||
} | ||
} | ||
|
||
.cancelButton { | ||
cursor: pointer; | ||
border: none; | ||
background: none; | ||
font-weight: 400; | ||
} | ||
|
||
.clearButton { | ||
cursor: pointer; | ||
background-color: #194aff; | ||
color: $primary-color; | ||
min-width: 15ch; | ||
padding: 12px 10px; | ||
font-weight: 500; | ||
border-radius: 4px; | ||
border: none; | ||
white-space: nowrap; | ||
|
||
&:disabled { | ||
opacity: 0.8; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/components/notifications/storage-full-notification.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,34 @@ | ||
import { useState } from 'react'; | ||
import { closeAll } from './notifications'; | ||
import { ERROR_REPORTING_CONSENT_KEY_NAME } from '../../utils'; | ||
import styles from './storage-full-notification.module.scss'; | ||
|
||
export default function StorageFullNotification() { | ||
const [busy, setBusy] = useState(false); | ||
|
||
return ( | ||
<div> | ||
We have detected that your local storage is full. Would you like to clear it and refresh the page? | ||
<div className={styles.buttonRow}> | ||
<button onClick={() => closeAll()} className={styles.cancelButton}> | ||
Cancel | ||
</button> | ||
<button | ||
className={styles.clearButton} | ||
disabled={busy} | ||
onClick={() => { | ||
setBusy(true); | ||
const errorReportingValue = window.localStorage.getItem(ERROR_REPORTING_CONSENT_KEY_NAME); | ||
window.localStorage.clear(); | ||
if (errorReportingValue) { | ||
window.localStorage.setItem(ERROR_REPORTING_CONSENT_KEY_NAME, errorReportingValue); | ||
} | ||
window.location.reload(); | ||
}} | ||
> | ||
{busy ? 'Clearing...' : 'Clear Storage'} | ||
</button> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,7 @@ code { | |
position: relative; | ||
z-index: 100; | ||
} | ||
|
||
.cursor-auto { | ||
cursor: auto; | ||
} |
Oops, something went wrong.