diff --git a/widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx b/widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx index bb8544a035..aef726425e 100644 --- a/widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx +++ b/widget/embedded/src/containers/WidgetInfo/WidgetInfo.tsx @@ -4,6 +4,7 @@ import { useManager } from '@rango-dev/queue-manager-react'; import React, { createContext, useContext } from 'react'; import { useLanguage } from '../../hooks/useLanguage'; +import { useUpdateQuoteInput } from '../../hooks/useUpdateQuoteInput/useUpdateQuoteInput'; import { useAppStore } from '../../store/AppStore'; import { useNotificationStore } from '../../store/notification'; import { useQuoteStore } from '../../store/quote'; @@ -35,6 +36,7 @@ export function WidgetInfo(props: React.PropsWithChildren) { const resetLanguage = useLanguage().resetLanguage; const notifications = useNotificationStore().getNotifications(); const clearNotifications = useNotificationStore().clearNotifications; + const updateQuoteInput = useUpdateQuoteInput(); // eslint-disable-next-line react/jsx-no-constructed-context-values const value: WidgetInfoContextInterface = { @@ -59,6 +61,9 @@ export function WidgetInfo(props: React.PropsWithChildren) { list: notifications, clearAll: clearNotifications, }, + quote: { + updateQuoteInput, + }, }; return ( diff --git a/widget/embedded/src/containers/WidgetInfo/WidgetInfo.types.ts b/widget/embedded/src/containers/WidgetInfo/WidgetInfo.types.ts index e1b6765fc8..13c51bf771 100644 --- a/widget/embedded/src/containers/WidgetInfo/WidgetInfo.types.ts +++ b/widget/embedded/src/containers/WidgetInfo/WidgetInfo.types.ts @@ -1,7 +1,7 @@ import type { WidgetHistory } from './WidgetInfo.helpers'; import type { FetchStatus, FindToken } from '../../store/slices/data'; import type { ConnectedWallet } from '../../store/wallets'; -import type { Wallet } from '../../types'; +import type { UpdateQuoteInput, Wallet } from '../../types'; import type { Notification } from '../../types/notification'; import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk'; @@ -27,4 +27,7 @@ export interface WidgetInfoContextInterface { list: Notification[]; clearAll: () => void; }; + quote: { + updateQuoteInput: UpdateQuoteInput; + }; } diff --git a/widget/embedded/src/hooks/useUpdateQuoteInput/index.ts b/widget/embedded/src/hooks/useUpdateQuoteInput/index.ts new file mode 100644 index 0000000000..4ed3dd9eea --- /dev/null +++ b/widget/embedded/src/hooks/useUpdateQuoteInput/index.ts @@ -0,0 +1 @@ +export { useUpdateQuoteInput } from './useUpdateQuoteInput'; diff --git a/widget/embedded/src/hooks/useUpdateQuoteInput/useUpdateQuoteInput.ts b/widget/embedded/src/hooks/useUpdateQuoteInput/useUpdateQuoteInput.ts new file mode 100644 index 0000000000..692cc8f1b6 --- /dev/null +++ b/widget/embedded/src/hooks/useUpdateQuoteInput/useUpdateQuoteInput.ts @@ -0,0 +1,50 @@ +import type { UpdateQuoteInput } from '../../types'; + +import { useAppStore } from '../../store/AppStore'; +import { useQuoteStore } from '../../store/quote'; + +// This hook provides a function to update quote inputs using fewer and simpler parameters. +export function useUpdateQuoteInput() { + const { findToken } = useAppStore(); + const blockchains = useAppStore().blockchains(); + const tokens = useAppStore().tokens(); + const { + setFromBlockchain, + setFromToken, + setToBlockchain, + setToToken, + setInputAmount, + } = useQuoteStore(); + + const updateQuoteInput: UpdateQuoteInput = (params) => { + const { fromBlockchain, fromToken, toBlockchain, toToken, requestAmount } = + params; + const meta = { blockchains, tokens }; + + if (fromBlockchain !== undefined) { + const blockchainMeta = + blockchains.find((blockchain) => blockchain.name === fromBlockchain) ?? + null; + setFromBlockchain(blockchainMeta); + } + if (fromToken !== undefined) { + const token = fromToken ? findToken(fromToken) ?? null : null; + setFromToken({ meta, token }); + } + if (toBlockchain !== undefined) { + const blockchainMeta = + blockchains.find((blockchain) => blockchain.name === toBlockchain) ?? + null; + setToBlockchain(blockchainMeta); + } + if (toToken !== undefined) { + const token = toToken ? findToken(toToken) ?? null : null; + setToToken({ meta, token }); + } + if (requestAmount !== undefined) { + setInputAmount(requestAmount); + } + }; + + return updateQuoteInput; +} diff --git a/widget/embedded/src/types/quote.ts b/widget/embedded/src/types/quote.ts index 505f1c2de3..4a07223773 100644 --- a/widget/embedded/src/types/quote.ts +++ b/widget/embedded/src/types/quote.ts @@ -3,6 +3,7 @@ import type { PriceImpactWarningLevel } from '@rango-dev/ui'; import type BigNumber from 'bignumber.js'; import type { + Asset, BlockchainValidationStatus, RouteTag, RoutingResultType, @@ -131,3 +132,11 @@ export type QuoteErrorResponse = { message: string; options: QuoteError; }; + +export type UpdateQuoteInput = (params: { + fromBlockchain?: string | null; + toBlockchain?: string | null; + fromToken?: Asset | null; + toToken?: Asset | null; + requestAmount?: string; +}) => void;