Skip to content

Commit

Permalink
replaced addressTemplate implementation with accountTemplate, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
poocart committed Oct 25, 2023
1 parent 74474ba commit f38974e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
15 changes: 11 additions & 4 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import * as EtherspotPrime from '@etherspot/prime-sdk';
import { ethers } from 'ethers';

const defaultAccountAddress = '0x7F30B1960D5556929B03a0339814fE903c55a347';
const otherAccountAddress = '0xAb4C67d8D7B248B2fA6B638C645466065fE8F1F1'
export const defaultAccountAddress = '0x7F30B1960D5556929B03a0339814fE903c55a347';
export const otherFactoryDefaultAccountAddress = '0xe383724e3bDC4753746dEC781809f8CD82010914';
export const otherAccountAddress = '0xAb4C67d8D7B248B2fA6B638C645466065fE8F1F1'

export class PrimeSdk {
sdkChainId;
userOps = [];
nonce = ethers.BigNumber.from(1);
factoryWallet;

constructor (provider, config) {
this.sdkChainId = config.chainId;
this.factoryWallet = config.factoryWallet;
}

getCounterFactualAddress() {
return defaultAccountAddress;
if (this.factoryWallet === Factory.ETHERSPOT) {
return defaultAccountAddress;
}
return otherFactoryDefaultAccountAddress;
}

getAccountBalances({ chainId, account }) {
Expand Down Expand Up @@ -260,7 +266,8 @@ export class PrimeSdk {
return userOpHash;
}
}

export const isWalletProvider = EtherspotPrime.isWalletProvider;

export const Factory = EtherspotPrime.Factory;

export default EtherspotPrime;
36 changes: 31 additions & 5 deletions __tests__/hooks/useWalletAddress.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { renderHook, waitFor } from '@testing-library/react';
import { ethers } from 'ethers';
import { Factory } from '@etherspot/prime-sdk';

// hooks
import { EtherspotTransactionKit, useWalletAddress } from '../../src';
import {
defaultAccountAddress,
otherFactoryDefaultAccountAddress,
} from '../../__mocks__/@etherspot/prime-sdk';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

const etherspotPrimeAddress = '0x7F30B1960D5556929B03a0339814fE903c55a347';
const providerWalletAddress = provider.address;

describe('useWalletAddress()', () => {
Expand All @@ -21,7 +25,7 @@ describe('useWalletAddress()', () => {
const { result } = renderHook(() => useWalletAddress(), { wrapper });

await waitFor(() => expect(result.current).not.toBe(undefined));
expect(result.current).toEqual(etherspotPrimeAddress);
expect(result.current).toEqual(defaultAccountAddress);
});

it('returns wallet address by type', async () => {
Expand All @@ -37,10 +41,10 @@ describe('useWalletAddress()', () => {
});

await waitFor(() => expect(result.current).not.toBe(undefined));
expect(result.current).toEqual(etherspotPrimeAddress);
expect(result.current).toEqual(defaultAccountAddress);

rerender({ providerType: 'provider' });
await waitFor(() => expect(result.current).not.toBe(etherspotPrimeAddress));
await waitFor(() => expect(result.current).not.toBe(defaultAccountAddress));
expect(result.current).toEqual(providerWalletAddress);
});

Expand All @@ -57,9 +61,31 @@ describe('useWalletAddress()', () => {
});

await waitFor(() => expect(result.current).not.toBe(undefined));
expect(result.current).toEqual(etherspotPrimeAddress);
expect(result.current).toEqual(defaultAccountAddress);

rerender({ providerType: 'whatever' });
await waitFor(() => expect(result.current).toBe(undefined));
});

it('returns different wallet address when account template provided', async () => {
const createWrapper = ({ accountTemplate } = {}) => ({ children }) => (
<EtherspotTransactionKit provider={provider} accountTemplate={accountTemplate}>
{children}
</EtherspotTransactionKit>
);

const { result: resultNoAccountTemplate } = renderHook(() => useWalletAddress(), {
wrapper: createWrapper(),
});

await waitFor(() => expect(resultNoAccountTemplate.current).not.toBe(undefined));
expect(resultNoAccountTemplate.current).toEqual(defaultAccountAddress);

const { result: resultWithAccountTemplate } = renderHook(() => useWalletAddress(), {
wrapper: createWrapper({ accountTemplate: Factory.SIMPLE_ACCOUNT }),
});

await waitFor(() => expect(resultWithAccountTemplate.current).not.toBe(undefined));
expect(resultWithAccountTemplate.current).toEqual(otherFactoryDefaultAccountAddress);
});
})
4 changes: 1 addition & 3 deletions src/components/EtherspotBatches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const EtherspotBatches = (props: EtherspotBatchesProps) => {
onSent,
id: batchesId,
paymaster,
addressTemplate
} = props;

const context = useContext(EtherspotTransactionKitContext);
Expand Down Expand Up @@ -57,7 +56,6 @@ const EtherspotBatches = (props: EtherspotBatchesProps) => {
onEstimated,
onSent,
paymaster,
addressTemplate,
};

context.setGroupedBatchesPerId((current) => ({ ...current, [componentId]: groupedBatch }));
Expand All @@ -68,7 +66,7 @@ const EtherspotBatches = (props: EtherspotBatchesProps) => {
return current;
});
}
}, [componentId, batchesPerId, skip, batchesId, paymaster, addressTemplate]);
}, [componentId, batchesPerId, skip, batchesId, paymaster]);

return (
<EtherspotBatchesContext.Provider value={{ setBatchesPerId }}>
Expand Down
20 changes: 15 additions & 5 deletions src/components/EtherspotTransactionKit.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import React from 'react';
import { WalletProviderLike } from '@etherspot/prime-sdk';
import { WalletProviderLike, Factory } from '@etherspot/prime-sdk';
import EtherspotContextProvider from '../providers/EtherspotContextProvider';

// providers
import EtherspotTransactionKitContextProvider from '../providers/EtherspotTransactionKitContextProvider';
import ProviderWalletContextProvider from '../providers/ProviderWalletContextProvider';

interface EtherspotTransactionKitProps extends React.PropsWithChildren {
provider?: WalletProviderLike | null | undefined;
chainId?: number | undefined;
provider: WalletProviderLike;
chainId?: number;
accountTemplate?: Factory;
}

const EtherspotTransactionKit = ({ children, provider, chainId = 1 }: EtherspotTransactionKitProps) => (
<EtherspotContextProvider provider={provider} chainId={chainId}>
const EtherspotTransactionKit = ({
children,
provider,
chainId = 1,
accountTemplate = Factory.ETHERSPOT,
}: EtherspotTransactionKitProps) => (
<EtherspotContextProvider
provider={provider}
chainId={chainId}
accountTemplate={accountTemplate}
>
<EtherspotTransactionKitContextProvider>
<ProviderWalletContextProvider>
{children}
Expand Down
12 changes: 8 additions & 4 deletions src/providers/EtherspotContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
PrimeSdk,
WalletProviderLike,
isWalletProvider
isWalletProvider,
Factory,
} from '@etherspot/prime-sdk';
import React, {
ReactNode,
Expand All @@ -19,11 +20,13 @@ let sdkPerChain: { [chainId: number]: PrimeSdk } = {};
const EtherspotContextProvider = ({
children,
provider,
chainId = 1,
chainId,
accountTemplate,
}: {
children: ReactNode;
provider: WalletProviderLike;
chainId?: number;
chainId: number;
accountTemplate: Factory;
}) => {
const context = useContext(EtherspotContext);

Expand All @@ -46,6 +49,7 @@ const EtherspotContextProvider = ({
const sdkForChain = new PrimeSdk(provider, {
chainId: sdkChainId,
projectKey: '__ETHERSPOT_PROJECT_KEY__' || undefined,
factoryWallet: accountTemplate,
});

sdkPerChain = {
Expand All @@ -57,7 +61,7 @@ const EtherspotContextProvider = ({
await sdkForChain.getCounterFactualAddress();

return sdkForChain;
}, [provider, chainId]);
}, [provider, chainId, accountTemplate]);

const contextData = useMemo(() => ({
getSdk,
Expand Down
3 changes: 1 addition & 2 deletions src/types/EtherspotTransactionKit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fragment, JsonFragment } from '@ethersproject/abi';
import { BigNumber, BigNumberish, BytesLike } from 'ethers';
import { Route } from '@lifi/types';
import { PaymasterApi, ExchangeOffer, Factory } from '@etherspot/prime-sdk';
import { PaymasterApi, ExchangeOffer } from '@etherspot/prime-sdk';

export interface ITransaction {
id?: string;
Expand Down Expand Up @@ -45,7 +45,6 @@ export interface IBatches {
onSent?: (sent: SentBatch[]) => void;
skip?: boolean;
paymaster?: PaymasterApi,
addressTemplate?: Factory;
}

export type IEstimatedBatches = IBatches & {
Expand Down

0 comments on commit f38974e

Please sign in to comment.