From 8796bdaa2256d1b4939d94fa60a174d265eeed91 Mon Sep 17 00:00:00 2001 From: Ji Young Lee <641712+jiyounglee@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:36:20 +1000 Subject: [PATCH] [NO CHANGELOG][Add Funds Widget] Add funds/squid config (#2178) Co-authored-by: Mimi Tran --- packages/checkout/sdk/src/index.ts | 1 + packages/checkout/sdk/src/types/config.ts | 11 +++++++ .../src/widgets/add-funds/AddFundsWidget.tsx | 32 +++++++++++++++--- .../add-funds/context/AddFundsContext.ts | 14 +++----- .../context/AddFundsContextProvider.tsx | 27 --------------- .../src/widgets/add-funds/hooks/useSquid.ts | 33 +++++++++++++++---- 6 files changed, 71 insertions(+), 47 deletions(-) delete mode 100644 packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContextProvider.tsx diff --git a/packages/checkout/sdk/src/index.ts b/packages/checkout/sdk/src/index.ts index 5e1ddf54de..c22d528308 100644 --- a/packages/checkout/sdk/src/index.ts +++ b/packages/checkout/sdk/src/index.ts @@ -144,6 +144,7 @@ export type { TransactionRequirement, WalletFilter, WalletInfo, + SquidConfig, } from './types'; export { diff --git a/packages/checkout/sdk/src/types/config.ts b/packages/checkout/sdk/src/types/config.ts index a57b922116..bde7aac84c 100644 --- a/packages/checkout/sdk/src/types/config.ts +++ b/packages/checkout/sdk/src/types/config.ts @@ -60,6 +60,7 @@ export interface CheckoutModuleConfiguration extends ModuleConfiguration ({ + addFundsState, + addFundsDispatch, + }), + [addFundsState, addFundsDispatch], + ); + + const squid = useSquid(checkout); + + useEffect(() => { + if (!squid || addFundsState.squid) return; + + addFundsDispatch({ + payload: { + type: AddFundsActions.SET_SQUID, + squid, + }, + }); + }, [squid]); useEffect(() => { if (!web3Provider) return; @@ -82,7 +106,7 @@ export default function AddFundsWidget({ return ( - + {viewState.view.type === AddFundsWidgetViews.ADD_FUNDS && ( )} - + ); } diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContext.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContext.ts index 1b7e60f125..1c0b9f62cf 100644 --- a/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContext.ts +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContext.ts @@ -1,5 +1,5 @@ import { Web3Provider } from '@ethersproject/providers'; -import { createContext, useMemo, useReducer } from 'react'; +import { createContext } from 'react'; import { Checkout, TokenInfo } from '@imtbl/checkout-sdk'; import { Squid } from '@0xsquid/sdk'; @@ -10,7 +10,7 @@ export interface AddFundsState { squid: Squid | null; } -const initialAddFundsState: AddFundsState = { +export const initialAddFundsState: AddFundsState = { checkout: null, provider: null, allowedTokens: null, @@ -66,9 +66,9 @@ export const AddFundsContext = createContext({ AddFundsContext.displayName = 'AddFundsContext'; -type Reducer = (prevState: S, action: A) => S; +export type Reducer = (prevState: S, action: A) => S; -const addFundsReducer: Reducer = ( +export const addFundsReducer: Reducer = ( state: AddFundsState, action: AddFundsAction, ) => { @@ -97,9 +97,3 @@ const addFundsReducer: Reducer = ( return state; } }; - -export const useAddFundsValues = (overrides: Partial = {}) => { - const [addFundsState, addFundsDispatch] = useReducer(addFundsReducer, { ...initialAddFundsState, ...overrides }); - const values = useMemo(() => ({ addFundsState, addFundsDispatch }), [addFundsState, addFundsDispatch]); - return values; -}; diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContextProvider.tsx b/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContextProvider.tsx deleted file mode 100644 index 99f3118a80..0000000000 --- a/packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContextProvider.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { ReactNode, useEffect } from 'react'; -import { AddFundsActions, useAddFundsValues, AddFundsContext } from './AddFundsContext'; -import { useSquid } from '../hooks/useSquid'; - -export function AddFundsContextProvider({ children }: { children: ReactNode }) { - const addFundsValue = useAddFundsValues(); - const { addFundsState, addFundsDispatch } = addFundsValue; - - const squid = useSquid(); - - useEffect(() => { - if (!squid || addFundsState.squid) return; - - addFundsDispatch({ - payload: { - type: AddFundsActions.SET_SQUID, - squid, - }, - }); - }, [squid]); - - return ( - - {children} - - ); -} diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useSquid.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useSquid.ts index 653703f237..2e7dce29d1 100644 --- a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useSquid.ts +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useSquid.ts @@ -1,22 +1,43 @@ import { Squid } from '@0xsquid/sdk'; -import { useEffect, useState } from 'react'; +import { Checkout, SquidConfig } from '@imtbl/checkout-sdk'; +import { useCallback, useEffect, useState } from 'react'; +import { SQUID_SDK_BASE_URL } from '../utils/config'; -export const useSquid = () => { +export const useSquid = (checkout: Checkout) => { const [squid, setSquid] = useState(null); + const squidConfig = useCallback(async (): Promise< + SquidConfig | undefined + > => { + try { + return (await checkout?.config?.remote.getConfig('squid')) as SquidConfig; + } catch (err: any) { + // eslint-disable-next-line no-console + console.error('Unable to fetch squid config: ', err); + } + return undefined; + }, [checkout]); + useEffect(() => { - if (squid) { + if (squid || !squidConfig) { return; } - const initialiseSquid = async () => { - const squidSDK = null; + const config = await squidConfig(); + + if (!config?.integratorId) return; + + const squidSDK = new Squid({ + baseUrl: SQUID_SDK_BASE_URL, + integratorId: config.integratorId, + }); + await squidSDK.init(); setSquid(squidSDK); }; initialiseSquid(); - }, []); + }, [squidConfig]); return squid; };