Skip to content

Commit

Permalink
chore: const / import refactor for Transactions (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcramer authored Aug 2, 2024
1 parent b7a52fc commit a348e27
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-zebras-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase/onchainkit": patch
---

-**chore**: Organize const variables and update imports for the Transaction component. By @cpcramer #961
23 changes: 13 additions & 10 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import {
useState,
} from 'react';
import type { TransactionExecutionError } from 'viem';
import { useAccount, useSwitchChain } from 'wagmi';
import { useWaitForTransactionReceipt } from 'wagmi';
import {
useAccount,
useSwitchChain,
useWaitForTransactionReceipt,
} from 'wagmi';
import { useValue } from '../../internal/hooks/useValue';
import { METHOD_NOT_SUPPORTED_ERROR_SUBSTRING } from '../constants';
import {
GENERIC_ERROR_MESSAGE,
METHOD_NOT_SUPPORTED_ERROR_SUBSTRING,
} from '../constants';
import { useCallsStatus } from '../hooks/useCallsStatus';
import { useWriteContract } from '../hooks/useWriteContract';
import {
genericErrorMessage,
useWriteContracts,
} from '../hooks/useWriteContracts';
import { useWriteContracts } from '../hooks/useWriteContracts';
import type {
TransactionContextType,
TransactionProviderReact,
Expand Down Expand Up @@ -87,7 +90,7 @@ export function TransactionProvider({
) {
setErrorMessage('Request denied.');
} else {
setErrorMessage(genericErrorMessage);
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
}
}
Expand Down Expand Up @@ -120,7 +123,7 @@ export function TransactionProvider({
try {
await fallbackToWriteContract();
} catch (_err) {
setErrorMessage(genericErrorMessage);
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
// handles user rejected request error
} else if (
Expand All @@ -130,7 +133,7 @@ export function TransactionProvider({
setErrorMessage('Request denied.');
// handles generic error
} else {
setErrorMessage(genericErrorMessage);
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
},
[fallbackToWriteContract],
Expand Down
11 changes: 9 additions & 2 deletions src/transaction/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export const SEND_CALLS_NOT_SUPPORTED_ERROR = 'SEND_CALLS_NOT_SUPPORTED_ERROR';
export const WRITE_CONTRACT_ERROR_CODE = 'WRITE_CONTRACT_ERROR';
export const WRITE_CONTRACTS_ERROR_CODE = 'WRITE_CONTRACTS_ERROR';
export const GENERIC_ERROR_MESSAGE = 'Something went wrong. Please try again.';
export const METHOD_NOT_SUPPORTED_ERROR_SUBSTRING =
'this request method is not supported'; // most likely EOA
'this request method is not supported'; // most likely EOAexport const genericErrorMessage = 'Something went wrong. Please try again.';
export const SEND_CALLS_NOT_SUPPORTED_ERROR = 'SEND_CALLS_NOT_SUPPORTED_ERROR';
export const UNCAUGHT_WRITE_CONTRACT_ERROR_CODE =
'UNCAUGHT_WRITE_CONTRACT_ERROR';
export const UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE =
'UNCAUGHT_WRITE_WRITE_CONTRACTS_ERROR';
4 changes: 2 additions & 2 deletions src/transaction/hooks/useWriteContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('useWriteContract', () => {
'Something went wrong. Please try again.',
);
expect(mockOnError).toHaveBeenCalledWith({
code: 'WRITE_TRANSACTION_ERROR',
code: 'WRITE_CONTRACT_ERROR',
error: 'Something went wrong. Please try again.',
});
});
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('useWriteContract', () => {
'Something went wrong. Please try again.',
);
expect(mockOnError).toHaveBeenCalledWith({
code: 'UNCAUGHT_WRITE_TRANSACTION_ERROR',
code: 'UNCAUGHT_WRITE_CONTRACT_ERROR',
error: JSON.stringify(uncaughtError),
});
});
Expand Down
20 changes: 12 additions & 8 deletions src/transaction/hooks/useWriteContract.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { TransactionExecutionError } from 'viem';
import { useWriteContract as useWriteContractWagmi } from 'wagmi';
import {
GENERIC_ERROR_MESSAGE,
UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
WRITE_CONTRACT_ERROR_CODE,
} from '../constants';
import type { TransactionError } from '../types';
import { genericErrorMessage } from './useWriteContracts';

type UseWriteContractParams = {
onError?: (e: TransactionError) => void;
setErrorMessage: (error: string) => void;
setTransactionId: (id: string) => void;
};

const uncaughtErrorCode = 'UNCAUGHT_WRITE_TRANSACTION_ERROR';
const errorCode = 'WRITE_TRANSACTION_ERROR';

/**
* Wagmi hook for single contract transactions.
* Supports both EOAs and Smart Wallets.
Expand All @@ -32,9 +33,9 @@ export function useWriteContract({
) {
setErrorMessage('Request denied.');
} else {
setErrorMessage(genericErrorMessage);
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
onError?.({ code: errorCode, error: e.message });
onError?.({ code: WRITE_CONTRACT_ERROR_CODE, error: e.message });
},
onSuccess: (id) => {
setTransactionId(id);
Expand All @@ -43,8 +44,11 @@ export function useWriteContract({
});
return { status, writeContractAsync, data };
} catch (err) {
onError?.({ code: uncaughtErrorCode, error: JSON.stringify(err) });
setErrorMessage(genericErrorMessage);
onError?.({
code: UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
error: JSON.stringify(err),
});
setErrorMessage(GENERIC_ERROR_MESSAGE);
return { status: 'error', writeContractAsync: () => {} };
}
}
4 changes: 2 additions & 2 deletions src/transaction/hooks/useWriteContracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('useWriteContracts', () => {
'Something went wrong. Please try again.',
);
expect(mockOnError).toHaveBeenCalledWith({
code: 'WRITE_TRANSACTIONS_ERROR',
code: 'WRITE_CONTRACTS_ERROR',
error: 'Something went wrong. Please try again.',
});
});
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('useWriteContracts', () => {
'Something went wrong. Please try again.',
);
expect(mockOnError).toHaveBeenCalledWith({
code: 'UNCAUGHT_WRITE_TRANSACTIONS_ERROR',
code: 'UNCAUGHT_WRITE_WRITE_CONTRACTS_ERROR',
error: JSON.stringify(uncaughtError),
});
});
Expand Down
22 changes: 13 additions & 9 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { TransactionExecutionError } from 'viem';
import { useWriteContracts as useWriteContractsWagmi } from 'wagmi/experimental';
import { METHOD_NOT_SUPPORTED_ERROR_SUBSTRING } from '../constants';
import {
GENERIC_ERROR_MESSAGE,
METHOD_NOT_SUPPORTED_ERROR_SUBSTRING,
UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
WRITE_CONTRACTS_ERROR_CODE,
} from '../constants';
import type { TransactionError } from '../types';

type UseWriteContractsParams = {
Expand All @@ -9,10 +14,6 @@ type UseWriteContractsParams = {
setTransactionId: (id: string) => void;
};

export const genericErrorMessage = 'Something went wrong. Please try again.';
const uncaughtErrorCode = 'UNCAUGHT_WRITE_TRANSACTIONS_ERROR';
const errorCode = 'WRITE_TRANSACTIONS_ERROR';

/**
* useWriteContracts: Experimental Wagmi hook for batching transactions.
* Supports Smart Wallets.
Expand Down Expand Up @@ -42,9 +43,9 @@ export function useWriteContracts({
) {
setErrorMessage('Request denied.');
} else {
setErrorMessage(genericErrorMessage);
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
onError?.({ code: errorCode, error: e.message });
onError?.({ code: WRITE_CONTRACTS_ERROR_CODE, error: e.message });
},
onSuccess: (id) => {
setTransactionId(id);
Expand All @@ -53,8 +54,11 @@ export function useWriteContracts({
});
return { status, writeContractsAsync };
} catch (err) {
onError?.({ code: uncaughtErrorCode, error: JSON.stringify(err) });
setErrorMessage(genericErrorMessage);
onError?.({
code: UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
error: JSON.stringify(err),
});
setErrorMessage(GENERIC_ERROR_MESSAGE);
return {
status: 'error',
writeContracts: () => {},
Expand Down

0 comments on commit a348e27

Please sign in to comment.