Skip to content

Commit

Permalink
♻️Add types & refactor getBestTrade
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPoblete committed Jun 21, 2024
1 parent db7ada3 commit 9114880
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
23 changes: 19 additions & 4 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { useSorobanReact } from '@soroban-react/core';
import { useFactory } from 'hooks';
import { useContext, useMemo } from 'react';
import { CurrencyAmount, Networks, Protocols, Router, Token, TradeType } from 'soroswap-router-sdk';
import { Currency, CurrencyAmount, Networks, Percent, Protocols, Route, Router, Token, TradeType } from 'soroswap-router-sdk';
import { AppContext } from 'contexts';
import axios from 'axios';
import { TokenType } from 'interfaces';
import { getBestPath, getHorizonBestPath } from 'helpers/horizon/getHorizonPath';
import { PlatformType } from 'state/routing/types';
import { CurrencyAmount as AmountAsset } from 'interfaces';

export interface BuildTradeRoute {
amountCurrency: AmountAsset | CurrencyAmount<Currency>;
quoteCurrency: AmountAsset | CurrencyAmount<Currency>;
tradeType: TradeType;
routeCurrency: any[] | Route<Currency, Currency>;
trade: {
amountIn?: string;
amountOut?: string;
amountOutMin?: string;
amountInMax?: string;
path: string[];
};
priceImpact: Percent;
platform: PlatformType;
}

export interface GenerateRouteProps {
amountAsset: AmountAsset;
Expand Down Expand Up @@ -93,8 +108,8 @@ export const useRouterSDK = () => {
tradeType,
};

const horizonPath = await getHorizonBestPath(horizonProps, sorobanContext);
const routerPath = await router.route(currencyAmount, quoteCurrency, tradeType, factory, sorobanContext as any).then(res=>{
const horizonPath: BuildTradeRoute | undefined = await getHorizonBestPath(horizonProps, sorobanContext);
const routerPath: BuildTradeRoute | undefined = await router.route(currencyAmount, quoteCurrency, tradeType, factory, sorobanContext as any).then(res=>{
if(!res) return;
const response = {
...res,
Expand All @@ -103,7 +118,7 @@ export const useRouterSDK = () => {
return response;
})

const bestPath = getBestPath(horizonPath, routerPath, tradeType);
const bestPath = getBestPath(horizonPath!, routerPath!, tradeType);

return bestPath;
};
Expand Down
47 changes: 14 additions & 33 deletions src/helpers/horizon/getHorizonPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { SorobanContextType } from '@soroban-react/core';
import BigNumber from 'bignumber.js';
import { CurrencyAmount, TokenType } from 'interfaces';
import { PlatformType, TradeType } from 'state/routing/types';
import { Percent } from 'soroswap-router-sdk';
import { BuildTradeRoute } from 'functions/generateRoute';



Expand All @@ -25,7 +27,7 @@ export const getAmount = (amount: string) => {
return new BigNumber(amount).dividedBy(10000000).toString()
}

export const parseHorizonResult = (payload: ServerApi.PaymentPathRecord | undefined, tradeType: TradeType) =>{
export const parseHorizonResult = (payload: ServerApi.PaymentPathRecord | undefined, tradeType: TradeType) => {
if (!payload) return;
const currecnyIn: TokenType = payload.source_asset_type == 'native' ? {
code: 'XLM',
Expand Down Expand Up @@ -71,13 +73,14 @@ export const parseHorizonResult = (payload: ServerApi.PaymentPathRecord | undefi
path: formattedPath
}
}
const priceImpact = new Percent(0)
const result = {
amountCurrency: inputAmount,
quoteCurrency: outputAmount,
tradeType: tradeType,
routeCurrency:[currecnyIn, ...payload.path, currencyOut],
trade: trade,
priceImpact: undefined,
priceImpact: priceImpact,
platform: PlatformType.STELLAR_CLASSIC,
}
return result
Expand Down Expand Up @@ -126,7 +129,6 @@ export function getHorizonBestPath(
return maxObj;
}
});
console.log(maxObj)
return parseHorizonResult(maxObj, payload.tradeType);
});
} catch (error) {
Expand All @@ -152,7 +154,6 @@ export function getHorizonBestPath(
return minObj;
}
});
console.log(minObj)
return parseHorizonResult(minObj, payload.tradeType);
});
} catch (error) {
Expand All @@ -161,43 +162,23 @@ export function getHorizonBestPath(
}
}

export const getBestPath = (horizonPath: any, routerPath: any, tradeType: TradeType)=>{
if(!horizonPath && !routerPath || !routerPath) return;
export const getBestPath = (horizonPath: BuildTradeRoute, routerPath: BuildTradeRoute, tradeType: TradeType)=>{
if(!tradeType) return;
if(!horizonPath) return routerPath
if(!routerPath) return horizonPath
if (tradeType === TradeType.EXACT_INPUT) {
const horizonAmountOutMin = parseInt(horizonPath?.trade.amountOutMin || '0');
const routerAmountOutMin = parseInt(routerPath?.trade.amountOutMin || '0');
if (horizonAmountOutMin !== 0 && routerAmountOutMin !== 0) {
if (routerAmountOutMin > horizonAmountOutMin) {
console.log('returning routerPath')
return routerPath;
} else {
console.log('returning horizonPath')
return horizonPath;
}
} else if (routerPath?.trade.amountOutMin) {
console.log('returning routerPath')
return routerPath;
} else if (horizonPath?.trade.amountOutMin) {
console.log('returning horizonPath')
return horizonPath;
}
if (routerAmountOutMin > horizonAmountOutMin) return routerPath;
else return horizonPath;
}
} else if (tradeType === TradeType.EXACT_OUTPUT) {
const horizonAmountInMax = parseInt(horizonPath?.trade.amountInMax || '0');
const routerAmountInMax = parseInt(routerPath?.trade.amountInMax || '0');
if (horizonAmountInMax !== 0 && routerAmountInMax !== 0) {
if (routerAmountInMax < horizonAmountInMax) {
console.log('returning routerPath')
return routerPath;
} else {
console.log('returning horizonPath')
return horizonPath;
}
} else if (routerPath?.trade.amountInMax) {
console.log('returning routerPath')
return routerPath;
} else if (horizonPath?.trade.amountInMax) {
console.log('returning horizonPath')
return horizonPath;
if (routerAmountInMax < horizonAmountInMax) return routerPath;
else return horizonPath;
}
}
}
3 changes: 1 addition & 2 deletions src/hooks/useBestTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { useRouterSDK } from 'functions/generateRoute';
import { CurrencyAmount, TokenType } from 'interfaces';
import { useEffect, useMemo, useState } from 'react';
import { TradeType as SdkTradeType } from 'soroswap-router-sdk';
import { InterfaceTrade, PlatformType, QuoteState, TradeState, TradeType } from 'state/routing/types';
import { InterfaceTrade, QuoteState, TradeState, TradeType } from 'state/routing/types';
import useSWR from 'swr';
import { SorobanContextType } from '@soroban-react/core';

const TRADE_NOT_FOUND = {
state: TradeState.NO_ROUTE_FOUND,
Expand Down

0 comments on commit 9114880

Please sign in to comment.