Skip to content

Commit

Permalink
Merge pull request #268 from oraichain/feat/smart-router-osmosis-pool
Browse files Browse the repository at this point in the history
pumb version universal swap 1.0.82
  • Loading branch information
haunv3 authored May 22, 2024
2 parents 3e4b938 + 3e39bbd commit 6675d6c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/universal-swap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-universal-swap",
"version": "1.0.81",
"version": "1.0.82",
"main": "build/index.js",
"files": [
"build/"
Expand Down
2 changes: 1 addition & 1 deletion packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class UniversalSwapHandler {
return client.signAndBroadcast(this.swapData.sender.cosmos, [...msgExecuteSwap, ...transferStringifyMemo], "auto");
}

stringifyMemos = (obj) => {
private stringifyMemos = (obj) => {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === "object" && obj[key] !== null) {
Expand Down
28 changes: 22 additions & 6 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ export class UniversalSwapHelper {
urlRouter: {
url: string;
path?: string;
}
},
useAlphaSmartRoute?: boolean
): Promise<SmartRouterResponse> => {
const { returnAmount, routes: routesSwap } = await UniversalSwapHelper.querySmartRoute(
offerInfo,
Expand All @@ -489,6 +490,15 @@ export class UniversalSwapHelper {
urlRouter
);

if (useAlphaSmartRoute) {
return {
swapAmount: offerAmount,
returnAmount,
routes: [],
routesSwap
};
}

const routes = routesSwap.map((route) => {
let ops = [];
let currTokenIn = offerInfo;
Expand Down Expand Up @@ -560,8 +570,9 @@ export class UniversalSwapHelper {
url: string;
path?: string;
};
useAlphaSmartRoute?: boolean;
}): Promise<SmartRouterResponse> => {
const { amount, fromInfo, toInfo, urlRouter } = query;
const { amount, fromInfo, toInfo, urlRouter, useAlphaSmartRoute } = query;

// check for universal-swap 2 tokens that have same coingeckoId, should return simulate data with average ratio 1-1.
if (fromInfo.coinGeckoId === toInfo.coinGeckoId) {
Expand All @@ -582,7 +593,8 @@ export class UniversalSwapHelper {
askInfo,
toInfo.chainId,
amount,
urlRouter
urlRouter,
useAlphaSmartRoute
);
} catch (error) {
throw new Error(`Error when trying to simulate swap using smart router: ${JSON.stringify(error)}`);
Expand Down Expand Up @@ -641,7 +653,10 @@ export class UniversalSwapHelper {
originalToInfo: TokenItemType;
originalAmount: number;
routerClient: OraiswapRouterReadOnlyInterface;
useSmartRoute?: boolean;
routerOption?: {
useSmartRoute?: boolean;
useAlphaSmartRoute?: boolean;
};
urlRouter?: {
url: string;
path?: string;
Expand Down Expand Up @@ -676,12 +691,13 @@ export class UniversalSwapHelper {
let amount;
let routes = [];
let routeSwapOps;
if (query.useSmartRoute) {
if (query.routerOption.useSmartRoute || query.routerOption.useAlphaSmartRoute) {
const simulateRes: SmartRouterResponse = await UniversalSwapHelper.simulateSwapUsingSmartRoute({
fromInfo,
toInfo,
amount: toAmount(query.originalAmount, fromInfo.decimals).toString(),
urlRouter: query.urlRouter
urlRouter: query.urlRouter,
useAlphaSmartRoute: query?.routerOption?.useAlphaSmartRoute
});
routes = simulateRes?.routesSwap;
amount = simulateRes.returnAmount;
Expand Down
46 changes: 44 additions & 2 deletions packages/universal-swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface UniversalSwapData {
readonly amounts?: AmountDetails;
readonly recipientAddress?: string; // recipient address from client, if user want to send to another address
readonly smartRoutes?: SmartRouteSwapOperations[];
readonly alphaSmartRoutes?: any;
readonly alphaSmartRoutes?: Router;
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ export type ConvertReverse = {
export type SmartRouteSwapOperations = {
swapAmount: string;
returnAmount: string;
swapOps: SwapOperation[] | any[];
swapOps: SwapOperation[];
};

export type SmartRouterResponse = {
Expand Down Expand Up @@ -199,3 +199,45 @@ export interface Forward {
retries: number;
next: NextWasm;
}

interface Router {
swapAmount: string;
returnAmount: string;
routes: Route[];
}

interface Route {
swapAmount: string;
returnAmount: string;
paths: Path[];
}

interface Path {
chainId: string;
tokenIn: string;
tokenInAmount: string;
tokenOut: string;
tokenOutAmount: string;
tokenOutChainId: string;
actions: Action[];
}

interface Action {
type: string;
tokenIn: string;
tokenInAmount: string;
tokenOut: string;
tokenOutAmount: string;
swapInfo?: SwapInfo[];
bridgeInfo?: BridgeInfo;
}

interface SwapInfo {
poolId: string;
tokenOut: string;
}

interface BridgeInfo {
port: string;
channel: string;
}
4 changes: 3 additions & 1 deletion packages/universal-swap/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,9 @@ describe("test universal swap handler functions", () => {
originalToInfo: oraichainTokens[1],
originalAmount: 0,
routerClient: new OraiswapRouterQueryClient(client, ""),
useSmartRoute
routerOption: {
useSmartRoute
}
});
expect(simulateData.amount).toEqual(expectedSimulateAmount);
}
Expand Down

0 comments on commit 6675d6c

Please sign in to comment.