Skip to content

Commit

Permalink
refactor simulate and params isAlphaSmartRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Sep 23, 2024
1 parent e8ca9be commit 764e2a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 65 deletions.
2 changes: 0 additions & 2 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,9 +1190,7 @@ export class UniversalSwapHandler {
obridgeAddress
);

// version alpha smart router oraiDEX pool + osmosis pool
if (
swapOptions?.isAlphaSmartRouter &&
this.swapData?.alphaSmartRoutes?.routes?.length &&
["oraichain-to-oraichain", "oraichain-to-cosmos", "cosmos-to-others"].includes(universalSwapType) &&
!swapOptions?.isIbcWasm
Expand Down
82 changes: 36 additions & 46 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import {
SimulateResponse,
SmartRouteSwapOperations,
SmartRouterResponse,
SmartRouterResponseAPI,
SwapDirection,
SwapOptions,
SwapRoute,
Expand Down Expand Up @@ -466,6 +465,9 @@ export class UniversalSwapHelper {
return routeData;
};

/**
* @deprecated. Use UniversalSwapHelper.generateMsgsSmartRouterV2withV3
*/
static generateSwapRoute = (offerAsset: AssetInfo, askAsset: AssetInfo, swapRoute: AssetInfo[]) => {
const swaps = [];
if (swapRoute.length === 0) {
Expand Down Expand Up @@ -500,7 +502,9 @@ export class UniversalSwapHelper {
return swaps;
};

// generate messages
/**
* @deprecated. Use UniversalSwapHelper.generateMsgsSmartRouterV2withV3
*/
static generateSwapOperationMsgs = (offerInfo: AssetInfo, askInfo: AssetInfo): SwapOperation[] => {
const pairExist = PAIRS.some((pair) => {
const assetInfos = pair.asset_infos;
Expand Down Expand Up @@ -534,7 +538,7 @@ export class UniversalSwapHelper {
askChainId: string,
offerAmount: string,
routerConfig: RouterConfigSmartRoute
): Promise<SmartRouterResponseAPI> => {
): Promise<SmartRouterResponse> => {
const { axios } = await getAxios(routerConfig.url);
const data = {
sourceAsset: parseAssetInfo(offerInfo),
Expand All @@ -548,7 +552,7 @@ export class UniversalSwapHelper {
}
};
const res: {
data: SmartRouterResponseAPI;
data: SmartRouterResponse;
} = await axios.post(routerConfig.path, data);
return {
swapAmount: res.data.swapAmount,
Expand All @@ -566,7 +570,7 @@ export class UniversalSwapHelper {
routerConfig: RouterConfigSmartRoute = {
url: "https://osor.oraidex.io",
path: "/smart-router",
protocols: ["Oraidex", "OraidexV3", "Osmosis"],
protocols: ["Oraidex", "OraidexV3"],
dontAllowSwapAfter: ["Oraidex", "OraidexV3"]
}
): Promise<SmartRouterResponse> => {
Expand Down Expand Up @@ -738,51 +742,37 @@ export class UniversalSwapHelper {
return { amount, displayAmount };
}

let amount;
let routes;
let decimals = 6;
if (query?.routerOption?.useAlphaSmartRoute) {
const fromInfo = query?.routerOption?.useIbcWasm
? getTokenOnOraichain(query.originalFromInfo.coinGeckoId)
: query.originalFromInfo;
const toInfo = query?.routerOption?.useIbcWasm
? getTokenOnOraichain(query.originalToInfo.coinGeckoId)
: query.originalToInfo;

if (!fromInfo || !toInfo)
throw new Error(`Cannot find token on Oraichain for token ${fromInfo.coinGeckoId} and ${toInfo.coinGeckoId}`);

const simulateRes: SmartRouterResponse = await UniversalSwapHelper.simulateSwapUsingSmartRoute({
fromInfo,
toInfo,
amount: toAmount(query.originalAmount, fromInfo.decimals).toString(),
routerConfig: query.routerConfig
});
const routerConfigDefault = {
url: query?.routerConfig?.url ?? "https://osor.oraidex.io",
path: query?.routerConfig?.path ?? "/smart-router",
protocols: query?.routerConfig?.protocols ?? ["Oraidex", "OraidexV3"],
dontAllowSwapAfter: query?.routerConfig?.dontAllowSwapAfter ?? ["Oraidex", "OraidexV3"]
};

routes = simulateRes;
amount = simulateRes.returnAmount;
decimals = toInfo.decimals;
} else {
const fromInfo = getTokenOnOraichain(query.originalFromInfo.coinGeckoId);
const toInfo = getTokenOnOraichain(query.originalToInfo.coinGeckoId);
if (!fromInfo || !toInfo)
throw new Error(
`Cannot find token on Oraichain for token ${query.originalFromInfo.coinGeckoId} and ${query.originalToInfo.coinGeckoId}`
);
amount = (
await UniversalSwapHelper.simulateSwap({
fromInfo,
toInfo,
amount: toAmount(query.originalAmount, fromInfo.decimals).toString(),
routerClient: query.routerClient
})
).amount;
decimals = toInfo.decimals;
let fromInfo = getTokenOnOraichain(query.originalFromInfo.coinGeckoId);
let toInfo = getTokenOnOraichain(query.originalToInfo.coinGeckoId);

if (!query?.routerOption?.useIbcWasm) {
fromInfo = query.originalFromInfo;
toInfo = query.originalToInfo;
}

if (!fromInfo || !toInfo)
throw new Error(`Cannot find token on Oraichain for token ${fromInfo.coinGeckoId} and ${toInfo.coinGeckoId}`);

const simulateRes: SmartRouterResponse = await UniversalSwapHelper.simulateSwapUsingSmartRoute({
fromInfo,
toInfo,
amount: toAmount(query.originalAmount, fromInfo.decimals).toString(),
routerConfig: routerConfigDefault
});

const amount = simulateRes.returnAmount;
const routes = simulateRes;
return {
amount,
displayAmount: toDisplay(amount, decimals),
routes: routes ?? {}
displayAmount: toDisplay(amount, toInfo.decimals),
routes
};
};

Expand Down
19 changes: 2 additions & 17 deletions packages/universal-swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum SwapDirection {
export interface SimulateResponse {
amount: Uint128;
displayAmount: number;
routes?: SmartRouteSwapAPIOperations;
routes?: SmartRouterResponse;
routeSwapOps?: SmartRouteSwapOperations[];
}

Expand Down Expand Up @@ -125,22 +125,7 @@ export type SmartRouteSwapOperations = {
export type SmartRouterResponse = {
swapAmount: string;
returnAmount: string;
routes: (SmartRouteSwapAPIOperations | Route)[];
};

export type SmartRouteSwapAPIOperations = {
swapAmount: string;
returnAmount: string;
paths: {
poolId: string;
tokenOut: string;
}[];
};

export type SmartRouterResponseAPI = {
swapAmount: string;
returnAmount: string;
routes: SmartRouteSwapAPIOperations[];
routes: Route[];
};

export type ConvertType = Convert | ConvertReverse;
Expand Down

0 comments on commit 764e2a1

Please sign in to comment.