Skip to content

Commit

Permalink
code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Jan 16, 2024
1 parent 1ad3124 commit b987752
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/crypto/src/wallets/third-party/hooks/useChainId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { useMemo, useSyncExternalStore } from 'react'

import { EthWalletConnectorBase } from '../classes'

/**
* Subscribe to chainId changes from a given wallet
*
* Note: Its easier for ethWalletConnector to be defined to avoid complex method signatures for subscribe function
**/
export const useChainId = (ethWalletConnector: EthWalletConnectorBase) => {
const { getSnapShot, subscribe } = useMemo(() => {
if (ethWalletConnector.installed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const handleActionRejected = (e: unknown, rejectCallback: (e: JsonRpcError['erro
}
}

/** Initiate a connection to the passed in wallet */
export const useConnectWallet = (ethWalletConnector: EthWalletConnectorBase) => {
const [connectRefused, setConnectRefused] = useState(false)
const [connectError, setConnectError] = useState<Error>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { useMemo, useSyncExternalStore } from 'react'

import { EthWalletConnectorBase } from '../classes'

/**
* Subscribe to account changes from a given wallet
*
* Note: Its easier for ethWalletConnector to be defined to avoid complex method signatures for subscribe function
**/
export const useCurrentAccountExternal = (ethWalletConnector: EthWalletConnectorBase) => {
const { getSnapShot, subscribe } = useMemo(() => {
return {
getSnapShot: () => ethWalletConnector.allowedAccounts,
subscribe: (notifier: () => void) => ethWalletConnector.subscribeToAccountsChanges(notifier),
subscribe: (notifier: () => void) => ethWalletConnector?.subscribeToAccountsChanges(notifier),
}
}, [ethWalletConnector])

Expand All @@ -23,7 +28,7 @@ export const useCurrentAccount = (ethWalletConnector: EthWalletConnectorBase): [
* see - https://docs.metamask.io/wallet/how-to/connect/access-accounts/#handle-accounts
*/
const [currentAddress, additionalAddresses] = useMemo(() => {
return addresses.length > 0 ? [EthAddress.fromString(addresses[0]), addresses.slice(1, addresses.length)] : [undefined, []]
return addresses && addresses.length > 0 ? [EthAddress.fromString(addresses[0]), addresses.slice(1, addresses.length)] : [undefined, []]
}, [addresses])

if (ethWalletConnector.installed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JsonRpcSigner } from 'ethers'

import { EthWalletConnectorBase } from '../classes'

/** Locate the signer on the passed wallet for a given address */
export const useSigner = (ethWalletConnector: EthWalletConnectorBase, localAddress?: EthAddress): JsonRpcSigner | undefined => {
const [signer] = usePromise(async () => {
if (ethWalletConnector.installed) {
Expand Down

0 comments on commit b987752

Please sign in to comment.