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 1 commit
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.6.13] - 2024-01-30

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

## [0.6.12] - 2024-01-26

### Added Changes
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@etherspot/transaction-kit",
"description": "React Etherspot Transaction Kit",
"version": "0.6.12",
"version": "0.6.13",
poocart 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",
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
42 changes: 40 additions & 2 deletions src/providers/EtherspotTransactionKitContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ interface EtherspotTransactionKitContextProviderProps {
const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransactionKitContextProviderProps) => {
const { provider, chainId, getSdk } = useEtherspot();
const [groupedBatchesPerId, setGroupedBatchesPerId] = useState<TypePerId<IBatches>>({});
const [isEstimating, setIsEstimating] = useState<boolean>(false);
const [isSending, setIsSending] = useState<boolean>(false);
const [containsSendingError, setContainsSendingError] = useState<boolean>(false);
const [containsEstimatingError, setContainsEstimatingError] = useState<boolean>(false);

const estimate = async (
batchesIds?: string[],
forSending: boolean = false,
): Promise<IEstimatedBatches[]> => {
if (!forSending) {
setIsEstimating(true);
setContainsSendingError(false);
}

const groupedBatchesToEstimate = Object.values<IBatches>(groupedBatchesPerId)
.filter((groupedBatch) => (!batchesIds?.length|| batchesIds.some((batchesId) => batchesId === groupedBatch.id)));

return Promise.all(groupedBatchesToEstimate.map(async (groupedBatch): Promise<IEstimatedBatches> => {
const result = await Promise.all(groupedBatchesToEstimate.map(async (groupedBatch): Promise<IEstimatedBatches> => {
const batches = (groupedBatch.batches ?? []) as IBatch[];

if (groupedBatch.skip) return { ...groupedBatch, estimatedBatches: [] };
Expand Down Expand Up @@ -71,9 +80,20 @@ const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransacti

return { ...groupedBatch, estimatedBatches };
}));

if (!forSending) {
const containsError = result.some((group) => group.estimatedBatches.some((batch) => !!batch.errorMessage));
setContainsEstimatingError(containsError);
setIsEstimating(false);
}

return result;
}

const send = async (batchesIds?: string[]): Promise<ISentBatches[]> => {
setIsSending(true);
setContainsSendingError(false);

const groupedBatchesToClean = Object.values<IBatches>(groupedBatchesPerId)
.filter((groupedBatch) => (!batchesIds?.length|| batchesIds.some((batchesId) => batchesId === groupedBatch.id)));

Expand All @@ -90,7 +110,7 @@ const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransacti

const estimated = await estimate(batchesIds, true);

return Promise.all(estimated.map(async (estimatedBatches): Promise<ISentBatches> => {
const result = await Promise.all(estimated.map(async (estimatedBatches): Promise<ISentBatches> => {
const sentBatches: SentBatch[] = [];

// send in same order as estimated
Expand Down Expand Up @@ -122,16 +142,34 @@ const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransacti

return { ...estimatedBatches, sentBatches };
}));

const containsError = result.some((group) => {
return group.estimatedBatches.some((batch) => !!batch.errorMessage) // estimate error during sending
|| group.sentBatches.some((batch) => !!batch.errorMessage);
});

setContainsSendingError(containsError);
setIsSending(false);

return result;
}

const contextData = useMemo(() => ({
batches: getObjectSortedByKeys(groupedBatchesPerId),
estimate,
send,
chainId,
isEstimating,
isSending,
containsEstimatingError,
containsSendingError,
}), [
chainId,
groupedBatchesPerId,
isEstimating,
isSending,
containsEstimatingError,
containsSendingError,
]);

return (
Expand Down
7 changes: 2 additions & 5 deletions src/types/EtherspotTransactionKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ export interface EstimatedBatch extends IBatch {
userOp?: UserOp;
}

export interface EtherspotPrimeSentBatch extends EstimatedBatch {
export interface SentBatch extends EstimatedBatch {
errorMessage?: string;
userOpHash?: string;
}

export type SentBatch = {
errorMessage?: string;
} & (EtherspotPrimeSentBatch);

export interface IBatches {
id?: string;
batches?: IBatch[];
Expand Down
Loading