Skip to content

Commit

Permalink
Merge pull request #133 from etherspot/fix/sdk-init-improvements
Browse files Browse the repository at this point in the history
optimize sdk init calls, use new sdk config
  • Loading branch information
poocart authored Mar 25, 2024
2 parents d8e5cb9 + 771eee2 commit 72c62d5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 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.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
Expand Down
2 changes: 2 additions & 0 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,6 @@ export const isWalletProvider = EtherspotPrime.isWalletProvider;

export const Factory = EtherspotPrime.Factory;

export const EtherspotBundler = jest.fn();

export default EtherspotPrime;
18 changes: 9 additions & 9 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,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",
Expand All @@ -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"
Expand Down
78 changes: 46 additions & 32 deletions src/providers/EtherspotContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Factory,
Web3WalletProvider,
DataUtils,
EtherspotBundler,
} from '@etherspot/prime-sdk';
import React, {
ReactNode,
Expand All @@ -13,14 +14,18 @@ import React, {
useEffect,
useMemo,
} from 'react';
import isEqual from 'lodash/isEqual';

// contexts
import EtherspotContext from '../contexts/EtherspotContext';

// types
import { AccountTemplate } from '../types/EtherspotTransactionKit';

let sdkPerChain: { [chainId: number]: PrimeSdk } = {};
let sdkPerChain: { [chainId: number]: PrimeSdk | Promise<PrimeSdk> } = {};
let prevProvider: WalletProviderLike;
let prevAccountTemplate: AccountTemplate | undefined;

let dataService: DataUtils;

const EtherspotContextProvider = ({
Expand All @@ -34,7 +39,7 @@ const EtherspotContextProvider = ({
children: ReactNode;
provider: WalletProviderLike;
chainId: number;
accountTemplate: AccountTemplate;
accountTemplate?: AccountTemplate;
dataApiKey?: string;
bundlerApiKey?: string;
}) => {
Expand All @@ -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(() => {
Expand Down

0 comments on commit 72c62d5

Please sign in to comment.