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

added sending and estimating states to useEtherspotTransactions hook #86

Merged
merged 4 commits into from
Jan 30, 2024
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [0.7.0] - 2024-01-30

### Added Changes
- Added `isSending`, `isEstimating`, `containsSendingError`, `containsEstimatingError` to `useEtherspotTransactions` hook

## [0.6.12] - 2024-01-26

### Added Changes
Expand Down
226 changes: 207 additions & 19 deletions __tests__/hooks/useEtherspotTransactions.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, render } from '@testing-library/react';
import { renderHook, render, act, waitFor } from '@testing-library/react';
import { ethers } from 'ethers';

// hooks
Expand Down Expand Up @@ -203,11 +203,33 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(ethers.BigNumber.isBigNumber(estimated[0].estimatedBatches[0].cost)).toBe(true);
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000');

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;
expect(result.current.containsSendingError).toBe(false);
expect(sent[0].sentBatches[0].userOpHash).toBe('0x7c');
});

Expand Down Expand Up @@ -246,7 +268,18 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;
expect(result.current.containsSendingError).toBe(false);
expect(sent[0].estimatedBatches[0].cost.toString()).toBe('350000');
expect(sent[0].sentBatches[0].userOpHash).toBe('0x7c');
});
Expand Down Expand Up @@ -308,13 +341,35 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000');
expect(estimated[0].estimatedBatches[1].cost.toString()).toBe('200000');
expect(estimated[1].estimatedBatches.length).toBe(0); // has skip prop
expect(estimated[2].estimatedBatches[0].cost.toString()).toBe('250000');

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;
expect(result.current.containsSendingError).toBe(false);
expect(sent[0].sentBatches[0].userOpHash).toBe('0x7c');
expect(sent[0].sentBatches[1].userOpHash).toBe('0x7d');
expect(sent[1].sentBatches.length).toBe(0); // has skip prop
Expand Down Expand Up @@ -365,11 +420,33 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('325000');

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;
expect(result.current.containsSendingError).toBe(false);
expect(sent[0].sentBatches[0].userOpHash).toBe('0x7c');
expect(sent[1].sentBatches[0].userOpHash).toBe('0x46');
});
Expand Down Expand Up @@ -434,13 +511,35 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000');
expect(estimated[0].estimatedBatches[1].cost.toString()).toBe('200000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('325000');
expect(estimated[2].estimatedBatches[0].cost.toString()).toBe('325000');

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;
expect(result.current.containsSendingError).toBe(false);
expect(sent[0].sentBatches[0].userOpHash).toBe('0x7c');
expect(sent[0].sentBatches[1].userOpHash).toBe('0x7e');
expect(sent[1].sentBatches[0].userOpHash).toBe('0x46');
Expand Down Expand Up @@ -501,8 +600,18 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(onEstimated1).toBeCalledTimes(1);
expect(onEstimated2).toBeCalledTimes(1);
expect(onEstimated1.mock.calls[0][0]).toStrictEqual(estimated[0].estimatedBatches);
Expand Down Expand Up @@ -563,7 +672,17 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;

expect(onSent1).toBeCalledTimes(1);
expect(onSent2).toBeCalledTimes(1);
Expand Down Expand Up @@ -618,8 +737,18 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(true);
expect(estimated[0].estimatedBatches[0].errorMessage).toBe('Transaction reverted: chain too high');
expect(estimated[1].estimatedBatches[0].errorMessage).toBe('Transaction reverted: invalid address');
expect(onEstimated1.mock.calls[0][0]).toStrictEqual(estimated[0].estimatedBatches);
Expand Down Expand Up @@ -673,13 +802,34 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated = await result.current.estimate();
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate();
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('250000');

const sent = await result.current.send();
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send();
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent = await sendPromise;

expect(result.current.containsSendingError).toBe(true);
expect(sent[0].sentBatches[0].errorMessage).toBe('Transaction reverted: chain too hot');
expect(sent[1].sentBatches[0].errorMessage).toBe('Transaction reverted: invalid signature');
expect(onSent1.mock.calls[0][0]).toStrictEqual(sent[0].sentBatches);
Expand Down Expand Up @@ -756,12 +906,31 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const estimated1 = await result.current.estimate(['test-id-1']);
expect(result.current.isEstimating).toBe(false);

let estimatePromise;
act(() => {
estimatePromise = result.current.estimate(['test-id-1']);
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated1 = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(true);
expect(estimated1.length).toBe(1);
expect(estimated1[0].estimatedBatches[0].errorMessage).toBe('Transaction reverted: chain too high');
expect(onEstimated1.mock.calls[0][0]).toStrictEqual(estimated1[0].estimatedBatches);

const estimated2 = await result.current.estimate(['test-id-2', 'test-id-3']);
act(() => {
estimatePromise = result.current.estimate(['test-id-2', 'test-id-3']);
});

await waitFor(() => expect(result.current.isEstimating).toBe(true));
await waitFor(() => expect(result.current.isEstimating).toBe(false));

const estimated2 = await estimatePromise;
expect(result.current.containsEstimatingError).toBe(false);
expect(estimated2.length).toBe(2);
expect(estimated2[0].estimatedBatches[0].cost.toString()).toBe('325000');
expect(estimated2[1].estimatedBatches[0].cost.toString()).toBe('200000');
Expand Down Expand Up @@ -839,13 +1008,32 @@ describe('useEtherspotTransactions()', () => {

const { result } = renderHook(() => useEtherspotTransactions(), { wrapper });

const sent1 = await result.current.send(['test-id-1']);
expect(result.current.isSending).toBe(false);

let sendPromise;
act(() => {
sendPromise = result.current.send(['test-id-1']);
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent1 = await sendPromise;
expect(sent1.length).toBe(1);
expect(result.current.containsSendingError).toBe(true);
expect(sent1[0].sentBatches[0].errorMessage).toBe('Transaction reverted: chain too hot');
expect(onSent1.mock.calls[0][0]).toStrictEqual(sent1[0].sentBatches);

const sent2 = await result.current.send(['test-id-2', 'test-id-3']);
act(() => {
sendPromise = result.current.send(['test-id-2', 'test-id-3']);
});

await waitFor(() => expect(result.current.isSending).toBe(true));
await waitFor(() => expect(result.current.isSending).toBe(false));

const sent2 = await sendPromise;
expect(sent2.length).toBe(2);
expect(result.current.containsSendingError).toBe(false);
expect(sent2[0].sentBatches[0].userOpHash).toBe('0x46');
expect(sent2[1].sentBatches[0].userOpHash).toBe('0x47');
expect(onSent2.mock.calls[0][0]).toStrictEqual(sent2[0].sentBatches);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@etherspot/transaction-kit",
"description": "React Etherspot Transaction Kit",
"version": "0.6.12",
"version": "0.7.0",
IAmKio marked this conversation as resolved.
Show resolved Hide resolved
"main": "dist/cjs/index.js",
"scripts": {
"rollup:build": "NODE_OPTIONS=--max-old-space-size=8192 rollup -c",
"rollup:watch": "rimraf ./node_modules/react ./node_modules/react-dom && rollup -c -w",
"test": "jest __tests__ --silent",
"test": "jest __tests__ --silent --detectOpenHandles",
"test:watch": "jest --watch"
},
"repository": {
Expand Down
4 changes: 4 additions & 0 deletions src/contexts/EtherspotTransactionKitContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export interface IEtherspotTransactionKitContext {
batches: IBatches[];
estimate: (batchesIds?: string[]) => Promise<IEstimatedBatches[]>;
send: (batchesIds?: string[]) => Promise<ISentBatches[]>;
isEstimating: boolean;
isSending: boolean;
containsSendingError: boolean;
containsEstimatingError: boolean;
},
setGroupedBatchesPerId: Dispatch<SetStateAction<TypePerId<IBatches>>>;
}
Expand Down
Loading
Loading