From 771eee237d254a940efbd82cff6cd711ba9b862a Mon Sep 17 00:00:00 2001 From: poocart <7067483+poocart@users.noreply.github.com> Date: Mon, 25 Mar 2024 11:08:13 +0200 Subject: [PATCH] optimize sdk init calls, use new sdk config --- CHANGELOG.md | 6 ++ __mocks__/@etherspot/prime-sdk.js | 2 + package-lock.json | 18 ++--- package.json | 4 +- src/providers/EtherspotContextProvider.tsx | 78 +++++++++++++--------- 5 files changed, 65 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c116d6..14d2481 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.10.1] - 2024-03-20 + +### Added Changes +- Fixes and optimizations on SDK initialization +- Updated `@etherspot/prime-sdk` to version `1.6.4` with config changes + ## [0.10.0] - 2024-03-20 ### Breaking Changes diff --git a/__mocks__/@etherspot/prime-sdk.js b/__mocks__/@etherspot/prime-sdk.js index 1024a1a..19f52d4 100644 --- a/__mocks__/@etherspot/prime-sdk.js +++ b/__mocks__/@etherspot/prime-sdk.js @@ -277,4 +277,6 @@ export const isWalletProvider = EtherspotPrime.isWalletProvider; export const Factory = EtherspotPrime.Factory; +export const EtherspotBundler = jest.fn(); + export default EtherspotPrime; diff --git a/package-lock.json b/package-lock.json index 4ffc9f9..5919c07 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "@etherspot/transaction-kit", - "version": "0.10.0", + "version": "0.10.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@etherspot/transaction-kit", - "version": "0.10.0", + "version": "0.10.1", "license": "MIT", "dependencies": { "@etherspot/eip1271-verification-util": "0.1.2", - "@etherspot/prime-sdk": "1.6.2", + "@etherspot/prime-sdk": "1.6.4", "buffer": "^6.0.3", "ethers": "^5.6.9", "lodash": "^4.17.21" @@ -2611,9 +2611,9 @@ } }, "node_modules/@etherspot/prime-sdk": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/@etherspot/prime-sdk/-/prime-sdk-1.6.2.tgz", - "integrity": "sha512-BqLIek3Y2YeECiEH/im/8lNUF1quX/AlzfdbSxw3rkfygJCZq7ikaUBb7QetRfugGVyhWb+ZrRCMmg+WhFmCtw==", + "version": "1.6.4", + "resolved": "https://registry.npmjs.org/@etherspot/prime-sdk/-/prime-sdk-1.6.4.tgz", + "integrity": "sha512-pNGA01XwnlAq6VrRCqz4g1rjqGVIr2O0Nz70lnGtlvWS8IXjC0fHvtYZz+rFIz3G7HExrV8Yfh9EbPxAxMF9SA==", "dependencies": { "@apollo/client": "3.8.7", "@lifi/sdk": "2.5.0", @@ -14121,9 +14121,9 @@ } }, "@etherspot/prime-sdk": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/@etherspot/prime-sdk/-/prime-sdk-1.6.2.tgz", - "integrity": "sha512-BqLIek3Y2YeECiEH/im/8lNUF1quX/AlzfdbSxw3rkfygJCZq7ikaUBb7QetRfugGVyhWb+ZrRCMmg+WhFmCtw==", + "version": "1.6.4", + "resolved": "https://registry.npmjs.org/@etherspot/prime-sdk/-/prime-sdk-1.6.4.tgz", + "integrity": "sha512-pNGA01XwnlAq6VrRCqz4g1rjqGVIr2O0Nz70lnGtlvWS8IXjC0fHvtYZz+rFIz3G7HExrV8Yfh9EbPxAxMF9SA==", "requires": { "@apollo/client": "3.8.7", "@lifi/sdk": "2.5.0", diff --git a/package.json b/package.json index 4f88df8..822c643 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@etherspot/transaction-kit", "description": "React Etherspot Transaction Kit", - "version": "0.10.0", + "version": "0.10.1", "main": "dist/cjs/index.js", "scripts": { "rollup:build": "NODE_OPTIONS=--max-old-space-size=8192 rollup -c", @@ -22,7 +22,7 @@ "homepage": "https://github.com/etherspot/transaction-kit#readme", "dependencies": { "@etherspot/eip1271-verification-util": "0.1.2", - "@etherspot/prime-sdk": "1.6.2", + "@etherspot/prime-sdk": "1.6.4", "buffer": "^6.0.3", "ethers": "^5.6.9", "lodash": "^4.17.21" diff --git a/src/providers/EtherspotContextProvider.tsx b/src/providers/EtherspotContextProvider.tsx index 9263606..28bfed6 100644 --- a/src/providers/EtherspotContextProvider.tsx +++ b/src/providers/EtherspotContextProvider.tsx @@ -5,6 +5,7 @@ import { Factory, Web3WalletProvider, DataUtils, + EtherspotBundler, } from '@etherspot/prime-sdk'; import React, { ReactNode, @@ -13,6 +14,7 @@ import React, { useEffect, useMemo, } from 'react'; +import isEqual from 'lodash/isEqual'; // contexts import EtherspotContext from '../contexts/EtherspotContext'; @@ -20,7 +22,10 @@ import EtherspotContext from '../contexts/EtherspotContext'; // types import { AccountTemplate } from '../types/EtherspotTransactionKit'; -let sdkPerChain: { [chainId: number]: PrimeSdk } = {}; +let sdkPerChain: { [chainId: number]: PrimeSdk | Promise } = {}; +let prevProvider: WalletProviderLike; +let prevAccountTemplate: AccountTemplate | undefined; + let dataService: DataUtils; const EtherspotContextProvider = ({ @@ -34,7 +39,7 @@ const EtherspotContextProvider = ({ children: ReactNode; provider: WalletProviderLike; chainId: number; - accountTemplate: AccountTemplate; + accountTemplate?: AccountTemplate; dataApiKey?: string; bundlerApiKey?: string; }) => { @@ -45,44 +50,53 @@ const EtherspotContextProvider = ({ } useEffect(() => { - // reset on provider change - sdkPerChain = {}; - }, [provider]); + return () => { + // reset on unmount + sdkPerChain = {}; + } + }, []); const getSdk = useCallback(async (sdkChainId: number = chainId, forceNewInstance: boolean = false) => { - if (sdkPerChain[sdkChainId] && !forceNewInstance) return sdkPerChain[sdkChainId]; - - let mappedProvider; - if (!isWalletProvider(provider)) { - try { - // @ts-ignore - mappedProvider = new Web3WalletProvider(provider); - await mappedProvider.refresh(); - } catch (e) { - // no need to log, this is an attempt - } + const accountTemplateOrProviderChanged = (prevProvider && !isEqual(prevProvider, provider)) + || (prevAccountTemplate && prevAccountTemplate !== accountTemplate); - if (!mappedProvider) { - throw new Error('Invalid provider!'); - } + if (sdkPerChain[sdkChainId] && !forceNewInstance && !accountTemplateOrProviderChanged) { + return sdkPerChain[sdkChainId]; } - // @ts-ignore - const sdkForChain = new PrimeSdk(mappedProvider ?? provider, { - chainId: +sdkChainId, - etherspotBundlerApiKey: bundlerApiKey ?? ('__ETHERSPOT_BUNDLER_API_KEY__' || undefined), - factoryWallet: accountTemplate as Factory, - }); + sdkPerChain[sdkChainId] = (async () => { + let mappedProvider; + + if (!isWalletProvider(provider)) { + try { + // @ts-ignore + mappedProvider = new Web3WalletProvider(provider); + await mappedProvider.refresh(); + } catch (e) { + // no need to log, this is an attempt + } + + if (!mappedProvider) { + throw new Error('Invalid provider!'); + } + } - sdkPerChain = { - ...sdkPerChain, - [sdkChainId]: sdkForChain, - } + const etherspotPrimeSdk = new PrimeSdk(mappedProvider ?? provider, { + chainId: +sdkChainId, + bundlerProvider: new EtherspotBundler(+sdkChainId, bundlerApiKey ?? ('__ETHERSPOT_BUNDLER_API_KEY__' || undefined)), + factoryWallet: accountTemplate as Factory, + }); + + // load the address into SDK state + await etherspotPrimeSdk.getCounterFactualAddress(); + + prevProvider = provider; + prevAccountTemplate = accountTemplate; - // establishes connection, requests signature - await sdkForChain.getCounterFactualAddress(); + return etherspotPrimeSdk; + })(); - return sdkForChain; + return sdkPerChain[sdkChainId]; }, [provider, chainId, accountTemplate, bundlerApiKey]); const getDataService = useCallback(() => {