Skip to content

Commit

Permalink
use capabilities instead of connectorId for feature detection (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkas authored Oct 14, 2024
1 parent c2a69df commit c07a1b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
37 changes: 37 additions & 0 deletions apps/web/src/hooks/useCapabilitiesSafe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
A hook to safely check wallet_getCapabilities support
Responsabilities:
- Check for unsupported wallets (e.g: metamask)
- Use experimental useCapabilities
- Check atomicBatch (batchcall) and paymasterService for a given chain
- Default to false
*/

import { Chain } from 'viem';
import { useAccount } from 'wagmi';
import { useCapabilities } from 'wagmi/experimental';

export type UseCapabilitiesSafeProps = {
chain: Chain;
};

export default function useCapabilitiesSafe({ chain }: UseCapabilitiesSafeProps) {
const { connector, isConnected } = useAccount();

// Metamask doesn't support wallet_getCapabilities
const isMetamaskWallet = connector?.id === 'io.metamask';
const enabled = isConnected && !isMetamaskWallet;

const { data: capabilities } = useCapabilities({ query: { enabled } });

const atomicBatchEnabled =
(capabilities && capabilities[chain.id]?.atomicBatch?.supported === true) ?? false;
const paymasterServiceEnabled =
(capabilities && capabilities[chain.id]?.paymasterService?.supported === true) ?? false;

return {
atomicBatchEnabled,
paymasterServiceEnabled,
};
}
15 changes: 6 additions & 9 deletions apps/web/src/hooks/useWriteContractsWithLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Chain } from 'viem';
import { WriteContractsParameters } from 'viem/experimental';
import { useAccount, useSwitchChain, useWaitForTransactionReceipt } from 'wagmi';
import { useCallsStatus, useWriteContracts } from 'wagmi/experimental';
import useCapabilitiesSafe from 'apps/web/src/hooks/useCapabilitiesSafe';

/*
A hook to request and track a wallet write transaction
Expand Down Expand Up @@ -54,11 +55,7 @@ export default function useWriteContractsWithLogs({
// Errors & Analytics
const { logEventWithContext } = useAnalytics();
const { logError } = useErrors();
const { connector } = useAccount();
const isCoinbaseSmartWallet = connector?.id === 'coinbaseWalletSDK';

const batchCallsEnabled = isCoinbaseSmartWallet;

const { atomicBatchEnabled } = useCapabilitiesSafe({ chain });
const { chain: connectedChain } = useAccount();

const [batchCallsStatus, setBatchCallsStatus] = useState<BatchCallsStatus>(BatchCallsStatus.Idle);
Expand All @@ -78,7 +75,7 @@ export default function useWriteContractsWithLogs({
// @ts-expect-error: We can expect sendCallsId to be undefined since we're only enabling the query when defined
id: sendCallsId,
query: {
enabled: !!sendCallsId && batchCallsEnabled,
enabled: !!sendCallsId && atomicBatchEnabled,
refetchInterval: 5000, // todo: smarter
},
});
Expand Down Expand Up @@ -107,7 +104,7 @@ export default function useWriteContractsWithLogs({

const initiateBatchCalls = useCallback(
async (writeContractParameters: WriteContractsParameters) => {
if (!isCoinbaseSmartWallet) return Promise.resolve("Wallet doesn't support sendCalls");
if (!atomicBatchEnabled) return Promise.resolve("Wallet doesn't support sendCalls");

if (!connectedChain) return;
if (connectedChain.id !== chain.id) {
Expand All @@ -126,7 +123,7 @@ export default function useWriteContractsWithLogs({
}
},
[
isCoinbaseSmartWallet,
atomicBatchEnabled,
connectedChain,
chain.id,
switchChainAsync,
Expand Down Expand Up @@ -198,6 +195,6 @@ export default function useWriteContractsWithLogs({
batchCallsIsSuccess,
batchCallsIsError,
batchCallsError,
batchCallsEnabled,
batchCallsEnabled: atomicBatchEnabled,
};
}

0 comments on commit c07a1b4

Please sign in to comment.