Skip to content

Commit

Permalink
fix not use backend
Browse files Browse the repository at this point in the history
  • Loading branch information
esteblock committed Sep 29, 2024
1 parent b2414db commit a1fd363
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
CurrencyAmount,
Networks,
Percent,
Protocols,
Protocol,
Route,
Router,
Token,
TradeType,
} from 'soroswap-router-sdk';
} from 'soroswap-router-sdk/src';
import { TokenType } from 'interfaces';
import { getBestPath, getHorizonBestPath } from 'helpers/horizon/getHorizonPath';
import { PlatformType } from 'state/routing/types';
Expand Down Expand Up @@ -54,28 +54,47 @@ const shouldUseBackend = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED === 't

export const useRouterSDK = () => {
const sorobanContext = useSorobanReact();
console.log("🚀 ~ useRouterSDK 0 ~ sorobanContext:", sorobanContext)
const { factory } = useFactory(sorobanContext);
console.log("🚀 ~ useRouterSDK 0 ~ factory:", factory)
const { isEnabled: isAggregator } = useAggregator();
console.log("🚀 ~ useRouterSDK 0 ~ isAggregator:", isAggregator)

const { Settings } = useContext(AppContext);
console.log("🚀 ~ useRouterSDK 0 ~ Settings:", Settings)
const { maxHops, protocolsStatus, setProtocolsStatus } = Settings;
console.log("🚀 ~ useRouterSDK 0 ~ protocolsStatus:", protocolsStatus)

const network = sorobanContext.activeChain?.networkPassphrase as Networks;
console.log("🚀 ~ useRouterSDK 0 ~ network:", network)

const getPairsFns = useMemo(() => {
console.log("🚀 ~ useRouterSDK 0 ~ protocolsStatus 2:", protocolsStatus)

console.log("🚀 ~ useRouterSDK 0 ~ getPairsFns will generate")
console.log("🚀 ~ useRouterSDK 0 ~ getPairsFns ~ shouldUseBackend:", shouldUseBackend)

const routerProtocols = []
if(!shouldUseBackend) return undefined
// here you should add your new supported aggregator protocols
if(shouldUseBackend) return undefined
console.log("🚀 ~ useRouterSDK 0 ~ protocolsStatus 3:", protocolsStatus)

// here you should add your new supported aggregator protocols
for(let protocol of protocolsStatus){
if(protocol.key === Protocols.SOROSWAP && protocol.value === true){
routerProtocols.push({protocol: Protocols.SOROSWAP, fn: async () => fetchAllSoroswapPairs(network)});
console.log("inside:", protocolsStatus)

console.log("🚀 useRouterSDK 0 ~ getPairsFns ~ protocolsStatus:", protocolsStatus)
console.log("🚀 useRouterSDK 0 ~ getPairsFns ~ protocol:", protocol)

if(protocol.key === Protocol.SOROSWAP && protocol.value === true){
routerProtocols.push({protocol: Protocol.SOROSWAP, fn: async () => fetchAllSoroswapPairs(network)});
}
if(protocol.key === Protocols.PHOENIX && protocol.value === true){
routerProtocols.push({protocol: Protocols.PHOENIX, fn: async () => fetchAllPhoenixPairs(network)});
if(protocol.key === Protocol.PHOENIX && protocol.value === true){
routerProtocols.push({protocol: Protocol.PHOENIX, fn: async () => fetchAllPhoenixPairs(network)});
}
}
return routerProtocols;
}, [network, protocolsStatus]);
}, [network, protocolsStatus, sorobanContext]);
console.log("🚀 ~ useRouterSDK 0 ~ getPairsFns:", getPairsFns)

const getProtocols = useMemo(() => {
const newProtocols = [];
Expand All @@ -84,7 +103,7 @@ export const useRouterSDK = () => {
newProtocols.push(protocol.key);
}
}
return newProtocols as Protocols[];
return newProtocols as Protocol[];
},[protocolsStatus]);

const router = useMemo(() => {
Expand Down Expand Up @@ -117,42 +136,62 @@ export const useRouterSDK = () => {
tradeType,
currentProtocolsStatus,
}: GenerateRouteProps) => {
console.log("🚀 ~ useRouterSDK ~ currentProtocolsStatus:", currentProtocolsStatus)
console.log("🚀 ~ useRouterSDK ~ tradeType:", tradeType)
console.log("🚀 ~ useRouterSDK ~ amount:", amount)
console.log("🚀 ~ useRouterSDK ~ quoteAsset:", quoteAsset)
console.log("🚀 ~ useRouterSDK ~ amountAsset:", amountAsset)
if (!factory) throw new Error('Factory address not found');
const currencyAmount = fromAddressAndAmountToCurrencyAmount(
amountAsset.currency.contract,
amount,
);
console.log("🚀 ~ useRouterSDK ~ currencyAmount:", currencyAmount)
const quoteCurrency = fromAddressToToken(quoteAsset.contract);
console.log("🚀 ~ useRouterSDK ~ quoteCurrency:", quoteCurrency)

const isHorizonEnabled = currentProtocolsStatus.find((p) => p.key === PlatformType.STELLAR_CLASSIC)?.value;
console.log("🚀 ~ useRouterSDK ~ isHorizonEnabled:", isHorizonEnabled)

const isSoroswapEnabled = currentProtocolsStatus.find((p) => p.key === Protocols.SOROSWAP)?.value;
const isSoroswapEnabled = currentProtocolsStatus.find((p) => p.key === Protocol.SOROSWAP)?.value;
console.log("🚀 ~ useRouterSDK ~ isSoroswapEnabled:", isSoroswapEnabled)

const horizonProps = {
assetFrom: amountAsset.currency,
assetTo: quoteAsset,
amount,
tradeType,
};
console.log("🚀 ~ useRouterSDK ~ horizonProps:", horizonProps)

let horizonPath: BuildTradeRoute | undefined;
if(isHorizonEnabled){
horizonPath = (await getHorizonBestPath(horizonProps, sorobanContext)) as BuildTradeRoute;
}
console.log("🚀 ~ useRouterSDK ~ horizonPath:", horizonPath)

let sorobanPath: BuildTradeRoute | undefined;
console.log("🚀 ~ useRouterSDK will ~ isAggregator:", isAggregator)
console.log("🚀 ~ useRouterSDK will ~ isSoroswapEnabled:", isSoroswapEnabled)
if (isAggregator) {
console.log(" will use aggregator")
sorobanPath = (await router
.routeSplit(currencyAmount, quoteCurrency, tradeType)
.then((response) => {
console.log("🚀 ~ useRouterSDK will s~ response:", response)
if (!response) return undefined;
const result = {
...response,
platform: PlatformType.AGGREGATOR,
};
return result;
})) as BuildTradeRoute;
console.log(" will use aggregator with path")

console.log("🚀 ~ useRouterSDK will ~ sorobanPath 0:", sorobanPath)
} else if(isSoroswapEnabled){
console.log(" will use only soroswap")

sorobanPath = (await router
.route(currencyAmount, quoteCurrency, tradeType, factory, sorobanContext as any)
.then((response) => {
Expand All @@ -163,9 +202,14 @@ export const useRouterSDK = () => {
};
return result;
})) as BuildTradeRoute;
console.log("🚀 ~ useRouterSDK will ~ sorobanPath 1:", sorobanPath)
}
console.log("🚀 ~ useRouterSDK will send ~ tradeType:", tradeType)
console.log("🚀 ~ useRouterSDK will send ~ sorobanPath:", sorobanPath)
console.log("🚀 ~ useRouterSDK will send ~ horizonPath:", horizonPath)

const bestPath = getBestPath(horizonPath, sorobanPath, tradeType);
console.log("🚀 ~ useRouterSDK ~ bestPath:", bestPath)

return bestPath;
};
Expand Down

0 comments on commit a1fd363

Please sign in to comment.