Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prompt for wallet connection in TransactionButton #1216

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-starfishes-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@coinbase/onchainkit': patch
---

**feat**: added prompt for wallet connection in `TransactionButton`. by @0xAlec #1216
37 changes: 35 additions & 2 deletions src/transaction/components/TransactionButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useAccount, useChainId } from 'wagmi';
import { useAccount, useChainId, useConnect } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { getChainExplorer } from '../../network/getChainExplorer';
import { TransactionButton } from './TransactionButton';
Expand All @@ -13,6 +13,7 @@ vi.mock('./TransactionProvider', () => ({
vi.mock('wagmi', () => ({
useChainId: vi.fn(),
useAccount: vi.fn(),
useConnect: vi.fn(),
}));

vi.mock('wagmi/experimental', () => ({
Expand All @@ -30,6 +31,10 @@ describe('TransactionButton', () => {
(useShowCallsStatus as vi.Mock).mockReturnValue({
showCallsStatus: vi.fn(),
});
(useConnect as vi.Mock).mockReturnValue({
connectAsync: vi.fn(),
connectors: {},
});
});

it('renders correctly', () => {
Expand Down Expand Up @@ -130,6 +135,34 @@ describe('TransactionButton', () => {
expect(button).not.toBeDisabled();
});

it('should prompt for connection when address is missing', async () => {
const onSubmit = vi.fn();
const mockConnectAsync = vi.fn();
const mockConnector = { id: 'test' };
(useAccount as vi.Mock).mockReturnValue({ address: undefined });
(useConnect as vi.Mock).mockReturnValue({
connectAsync: mockConnectAsync,
connectors: [mockConnector],
});
(useTransactionContext as vi.Mock).mockReturnValue({
contracts: {},
isLoading: false,
lifeCycleStatus: { statusName: 'init', statusData: null },
transactionId: undefined,
transactionHash: undefined,
receipt: undefined,
onSubmit,
});
render(<TransactionButton text="Transact" />);
const button = screen.getByText('Transact');
await act(async () => {
fireEvent.click(button);
});
expect(mockConnectAsync).toHaveBeenCalled();
expect(mockConnectAsync).toHaveBeenCalledWith({ connector: mockConnector });
expect(onSubmit).toHaveBeenCalled();
});

it('should open transaction link when only receipt exists', () => {
const onSubmit = vi.fn();
const chainExplorerUrl = 'https://explorer.com';
Expand Down
13 changes: 10 additions & 3 deletions src/transaction/components/TransactionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useMemo } from 'react';
import { useAccount, useChainId } from 'wagmi';
import { useAccount, useChainId, useConnect } from 'wagmi';
import { useShowCallsStatus } from 'wagmi/experimental';
import { Spinner } from '../../internal/components/Spinner';
import { getChainExplorer } from '../../network/getChainExplorer';
Expand All @@ -26,13 +26,14 @@ export function TransactionButton({
} = useTransactionContext();

const { address } = useAccount();
const { connectAsync, connectors } = useConnect();

const accountChainId = chainId ?? useChainId();
const { showCallsStatus } = useShowCallsStatus();

const isInProgress =
lifeCycleStatus.statusName === 'transactionPending' || isLoading;
const isMissingProps = !contracts || !address;
const isMissingProps = !contracts;
const isWaitingForReceipt = !!transactionId || !!transactionHash;

const isDisabled =
Expand All @@ -59,7 +60,10 @@ export function TransactionButton({
return buttonText;
}, [buttonText, errorMessage, receipt]);

const handleSubmit = useCallback(() => {
const handleSubmit = useCallback(async () => {
if (!address) {
await connectAsync({ connector: connectors[0] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid create a branch of developer experience by adding a new way to solve the same problem we did with #1173 .

We want to make sure both experience re-use Wallet logic, and do not create internal branch on how the same problem can be solved.

}
// SW will have txn id so open in wallet
if (receipt && transactionId) {
showCallsStatus({ id: transactionId });
Expand All @@ -76,7 +80,10 @@ export function TransactionButton({
onSubmit();
}
}, [
address,
accountChainId,
connectAsync,
connectors[0],
onSubmit,
receipt,
showCallsStatus,
Expand Down