Skip to content

Commit

Permalink
useSendSCWTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Aug 14, 2024
1 parent a761e9d commit 79c05bf
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import { useValue } from '../../internal/hooks/useValue';
import {
GENERIC_ERROR_MESSAGE,
METHOD_NOT_SUPPORTED_ERROR_SUBSTRING,
TRANSACTION_TYPE_CALLS,
TRANSACTION_TYPE_CONTRACTS,
} from '../constants';
import { useCallsStatus } from '../hooks/useCallsStatus';
import { useSendCall } from '../hooks/useSendCall';
import { useSendCalls } from '../hooks/useSendCalls';
import { useSendEOATransactions } from '../hooks/useSendEOATransactions';
import { useSendSCWTransactions } from '../hooks/useSendSCWTransactions';
import { useTransactionStatus } from '../hooks/useTransactionStatus';
import { useTransactionType } from '../hooks/useTransactionType';
import { useWriteContract } from '../hooks/useWriteContract';
Expand Down Expand Up @@ -193,30 +192,17 @@ export function TransactionProvider({
);

/*
Execute batched transactions using the experimental hooks.
Execute batched transactions using Wagmi experimental hooks.
Based off the transaction type (either contract functions or calls)
*/
const executeBatchedTransactions = useCallback(async () => {
if (transactionType === TRANSACTION_TYPE_CONTRACTS && contracts) {
await writeContractsAsync({
contracts,
capabilities,
});
}
if (transactionType === TRANSACTION_TYPE_CALLS && calls) {
await sendCallsAsync({
calls,
capabilities,
});
}
}, [
writeContractsAsync,
sendCallsAsync,
calls,
const sendSCWTransactions = useSendSCWTransactions({
transactionType,
contracts,
calls,
capabilities,
transactionType,
]);
writeContractsAsync,
sendCallsAsync,
});

const handleSubmitErrors = useCallback(
async (err: unknown) => {
Expand Down Expand Up @@ -250,11 +236,11 @@ export function TransactionProvider({
setIsToastVisible(true);
try {
await switchChain(chainId);
await executeBatchedTransactions();
await sendSCWTransactions();
} catch (err) {
await handleSubmitErrors(err);
}
}, [chainId, executeBatchedTransactions, handleSubmitErrors, switchChain]);
}, [chainId, sendSCWTransactions, handleSubmitErrors, switchChain]);

useEffect(() => {
if (receiptArray?.length) {
Expand Down
118 changes: 118 additions & 0 deletions src/transaction/hooks/useSendSCWTransactions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { renderHook } from '@testing-library/react';
import type { Hex } from 'viem';
import { describe, expect, it, vi } from 'vitest';
import {
TRANSACTION_TYPE_CALLS,
TRANSACTION_TYPE_CONTRACTS,
} from '../constants';
import { useSendSCWTransactions } from './useSendSCWTransactions';

describe('useSendSCWTransactions', () => {
it('should call writeContractsAsync when transactionType is TRANSACTION_TYPE_CONTRACTS', async () => {
const writeContractsAsync = vi.fn().mockResolvedValue(undefined);
const sendCallsAsync = vi.fn();
const contracts = [
{ address: '0x789' as Hex, abi: [], functionName: 'test' },
];
const capabilities = {};

const { result } = renderHook(() =>
useSendSCWTransactions({
transactionType: TRANSACTION_TYPE_CONTRACTS,
contracts,
capabilities,
writeContractsAsync,
sendCallsAsync,
}),
);

await result.current();

expect(writeContractsAsync).toHaveBeenCalledWith({
contracts,
capabilities,
});
expect(sendCallsAsync).not.toHaveBeenCalled();
});

it('should call sendCallsAsync when transactionType is TRANSACTION_TYPE_CALLS', async () => {
const writeContractsAsync = vi.fn();
const sendCallsAsync = vi.fn().mockResolvedValue(undefined);
const calls = [{ to: '0x123' as Hex, data: '0x456' as Hex }];
const capabilities = {
/* mock capabilities */
};

const { result } = renderHook(() =>
useSendSCWTransactions({
transactionType: TRANSACTION_TYPE_CALLS,
calls,
capabilities,
writeContractsAsync,
sendCallsAsync,
}),
);

await result.current();

expect(sendCallsAsync).toHaveBeenCalledWith({
calls,
capabilities,
});
expect(writeContractsAsync).not.toHaveBeenCalled();
});

it('should not call either function when transactionType is unknown', async () => {
const writeContractsAsync = vi.fn();
const sendCallsAsync = vi.fn();

const { result } = renderHook(() =>
useSendSCWTransactions({
transactionType: 'UNKNOWN_TYPE',
writeContractsAsync,
sendCallsAsync,
}),
);

await result.current();

expect(writeContractsAsync).not.toHaveBeenCalled();
expect(sendCallsAsync).not.toHaveBeenCalled();
});

it('should not call writeContractsAsync when contracts are not provided', async () => {
const writeContractsAsync = vi.fn();
const sendCallsAsync = vi.fn();

const { result } = renderHook(() =>
useSendSCWTransactions({
transactionType: TRANSACTION_TYPE_CONTRACTS,
writeContractsAsync,
sendCallsAsync,
}),
);

await result.current();

expect(writeContractsAsync).not.toHaveBeenCalled();
expect(sendCallsAsync).not.toHaveBeenCalled();
});

it('should not call sendCallsAsync when calls are not provided', async () => {
const writeContractsAsync = vi.fn();
const sendCallsAsync = vi.fn();

const { result } = renderHook(() =>
useSendSCWTransactions({
transactionType: TRANSACTION_TYPE_CALLS,
writeContractsAsync,
sendCallsAsync,
}),
);

await result.current();

expect(writeContractsAsync).not.toHaveBeenCalled();
expect(sendCallsAsync).not.toHaveBeenCalled();
});
});
47 changes: 47 additions & 0 deletions src/transaction/hooks/useSendSCWTransactions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useCallback } from 'react';
import type { ContractFunctionParameters } from 'viem';
import {
TRANSACTION_TYPE_CALLS,
TRANSACTION_TYPE_CONTRACTS,
} from '../constants';
import type { CallsType, WalletCapabilities } from '../types';

export const useSendSCWTransactions = ({
transactionType,
contracts,
calls,
capabilities,
writeContractsAsync,
sendCallsAsync,
}: {
transactionType: string;
contracts?: ContractFunctionParameters[];
calls?: CallsType[];
capabilities?: WalletCapabilities;
// biome-ignore lint: cannot find module 'wagmi/experimental/query'
writeContractsAsync: any;
// biome-ignore lint: cannot find module 'wagmi/experimental/query'
sendCallsAsync: any;
}) => {
return useCallback(async () => {
if (transactionType === TRANSACTION_TYPE_CONTRACTS && contracts) {
await writeContractsAsync({
contracts,
capabilities,
});
}
if (transactionType === TRANSACTION_TYPE_CALLS && calls) {
await sendCallsAsync({
calls,
capabilities,
});
}
}, [
writeContractsAsync,
sendCallsAsync,
calls,
contracts,
capabilities,
transactionType,
]);
};

0 comments on commit 79c05bf

Please sign in to comment.