-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from WatchItDev/app/refactor/quick-trasnsfer-…
…amount refactor: metamask handling
- Loading branch information
Showing
17 changed files
with
248 additions
and
218 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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
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,32 +1,108 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Address } from 'viem'; | ||
import { connectToMetaMask } from '@src/utils/metamask'; | ||
import { notifyError } from '@notifications/internal-notifications.ts'; | ||
import { ERRORS } from '@notifications/errors.ts'; | ||
|
||
export const useMetaMask = () => { | ||
const [address, setAddress] = useState<Address | undefined>(); | ||
const [connecting, setConnecting] = useState(false); | ||
|
||
useEffect(() => { | ||
const walletConnected = localStorage.getItem('walletConnected'); | ||
if (walletConnected === 'true') { | ||
connect(); | ||
} | ||
}, []); | ||
|
||
const connect = async () => { | ||
setConnecting(true); | ||
try { | ||
const walletAddress = await connectToMetaMask(); | ||
setAddress(walletAddress); | ||
localStorage.setItem('walletConnected', 'true'); | ||
} catch (err) { | ||
notifyError(ERRORS.METAMASK_CONNECTING_ERROR); | ||
} finally { | ||
setConnecting(false); | ||
// REACT IMPORTS | ||
import { useCallback } from 'react'; | ||
|
||
// VIEM IMPORTS | ||
import { createWalletClient, custom, WalletClient } from 'viem'; | ||
import type { Address } from 'viem'; | ||
import { polygonAmoy } from 'viem/chains'; | ||
|
||
// METAMASK IMPORTS | ||
import { useSDK } from '@metamask/sdk-react'; | ||
// import { enqueueSnackbar } from 'notistack'; | ||
|
||
/** | ||
* Represents the shape of the object returned by the useMetaMask hook. | ||
*/ | ||
interface UseMetaMaskReturn { | ||
/** | ||
* Initiates the MetaMask connection flow via the MetaMask SDK. | ||
* Returns a list of connected accounts if successful. | ||
* | ||
* Throws an error if the MetaMask SDK isn't yet initialized. | ||
*/ | ||
connect: () => Promise<string[] | undefined>; | ||
|
||
/** | ||
* Indicates whether the user is currently connected to MetaMask. | ||
*/ | ||
connected: boolean; | ||
|
||
/** | ||
* The address of the connected account, if any. | ||
*/ | ||
account?: Address; | ||
|
||
/** | ||
* The chain ID of the currently connected network. | ||
*/ | ||
chainId?: string; | ||
|
||
/** | ||
* A viem WalletClient instance that uses the MetaMask provider as transport. | ||
*/ | ||
walletClient?: WalletClient; | ||
|
||
/** | ||
* Indicates if the connection flow is still in progress. | ||
*/ | ||
loading: boolean; | ||
|
||
/** | ||
* Contains any error that occurred during connection (if applicable). | ||
*/ | ||
error?: Error; | ||
} | ||
|
||
/** | ||
* Custom React hook that leverages the MetaMask React SDK | ||
* along with viem to create a WalletClient for the Polygon Amoy chain. | ||
* | ||
* @returns An object with methods and states for handling the MetaMask connection. | ||
*/ | ||
export function useMetaMask(): UseMetaMaskReturn { | ||
const { | ||
sdk, | ||
connected, | ||
chainId: sdkChainId, | ||
account, | ||
connecting, | ||
error, | ||
provider, | ||
} = useSDK(); | ||
|
||
// useEffect(() => { | ||
// if (error) { | ||
// const errorMessage = typeof error === 'object' ? JSON.stringify(error) : String(error); | ||
// enqueueSnackbar(`METAMASK ERROR: ${errorMessage}`); | ||
// } | ||
// }, [error]); | ||
|
||
/** | ||
* We define 'connect' as a guaranteed function (non-optional). | ||
* If the sdk is not ready, this method will throw an error. | ||
*/ | ||
const connect = useCallback(async (): Promise<string[] | undefined> => { | ||
if (!sdk) { | ||
throw new Error('MetaMask SDK is not initialized.'); | ||
} | ||
}; | ||
return sdk.connect(); | ||
}, [sdk]); | ||
|
||
return { address, connecting, connect, setAddress }; | ||
}; | ||
// Generate a viem wallet client using the MetaMask provider, if available. | ||
const walletClient = provider | ||
? createWalletClient({ | ||
chain: polygonAmoy, | ||
transport: custom(provider), | ||
}) | ||
: undefined; | ||
|
||
return { | ||
connect, | ||
connected, | ||
account: account as Address, | ||
chainId: sdkChainId, | ||
walletClient, | ||
loading: connecting, | ||
error, | ||
}; | ||
} |
Oops, something went wrong.