diff --git a/README.md b/README.md index ca9df5e7..0ee7c692 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,8 @@ cp .env.production.example .env You have successfully set up Soroswap on your local machine! Start swapping, pooling, and exploring the possibilities of decentralized finance (DeFi) on the Soroban network. +If you want to add or remove supported protocols, you can do so by editing the `functions/generateRoute.ts:79-97` file and adding or removing the protocols you want to support on swap. + ## 🧪🔨 Testing 🧪🔨 To execute the tests, you must first start the development container. To do this, run the following command from your host machine: diff --git a/src/components/Providers.tsx b/src/components/Providers.tsx index e2d0d44d..1669b353 100644 --- a/src/components/Providers.tsx +++ b/src/components/Providers.tsx @@ -1,15 +1,13 @@ import { Analytics } from '@vercel/analytics/react'; import { AppContext, AppContextType, ColorModeContext, SnackbarIconType, ProtocolsStatus } from 'contexts'; import { Provider } from 'react-redux'; -import { useEffect, useMemo, useState } from 'react'; +import { useMemo, useState } from 'react'; import InkathonProvider from 'inkathon/InkathonProvider'; import MainLayout from './Layout/MainLayout'; import MySorobanReactProvider from 'soroban/MySorobanReactProvider'; import store from 'state'; import { SorobanContextType } from '@soroban-react/core'; import { SoroswapThemeProvider } from 'soroswap-ui'; -import { Protocols } from 'soroswap-router-sdk'; -import { PlatformType } from 'state/routing/types'; export default function Providers({ children, @@ -21,20 +19,7 @@ export default function Providers({ const [isConnectWalletModal, setConnectWalletModal] = useState(false); const [maxHops, setMaxHops] = useState(2); - - //Defines the default protocols to be used in the app - const defaultProtocols = [ - Protocols.SOROSWAP - ] - const [protocols, setProtocols] = useState(defaultProtocols); - - //Defines the default platforms to be used in the app - const defaultProtocolsStatus: ProtocolsStatus[] = [ - { key: Protocols.SOROSWAP, value: true }, - { key: Protocols.PHOENIX, value: false }, - { key: PlatformType.STELLAR_CLASSIC, value: true }, - ] - const [protocolsStatus, setProtocolsStatus] = useState(defaultProtocolsStatus); + const [protocolsStatus, setProtocolsStatus] = useState([]); const [openSnackbar, setOpenSnackbar] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(''); const [snackbarTitle, setSnackbarTitle] = useState('Swapped'); @@ -68,8 +53,6 @@ export default function Providers({ Settings: { maxHops, setMaxHops, - protocols, - setProtocols, protocolsStatus, setProtocolsStatus, }, diff --git a/src/components/Settings/ProtocolsSettings/index.tsx b/src/components/Settings/ProtocolsSettings/index.tsx index 921f0077..12276c4f 100644 --- a/src/components/Settings/ProtocolsSettings/index.tsx +++ b/src/components/Settings/ProtocolsSettings/index.tsx @@ -83,7 +83,6 @@ const ProtocolsSettings = () => { setProtocolsStatus(newProtocolsStatus); mutate( (key: any) => { - console.log(key) return true; }, undefined, diff --git a/src/contexts/index.ts b/src/contexts/index.ts index 0b6ef333..f3a45c7d 100644 --- a/src/contexts/index.ts +++ b/src/contexts/index.ts @@ -34,8 +34,6 @@ export type SnackbarContextType = { export type Settings = { maxHops: number; setMaxHops: React.Dispatch>; - protocols: Protocols[]; - setProtocols: React.Dispatch>; protocolsStatus: ProtocolsStatus[]; setProtocolsStatus: React.Dispatch>; }; @@ -68,8 +66,6 @@ export const AppContext = React.createContext({ Settings: { maxHops: 2, setMaxHops: () => {}, - protocols: [], - setProtocols: () => {}, protocolsStatus: [], setProtocolsStatus: () => {}, }, diff --git a/src/functions/generateRoute.ts b/src/functions/generateRoute.ts index 74b3bb1e..052a5fe9 100644 --- a/src/functions/generateRoute.ts +++ b/src/functions/generateRoute.ts @@ -2,7 +2,7 @@ import { useSorobanReact } from '@soroban-react/core'; import { AppContext } from 'contexts'; import { useFactory } from 'hooks'; import { useAggregator } from 'hooks/useAggregator'; -import { useContext, useMemo } from 'react'; +import { useContext, useEffect, useMemo } from 'react'; import { fetchAllPhoenixPairs, fetchAllSoroswapPairs } from 'services/pairs'; import { Currency, @@ -57,10 +57,54 @@ export const useRouterSDK = () => { const { isEnabled: isAggregator } = useAggregator(); const { Settings } = useContext(AppContext); - const { maxHops, protocols, protocolsStatus } = Settings; + const { maxHops, protocolsStatus, setProtocolsStatus } = Settings; const network = sorobanContext.activeChain?.networkPassphrase as Networks; + const getValuebyKey = (key: string) => { + let value = protocolsStatus.find((p) => p.key === key)?.value; + if (typeof value === 'undefined') { + return false; + } + if (typeof value === 'undefined' && key === Protocols.SOROSWAP) { + return true; + } + if (value === true || value === false) { + return value; + } + return value; + } + + const getDefaultProtocolsStatus = async (network: Networks) => { + switch (network) { + case Networks.PUBLIC: + // here you should add your new supported protocols + return [ + { key: Protocols.SOROSWAP , value: getValuebyKey(Protocols.SOROSWAP) }, + { key: PlatformType.STELLAR_CLASSIC, value: getValuebyKey(PlatformType.STELLAR_CLASSIC) }, + ]; + case Networks.TESTNET: + return [ + { key: Protocols.SOROSWAP, value: getValuebyKey(Protocols.SOROSWAP) }, + { key: Protocols.PHOENIX, value: getValuebyKey(Protocols.PHOENIX) }, + ]; + default: + return [ + { key: Protocols.SOROSWAP, value: true }, + { key: Protocols.PHOENIX, value: false }, + { key: PlatformType.STELLAR_CLASSIC, value: false }, + ]; + } + } + + useEffect(() => { + const fetchProtocolsStatus = async () => { + const defaultProtocols = await getDefaultProtocolsStatus(network); + setProtocolsStatus(defaultProtocols); + }; + fetchProtocolsStatus(); + }, [network]); + const getPairsFns = useMemo(() => { const routerProtocols = [] if(!shouldUseBackend) return undefined