Skip to content

Commit

Permalink
added fixes for estimation cost and error parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
poocart committed Feb 28, 2024
1 parent be20595 commit 91bc4ac
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.8.0] - 2024-02-28

### Breaking Changes
- Updated `useEtherspotTransactions` hook's `estimate` and `send` methods to return actual gas cost on `estimated` key and nicer error messages on `errorMessage` key
- Changed return type to `JSX.Element` for `EtherspotApprovalTransaction`, `EtherspotContractTransaction` and `EtherspotTokenTransferTransaction` components

## [0.7.7] - 2024-02-22

### Added Changes
Expand Down
30 changes: 15 additions & 15 deletions __tests__/hooks/useEtherspotTransactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('useEtherspotTransactions()', () => {
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');
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000000000000');

expect(result.current.isSending).toBe(false);

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

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

Expand Down Expand Up @@ -353,10 +353,10 @@ describe('useEtherspotTransactions()', () => {

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[0].estimatedBatches[0].cost.toString()).toBe('350000000000000');
expect(estimated[0].estimatedBatches[1].cost.toString()).toBe('200000000000000');
expect(estimated[1].estimatedBatches.length).toBe(0); // has skip prop
expect(estimated[2].estimatedBatches[0].cost.toString()).toBe('250000');
expect(estimated[2].estimatedBatches[0].cost.toString()).toBe('250000000000000');

expect(result.current.isSending).toBe(false);

Expand Down Expand Up @@ -432,8 +432,8 @@ describe('useEtherspotTransactions()', () => {

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');
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000000000000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('650000000000000');

expect(result.current.isSending).toBe(false);

Expand Down Expand Up @@ -523,10 +523,10 @@ describe('useEtherspotTransactions()', () => {

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');
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000000000000');
expect(estimated[0].estimatedBatches[1].cost.toString()).toBe('200000000000000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('650000000000000');
expect(estimated[2].estimatedBatches[0].cost.toString()).toBe('650000000000000');

expect(result.current.isSending).toBe(false);

Expand Down Expand Up @@ -814,8 +814,8 @@ describe('useEtherspotTransactions()', () => {

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');
expect(estimated[0].estimatedBatches[0].cost.toString()).toBe('350000000000000');
expect(estimated[1].estimatedBatches[0].cost.toString()).toBe('250000000000000');

expect(result.current.isSending).toBe(false);

Expand Down Expand Up @@ -932,8 +932,8 @@ describe('useEtherspotTransactions()', () => {
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');
expect(estimated2[0].estimatedBatches[0].cost.toString()).toBe('650000000000000');
expect(estimated2[1].estimatedBatches[0].cost.toString()).toBe('200000000000000');
expect(onEstimated2.mock.calls[0][0]).toStrictEqual(estimated2[0].estimatedBatches);
expect(onEstimated3.mock.calls[0][0]).toStrictEqual(estimated2[1].estimatedBatches);
});
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.7.7",
"version": "0.8.0",
"main": "dist/cjs/index.js",
"scripts": {
"rollup:build": "NODE_OPTIONS=--max-old-space-size=8192 rollup -c",
Expand Down
4 changes: 2 additions & 2 deletions src/components/EtherspotApprovalTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface EtherspotApprovalTransactionProps extends IEtherspotApprovalTransactio
* @name EtherspotApprovalTransaction
* @description Component allowing to build ERC20 token approval transactions.
* @param {EtherspotApprovalTransactionProps} props
* @returns {React.ReactNode}
* @returns {JSX.Element}
*/
const EtherspotApprovalTransaction = ({
children,
Expand All @@ -30,7 +30,7 @@ const EtherspotApprovalTransaction = ({
receiverAddress,
id: transactionId,
tokenDecimals = 18,
}: EtherspotApprovalTransactionProps): React.ReactNode => {
}: EtherspotApprovalTransactionProps): JSX.Element => {
const context = useContext(EtherspotBatchContext);
const componentId = useId();

Expand Down
2 changes: 1 addition & 1 deletion src/components/EtherspotContractTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface EtherspotContractTransactionProps extends IEtherspotContractTransactio
* @name EtherspotContractTransaction
* @description Component allowing to build ABI based data transactions.
* @param {EtherspotContractTransactionProps} props
* @returns {React.ReactNode}
* @returns {JSX.Element}
*/
const EtherspotContractTransaction = ({
children,
Expand Down
4 changes: 2 additions & 2 deletions src/components/EtherspotTokenTransferTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface EtherspotTokenTransferTransactionProps extends IEtherspotTokenTransfer
* @name EtherspotTokenTransferTransaction
* @description Component allowing to token transfer transactions.
* @param {EtherspotTokenTransferTransactionProps} props
* @returns {React.ReactNode}
* @returns {JSX.Element}
*/
const EtherspotTokenTransferTransaction = ({
children,
Expand All @@ -31,7 +31,7 @@ const EtherspotTokenTransferTransaction = ({
receiverAddress,
id: transactionId,
tokenDecimals = 18,
}: EtherspotTokenTransferTransactionProps): React.ReactNode => {
}: EtherspotTokenTransferTransactionProps): JSX.Element => {
const context = useContext(EtherspotBatchContext);
const componentId = useId();
const senderAddress = useWalletAddress('etherspot-prime', context?.chainId);
Expand Down
16 changes: 13 additions & 3 deletions src/providers/EtherspotTransactionKitContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useMemo, useState } from 'react';
import { BigNumber } from 'ethers';
import { ErrorHandler } from '@etherspot/prime-sdk/dist/sdk/errorHandler/errorHandler.service';

// contexts
import EtherspotTransactionKitContext from '../contexts/EtherspotTransactionKitContext';
Expand All @@ -17,6 +19,12 @@ interface EtherspotTransactionKitContextProviderProps {
children?: React.ReactNode;
}

const parseEtherspotErrorMessage = (e: ErrorHandler | Error | unknown, defaultMessage: string ): string => {
return (e instanceof ErrorHandler && (e.possibleSolution ?? e.message))
|| (e instanceof Error && e.message)
|| defaultMessage;
}

const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransactionKitContextProviderProps) => {
const { provider, chainId, getSdk } = useEtherspot();
const [groupedBatchesPerId, setGroupedBatchesPerId] = useState<TypePerId<IBatches>>({});
Expand Down Expand Up @@ -70,9 +78,10 @@ const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransacti

const userOp = await etherspotPrimeSdk.estimate(groupedBatch.paymaster);
const totalGas = await etherspotPrimeSdk.totalGasEstimated(userOp);
estimatedBatches.push({ ...batch, cost: totalGas, userOp });
estimatedBatches.push({ ...batch, cost: totalGas.mul(userOp.maxFeePerGas as BigNumber), userOp });
} catch (e) {
estimatedBatches.push({ ...batch, errorMessage: (e instanceof Error && e.message) || 'Failed to estimate!' });
const errorMessage = parseEtherspotErrorMessage(e, 'Failed to estimate!');
estimatedBatches.push({ ...batch, errorMessage });
}
}

Expand Down Expand Up @@ -134,7 +143,8 @@ const EtherspotTransactionKitContextProvider = ({ children }: EtherspotTransacti
const userOpHash = await etherspotPrimeSdk.send(estimatedBatch.userOp);
sentBatches.push({ ...estimatedBatch, userOpHash });
} catch (e) {
sentBatches.push({ ...estimatedBatch, errorMessage: (e instanceof Error && e.message) || 'Failed to send!' });
const errorMessage = parseEtherspotErrorMessage(e, 'Failed to send!');
sentBatches.push({ ...estimatedBatch, errorMessage });
}
}

Expand Down

0 comments on commit 91bc4ac

Please sign in to comment.