Skip to content

Commit

Permalink
dope
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 12, 2024
1 parent 59567ff commit 3db4c23
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function TransactionProvider({
const [isToastVisible, setIsToastVisible] = useState(false);
const [lifeCycleState, setLifeCycleState] = useState<LifeCycleState>({
stateName: 'init',
stateData: {},
stateData: null,
}); // Component lifecycle
const [receiptArray, setReceiptArray] = useState<TransactionReceipt[]>([]);
const [transactionId, setTransactionId] = useState('');
Expand Down Expand Up @@ -104,7 +104,7 @@ export function TransactionProvider({
}
// Emit State
onState?.(lifeCycleState.stateName, lifeCycleState.stateData);
}, [onState, lifeCycleState.stateData, lifeCycleState.stateName]);
}, [onError, onState, lifeCycleState.stateData, lifeCycleState.stateName]);

const getTransactionReceipts = useCallback(async () => {
const receipts = [];
Expand Down
9 changes: 5 additions & 4 deletions src/transaction/hooks/useCallsStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { UseCallsStatusParams } from '../types';
const uncaughtErrorCode = 'UNCAUGHT_CALL_STATUS_ERROR';

export function useCallsStatus({
onError,
setLifeCycleState,
transactionId,
}: UseCallsStatusParams) {
try {
Expand All @@ -17,12 +17,13 @@ export function useCallsStatus({
enabled: !!transactionId,
},
});

const transactionHash = data?.receipts?.[0]?.transactionHash;

return { status: data?.status, transactionHash };
} catch (err) {
onError?.({ code: uncaughtErrorCode, error: JSON.stringify(err) });
setLifeCycleState({

Check failure on line 23 in src/transaction/hooks/useCallsStatus.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/transaction/hooks/useCallsStatus.test.ts > useCallsStatus > should handle errors and call onError callback

TypeError: setLifeCycleState is not a function ❯ Module.useCallsStatus src/transaction/hooks/useCallsStatus.ts:23:5 ❯ src/transaction/hooks/useCallsStatus.test.ts:38:7 ❯ TestComponent node_modules/@testing-library/react/dist/pure.js:309:27 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7
stateName: 'error',
stateData: { code: uncaughtErrorCode, error: JSON.stringify(err) },
});
return { status: 'error', transactionHash: undefined };
}
}
16 changes: 11 additions & 5 deletions src/transaction/hooks/useWriteContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import type { UseWriteContractParams } from '../types';
* Does not support transaction batching or paymasters.
*/
export function useWriteContract({
onError,
setErrorMessage,
setLifeCycleState,
setTransactionHashArray,
transactionHashArray,
}: UseWriteContractParams) {
Expand All @@ -30,7 +30,10 @@ export function useWriteContract({
} else {
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
onError?.({ code: WRITE_CONTRACT_ERROR_CODE, error: e.message });
setLifeCycleState({

Check failure on line 33 in src/transaction/hooks/useWriteContract.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/transaction/hooks/useWriteContract.test.ts > useWriteContract > should handle generic error

TypeError: setLifeCycleState is not a function ❯ onError src/transaction/hooks/useWriteContract.ts:33:11 ❯ src/transaction/hooks/useWriteContract.test.ts:79:5
stateName: 'error',
stateData: { code: WRITE_CONTRACT_ERROR_CODE, error: e.message },
});
},
onSuccess: (hash: Address) => {
setTransactionHashArray(
Expand All @@ -41,9 +44,12 @@ export function useWriteContract({
});
return { status, writeContractAsync, data };
} catch (err) {
onError?.({
code: UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
error: JSON.stringify(err),
setLifeCycleState({

Check failure on line 47 in src/transaction/hooks/useWriteContract.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/transaction/hooks/useWriteContract.test.ts > useWriteContract > should handle uncaught errors

TypeError: setLifeCycleState is not a function ❯ Module.useWriteContract src/transaction/hooks/useWriteContract.ts:47:5 ❯ src/transaction/hooks/useWriteContract.test.ts:130:7 ❯ TestComponent node_modules/@testing-library/react/dist/pure.js:309:27 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7
stateName: 'error',
stateData: {
code: UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
error: JSON.stringify(err),
},
});
setErrorMessage(GENERIC_ERROR_MESSAGE);
return { status: 'error', writeContractAsync: () => {} };
Expand Down
14 changes: 7 additions & 7 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function useWriteContracts({
setErrorMessage(GENERIC_ERROR_MESSAGE);
}
setLifeCycleState({

Check failure on line 42 in src/transaction/hooks/useWriteContracts.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/transaction/hooks/useWriteContracts.test.ts > useWriteContracts > should handle generic error

TypeError: setLifeCycleState is not a function ❯ onError src/transaction/hooks/useWriteContracts.ts:42:11 ❯ src/transaction/hooks/useWriteContracts.test.ts:43:5
stateName: 'error',
stateDaat: { code: WRITE_CONTRACTS_ERROR_CODE, error: e.message }
stateName: 'error',
stateData: { code: WRITE_CONTRACTS_ERROR_CODE, error: e.message },
});
},
onSuccess: (id) => {
Expand All @@ -52,12 +52,12 @@ export function useWriteContracts({
return { status, writeContractsAsync };
} catch (err) {
setLifeCycleState({

Check failure on line 54 in src/transaction/hooks/useWriteContracts.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

src/transaction/hooks/useWriteContracts.test.ts > useWriteContracts > should handle uncaught errors

TypeError: setLifeCycleState is not a function ❯ Module.useWriteContracts src/transaction/hooks/useWriteContracts.ts:54:5 ❯ src/transaction/hooks/useWriteContracts.test.ts:93:7 ❯ TestComponent node_modules/@testing-library/react/dist/pure.js:309:27 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7
stateName: 'error',
stateName: 'error',
stateData: {
code: UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
error: JSON.stringify(err),
}
});
code: UNCAUGHT_WRITE_CONTRACTS_ERROR_CODE,
error: JSON.stringify(err),
},
});
setErrorMessage(GENERIC_ERROR_MESSAGE);
return {
status: 'error',
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { TransactionToastIcon } from './components/TransactionToastIcon';
export { TransactionToastAction } from './components/TransactionToastAction';
export { TransactionToastLabel } from './components/TransactionToastLabel';
export type {
LifeCycleStateName,
LifeCycleState,
TransactionButtonReact,
TransactionError,
TransactionReact,
Expand Down
12 changes: 6 additions & 6 deletions src/transaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
export type LifeCycleState =
| {
stateName: 'init';
stateData: {};
stateData: null;
}
| {
stateName: 'error';
Expand Down Expand Up @@ -79,7 +79,7 @@ export type TransactionProviderReact = {
children: ReactNode; // The child components to be rendered within the provider component.
contracts: ContractFunctionParameters[]; // An array of contract function parameters provided to the child components.
onError?: (e: TransactionError) => void; // An optional callback function that handles errors within the provider.
onState?: (stateName: string, stateData: Object) => void; // An optional callback function that exposes the component lifecycle state
onState?: (lifeCycleState: LifeCycleState) => void; // An optional callback function that exposes the component lifecycle state
onSuccess?: (response: TransactionResponse) => void; // An optional callback function that exposes the transaction receipts
};

Expand All @@ -94,7 +94,7 @@ export type TransactionReact = {
className?: string; // An optional CSS class name for styling the component.
contracts: ContractFunctionParameters[]; // An array of contract function parameters for the transaction.
onError?: (e: TransactionError) => void; // An optional callback function that handles transaction errors.
onState?: (stateName: string, stateData: Object) => void; // An optional callback function that exposes the component lifecycle state
onState?: (lifeCycleState: LifeCycleState) => void; // An optional callback function that exposes the component lifecycle state
onSuccess?: (response: TransactionResponse) => void; // An optional callback function that exposes the transaction receipts
};

Expand Down Expand Up @@ -166,20 +166,20 @@ export type TransactionToastLabelReact = {
};

export type UseCallsStatusParams = {
onError?: (e: TransactionError) => void;
setLifeCycleState: (state: LifeCycleState) => void;
transactionId: string;
};

export type UseWriteContractParams = {
onError?: (e: TransactionError) => void;
setErrorMessage: (error: string) => void;
setLifeCycleState: (state: LifeCycleState) => void;
setTransactionHashArray: (ids: Address[]) => void;
transactionHashArray?: Address[];
};

export type UseWriteContractsParams = {
onError?: (e: TransactionError) => void;
setErrorMessage: (error: string) => void;
setLifeCycleState: (state: LifeCycleState) => void;
setTransactionId: (id: string) => void;
};

Expand Down

0 comments on commit 3db4c23

Please sign in to comment.