Skip to content

Commit

Permalink
✨Set default protocols by network
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPoblete committed Sep 4, 2024
1 parent f210de0 commit 89d7d5f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
21 changes: 2 additions & 19 deletions src/components/Providers.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,20 +19,7 @@ export default function Providers({
const [isConnectWalletModal, setConnectWalletModal] = useState<boolean>(false);

const [maxHops, setMaxHops] = useState<number>(2);

//Defines the default protocols to be used in the app
const defaultProtocols = [
Protocols.SOROSWAP
]
const [protocols, setProtocols] = useState<Protocols[]>(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<ProtocolsStatus[]>(defaultProtocolsStatus);
const [protocolsStatus, setProtocolsStatus] = useState<ProtocolsStatus[]>([]);
const [openSnackbar, setOpenSnackbar] = useState<boolean>(false);
const [snackbarMessage, setSnackbarMessage] = useState<string>('');
const [snackbarTitle, setSnackbarTitle] = useState<string>('Swapped');
Expand Down Expand Up @@ -68,8 +53,6 @@ export default function Providers({
Settings: {
maxHops,
setMaxHops,
protocols,
setProtocols,
protocolsStatus,
setProtocolsStatus,
},
Expand Down
1 change: 0 additions & 1 deletion src/components/Settings/ProtocolsSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const ProtocolsSettings = () => {
setProtocolsStatus(newProtocolsStatus);
mutate(
(key: any) => {
console.log(key)
return true;
},
undefined,
Expand Down
4 changes: 0 additions & 4 deletions src/contexts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export type SnackbarContextType = {
export type Settings = {
maxHops: number;
setMaxHops: React.Dispatch<React.SetStateAction<number>>;
protocols: Protocols[];
setProtocols: React.Dispatch<React.SetStateAction<Protocols[]>>;
protocolsStatus: ProtocolsStatus[];
setProtocolsStatus: React.Dispatch<React.SetStateAction<ProtocolsStatus[]>>;
};
Expand Down Expand Up @@ -68,8 +66,6 @@ export const AppContext = React.createContext<AppContextType>({
Settings: {
maxHops: 2,
setMaxHops: () => {},
protocols: [],
setProtocols: () => {},
protocolsStatus: [],
setProtocolsStatus: () => {},
},
Expand Down
48 changes: 46 additions & 2 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 89d7d5f

Please sign in to comment.