From 91bc4ac74a420ea8a3bdaa8e31058b3360bd3618 Mon Sep 17 00:00:00 2001 From: poocart <7067483+poocart@users.noreply.github.com> Date: Wed, 28 Feb 2024 09:27:12 +0200 Subject: [PATCH] added fixes for estimation cost and error parsing --- CHANGELOG.md | 6 ++++ .../hooks/useEtherspotTransactions.test.js | 30 +++++++++---------- package-lock.json | 4 +-- package.json | 2 +- .../EtherspotApprovalTransaction.tsx | 4 +-- .../EtherspotContractTransaction.tsx | 2 +- .../EtherspotTokenTransferTransaction.tsx | 4 +-- ...EtherspotTransactionKitContextProvider.tsx | 16 ++++++++-- 8 files changed, 42 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2636d83..0cbb82c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/__tests__/hooks/useEtherspotTransactions.test.js b/__tests__/hooks/useEtherspotTransactions.test.js index 67cdc79..dcd4484 100644 --- a/__tests__/hooks/useEtherspotTransactions.test.js +++ b/__tests__/hooks/useEtherspotTransactions.test.js @@ -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); @@ -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'); }); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); }); diff --git a/package-lock.json b/package-lock.json index ebe0c07..08d7807 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@etherspot/transaction-kit", - "version": "0.7.7", + "version": "0.8.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@etherspot/transaction-kit", - "version": "0.7.7", + "version": "0.8.0", "license": "MIT", "dependencies": { "@etherspot/eip1271-verification-util": "0.1.2", diff --git a/package.json b/package.json index 64dc917..6d988ba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/EtherspotApprovalTransaction.tsx b/src/components/EtherspotApprovalTransaction.tsx index 0f98ff8..7a9fd32 100644 --- a/src/components/EtherspotApprovalTransaction.tsx +++ b/src/components/EtherspotApprovalTransaction.tsx @@ -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, @@ -30,7 +30,7 @@ const EtherspotApprovalTransaction = ({ receiverAddress, id: transactionId, tokenDecimals = 18, -}: EtherspotApprovalTransactionProps): React.ReactNode => { +}: EtherspotApprovalTransactionProps): JSX.Element => { const context = useContext(EtherspotBatchContext); const componentId = useId(); diff --git a/src/components/EtherspotContractTransaction.tsx b/src/components/EtherspotContractTransaction.tsx index 78e1623..0512f33 100644 --- a/src/components/EtherspotContractTransaction.tsx +++ b/src/components/EtherspotContractTransaction.tsx @@ -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, diff --git a/src/components/EtherspotTokenTransferTransaction.tsx b/src/components/EtherspotTokenTransferTransaction.tsx index a5026aa..cf601f9 100644 --- a/src/components/EtherspotTokenTransferTransaction.tsx +++ b/src/components/EtherspotTokenTransferTransaction.tsx @@ -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, @@ -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); diff --git a/src/providers/EtherspotTransactionKitContextProvider.tsx b/src/providers/EtherspotTransactionKitContextProvider.tsx index fa42cc3..bc5bc33 100644 --- a/src/providers/EtherspotTransactionKitContextProvider.tsx +++ b/src/providers/EtherspotTransactionKitContextProvider.tsx @@ -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'; @@ -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>({}); @@ -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 }); } } @@ -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 }); } }