Skip to content

Commit

Permalink
smart wallet default
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Oct 3, 2024
1 parent 6aeb519 commit f9aa4a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/pay/components/PayProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useAccount, useConnect, useSwitchChain } from 'wagmi';
import { useWaitForTransactionReceipt } from 'wagmi';
import { useCallsStatus } from 'wagmi/experimental';
import { useWriteContracts } from 'wagmi/experimental';
import { useIsWalletACoinbaseSmartWallet } from '../../wallet/hooks/useIsWalletACoinbaseSmartWallet';
import { GENERIC_ERROR_MESSAGE } from '../constants';
import { useCommerceContracts } from '../hooks/useCommerceContracts';
import { PayProvider, usePayContext } from './PayProvider';
Expand All @@ -32,6 +33,10 @@ vi.mock('../hooks/useCommerceContracts', () => ({
useCommerceContracts: vi.fn(),
}));

vi.mock('../../wallet/hooks/useIsWalletACoinbaseSmartWallet', () => ({
useIsWalletACoinbaseSmartWallet: vi.fn(),
}));

const windowOpenMock = vi.fn();

const TestComponent = () => {
Expand Down Expand Up @@ -77,6 +82,7 @@ describe('PayProvider', () => {
(useCommerceContracts as Mock).mockReturnValue(() =>
Promise.resolve({ contracts: [{}], insufficientBalance: false }),
);
(useIsWalletACoinbaseSmartWallet as Mock).mockReturnValue(true);
});

afterEach(() => {
Expand Down Expand Up @@ -207,7 +213,7 @@ describe('PayProvider', () => {
});
});

it('should default to coinbase wallet in connection request', async () => {
it('should default to coinbase wallet in connection request if not connected', async () => {
(useAccount as Mock).mockReturnValue({
address: undefined,
chainId: undefined,
Expand All @@ -230,6 +236,19 @@ describe('PayProvider', () => {
});
});

it('should default to coinbase wallet in connection request if not smart wallet', async () => {
(useIsWalletACoinbaseSmartWallet as Mock).mockReturnValue(false);
render(
<PayProvider>
<TestComponent />
</PayProvider>,
);
fireEvent.click(screen.getByText('Submit'));
await waitFor(() => {
expect(useConnect().connectAsync).toHaveBeenCalled();
});
});

it('should call the provided onStatus', async () => {
const onStatus = vi.fn();
(useWaitForTransactionReceipt as Mock).mockReturnValue({
Expand Down
9 changes: 4 additions & 5 deletions src/pay/components/PayProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useWriteContracts } from 'wagmi/experimental';
import { useCallsStatus } from 'wagmi/experimental';
import { useValue } from '../../internal/hooks/useValue';
import { isUserRejectedRequestError } from '../../transaction/utils/isUserRejectedRequestError';
import { useIsWalletACoinbaseSmartWallet } from '../../wallet/hooks/useIsWalletACoinbaseSmartWallet';
import {
GENERIC_ERROR_MESSAGE,
NO_CONNECTED_ADDRESS_ERROR,
Expand Down Expand Up @@ -53,6 +54,7 @@ export function PayProvider({
const [chargeId, setChargeId] = useState('');
const [transactionId, setTransactionId] = useState('');
const [errorMessage, setErrorMessage] = useState<string>('');
const isSmartWallet = useIsWalletACoinbaseSmartWallet();

// Component lifecycle
const { lifecycleStatus, updateLifecycleStatus } = useLifecycleStatus({
Expand Down Expand Up @@ -166,14 +168,11 @@ export function PayProvider({

let connectedAddress = address;
let connectedChainId = chainId;
if (!isConnected) {
if (!isConnected || !isSmartWallet) {
// Prompt for wallet connection
// This is defaulted to Coinbase Smart Wallet
const { accounts, chainId: _connectedChainId } = await connectAsync({
connector:
connectors.find(
(connector) => connector.id === 'coinbaseWalletSDK',
) || coinbaseWallet({ preference: 'smartWalletOnly' }),
connector: coinbaseWallet({ preference: 'smartWalletOnly' }),
});
connectedAddress = accounts[0];
connectedChainId = _connectedChainId;
Expand Down

0 comments on commit f9aa4a7

Please sign in to comment.