From a205342b7315e014d605faef8ce2888727084375 Mon Sep 17 00:00:00 2001 From: Nate Reiners Date: Mon, 4 Mar 2024 16:22:33 -0800 Subject: [PATCH] testapp dropdown, update more tests --- apps/testapp/src/components/Layout.tsx | 14 ++++---- .../context/CBWSDKReactContextProvider.tsx | 33 +++++++------------ .../src/CoinbaseWalletProvider.test.ts | 6 ++-- .../wallet-sdk/src/CoinbaseWalletSDK.test.ts | 8 ++--- packages/wallet-sdk/src/index.ts | 1 - 5 files changed, 26 insertions(+), 36 deletions(-) diff --git a/apps/testapp/src/components/Layout.tsx b/apps/testapp/src/components/Layout.tsx index 80bd01a41d..cef0875a9e 100644 --- a/apps/testapp/src/components/Layout.tsx +++ b/apps/testapp/src/components/Layout.tsx @@ -20,7 +20,7 @@ type LayoutProps = { export const WIDTH_2XL = '1536px'; export function Layout({ children }: LayoutProps) { - const { provider, connectionPreference, setPreference } = useCBWSDK(); + const { provider, smartWalletOnly, setPreference } = useCBWSDK(); const handleDisconnect = () => { if (provider) { @@ -37,17 +37,17 @@ export function Layout({ children }: LayoutProps) { }> - {`Mode: ${connectionPreference}`} + {`smartWalletOnly: ${smartWalletOnly}`} - {['default', 'embedded'].map((preference) => ( + {[true, false].map((b) => ( : null} - onClick={() => setPreference(preference)} + key={b.toString()} + icon={b === smartWalletOnly ? : null} + onClick={() => setPreference(b)} > - {preference} + {b.toString()} ))} diff --git a/apps/testapp/src/context/CBWSDKReactContextProvider.tsx b/apps/testapp/src/context/CBWSDKReactContextProvider.tsx index 4d58717977..4e5a248053 100644 --- a/apps/testapp/src/context/CBWSDKReactContextProvider.tsx +++ b/apps/testapp/src/context/CBWSDKReactContextProvider.tsx @@ -1,5 +1,4 @@ import CoinbaseWalletSDK from '@coinbase/wallet-sdk'; -import { ConnectionPreference } from '@coinbase/wallet-sdk/dist/core/communicator/ConnectionPreference'; import React, { useEffect, useMemo } from 'react'; type CBWSDKProviderProps = { @@ -7,32 +6,24 @@ type CBWSDKProviderProps = { }; const CBWSDKReactContext = React.createContext(null); -const PREFERRED_CONNECTION_KEY = 'preferred_connection'; +const SMART_WALLET_ONLY_KEY = 'smart_wallet_only'; export function CBWSDKReactContextProvider({ children }: CBWSDKProviderProps) { - const [connectionPreference, setConnectionPreference] = React.useState< - ConnectionPreference | undefined - >(undefined); + const [smartWalletOnly, setSmartWalletOnly] = React.useState(undefined); const [sdk, setSdk] = React.useState(null); const [provider, setProvider] = React.useState(null); useEffect(() => { - if (connectionPreference === undefined) { - const savedPreference = localStorage.getItem( - PREFERRED_CONNECTION_KEY - ) as ConnectionPreference; - setConnectionPreference( - ['default', 'embedded'].includes(savedPreference) - ? savedPreference - : ('default' as ConnectionPreference) - ); + if (smartWalletOnly === undefined) { + const smartWalletOnly = localStorage.getItem(SMART_WALLET_ONLY_KEY); + setSmartWalletOnly(smartWalletOnly === 'true' ? true : false); } - }, [connectionPreference]); + }, [smartWalletOnly]); useEffect(() => { const cbwsdk = new CoinbaseWalletSDK({ appName: 'SDK Playground', - connectionPreference: connectionPreference ?? 'default', + smartWalletOnly, }); setSdk(cbwsdk); const cbwprovider = cbwsdk.makeWeb3Provider(); @@ -40,18 +31,18 @@ export function CBWSDKReactContextProvider({ children }: CBWSDKProviderProps) { cbwprovider.on('disconnect', () => { location.reload(); }); - }, [connectionPreference]); + }, [smartWalletOnly]); - const setPreference = (connectionPreference: ConnectionPreference) => { - localStorage.setItem(PREFERRED_CONNECTION_KEY, connectionPreference); - setConnectionPreference(connectionPreference); + const setPreference = (smartWalletOnly: boolean) => { + localStorage.setItem(SMART_WALLET_ONLY_KEY, smartWalletOnly.toString()); + setSmartWalletOnly(smartWalletOnly); }; const ctx = useMemo( () => ({ sdk, provider, - connectionPreference, + smartWalletOnly, setPreference, }), [sdk, provider] diff --git a/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts b/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts index 57196a8434..89bd3015d4 100644 --- a/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts +++ b/packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts @@ -9,7 +9,7 @@ describe('EIP1193Provider', () => { scwUrl: 'http://fooUrl.com', appName: 'TestApp', appChainIds: [], - connectionPreference: 'default', + smartWalletOnly: false, }); }); @@ -30,7 +30,7 @@ describe('EIP1193Provider', () => { scwUrl: 'http://fooUrl.com', appName: 'TestApp', appChainIds: [8453, 84532], - connectionPreference: 'default', + smartWalletOnly: false, }); expect(provider.chainId).toBe(8453); }); @@ -40,7 +40,7 @@ describe('EIP1193Provider', () => { scwUrl: 'http://fooUrl.com', appName: 'TestApp', appChainIds: [], - connectionPreference: 'default', + smartWalletOnly: false, }); expect(provider.chainId).toBe(1); }); diff --git a/packages/wallet-sdk/src/CoinbaseWalletSDK.test.ts b/packages/wallet-sdk/src/CoinbaseWalletSDK.test.ts index 8a1a3339c3..2d907505dc 100644 --- a/packages/wallet-sdk/src/CoinbaseWalletSDK.test.ts +++ b/packages/wallet-sdk/src/CoinbaseWalletSDK.test.ts @@ -104,11 +104,11 @@ describe('CoinbaseWalletSDK', () => { expect(coinbaseWalletSDK2.makeWeb3Provider()).toEqual(mockProvider); }); - test('@makeWeb3Provider, but with connectionPreference as embedded', () => { + test('@makeWeb3Provider, but with smartWalletOnly as true', () => { const sdk = new CoinbaseWalletSDK({ appName: 'Test', appLogoUrl: 'http://coinbase.com/wallet-logo.png', - connectionPreference: 'embedded', + smartWalletOnly: true, }); // Returns extension provider const provider = sdk.makeWeb3Provider(); @@ -144,11 +144,11 @@ describe('CoinbaseWalletSDK', () => { expect(coinbaseWalletSDK2.makeWeb3Provider()).toEqual(mockCipherProvider); }); - test('@makeWeb3Provider, it should ignore embedded connectionPreference', () => { + test('@makeWeb3Provider, it should ignore smartWalletOnly true', () => { const sdk = new CoinbaseWalletSDK({ appName: 'Test', appLogoUrl: 'http://coinbase.com/wallet-logo.png', - connectionPreference: 'embedded', + smartWalletOnly: true, }); expect(sdk.makeWeb3Provider()).toEqual(mockCipherProvider); }); diff --git a/packages/wallet-sdk/src/index.ts b/packages/wallet-sdk/src/index.ts index ce89233dc8..09b5b5250b 100644 --- a/packages/wallet-sdk/src/index.ts +++ b/packages/wallet-sdk/src/index.ts @@ -5,7 +5,6 @@ import { CoinbaseWalletSDK } from './CoinbaseWalletSDK'; import { ProviderInterface } from './core/type/ProviderInterface'; export { CoinbaseWalletSDK } from './CoinbaseWalletSDK'; -export type { ConnectionPreference } from './core/communicator/ConnectionPreference'; export default CoinbaseWalletSDK; declare global {