Skip to content

Commit

Permalink
fix universal swap oraichain to inj injective (#265)
Browse files Browse the repository at this point in the history
* fix universal swap oraichain to inj injective

* add params url smart router
  • Loading branch information
haunv3 authored May 14, 2024
1 parent 855463a commit d0df910
Show file tree
Hide file tree
Showing 3 changed files with 19 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.78",
"version": "1.0.80",
"main": "build/index.js",
"files": [
"build/"
Expand Down
5 changes: 3 additions & 2 deletions packages/universal-swap/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ export class UniversalSwapHandler {
const isNotMatchCoingeckoId = fromCoinGeckoId !== toCoinGeckoId;
let getEncodedExecuteMsgs = [];
if (isSpecialChain) {
// 1. = coingeckoId => convert + bridge
if (fromCoinGeckoId === toCoinGeckoId && isSpecialCoingecko) {
// convert token
if (isSpecialCoingecko) {
const evmToken = tokenMap[toTokenInOrai.denom];
const evmAmount = coin(toAmount(this.swapData.fromAmount, evmToken.decimals).toString(), evmToken.denom);
const msgConvertReverses = UniversalSwapHelper.generateConvertCw20Erc20Message(
Expand All @@ -167,6 +167,7 @@ export class UniversalSwapHandler {
getEncodedExecuteMsgs = getEncodedExecuteContractMsgs(sender, executeContractMsgs);
}

// 1. = coingeckoId => convert + bridge
// 2. != coingeckoId => swap + convert + bridge
// if not same coingeckoId, swap first then transfer token that have same coingeckoid.
if (isNotMatchCoingeckoId) {
Expand Down
23 changes: 15 additions & 8 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,11 @@ export class UniversalSwapHelper {
offerChainId: string,
askInfo: AssetInfo,
askChainId: string,
offerAmount: string
offerAmount: string,
urlRouter?: string
): Promise<SmartRouterResponseAPI> => {
const urlRouter = "https://router.oraidex.io";
const { axios } = await getAxios(urlRouter);
const urlSmartRouter = urlRouter ?? "https://osor.oraidex.io";
const { axios } = await getAxios(urlSmartRouter);
const data = {
sourceAsset: parseAssetInfo(offerInfo),
sourceChainId: offerChainId,
Expand All @@ -469,14 +470,16 @@ export class UniversalSwapHelper {
offerChainId: string,
askInfo: AssetInfo,
askChainId: string,
offerAmount: string
offerAmount: string,
urlRouter?: string
): Promise<SmartRouterResponse> => {
const { returnAmount, routes: routesSwap } = await UniversalSwapHelper.querySmartRoute(
offerInfo,
offerChainId,
askInfo,
askChainId,
offerAmount
offerAmount,
urlRouter
);

const routes = routesSwap.map((route) => {
Expand Down Expand Up @@ -546,8 +549,9 @@ export class UniversalSwapHelper {
fromInfo: TokenItemType;
toInfo: TokenItemType;
amount: string;
urlRouter?: string;
}): Promise<SmartRouterResponse> => {
const { amount, fromInfo, toInfo } = query;
const { amount, fromInfo, toInfo, urlRouter } = 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 @@ -567,7 +571,8 @@ export class UniversalSwapHelper {
fromInfo.chainId,
askInfo,
toInfo.chainId,
amount
amount,
urlRouter
);
} catch (error) {
throw new Error(`Error when trying to simulate swap using smart router: ${JSON.stringify(error)}`);
Expand Down Expand Up @@ -627,6 +632,7 @@ export class UniversalSwapHelper {
originalAmount: number;
routerClient: OraiswapRouterReadOnlyInterface;
useSmartRoute?: boolean;
urlRouter?: string;
}): Promise<SimulateResponse> => {
// if the from token info is on bsc or eth, then we simulate using uniswap / pancake router
// otherwise, simulate like normal
Expand Down Expand Up @@ -661,7 +667,8 @@ export class UniversalSwapHelper {
const simulateRes: SmartRouterResponse = await UniversalSwapHelper.simulateSwapUsingSmartRoute({
fromInfo,
toInfo,
amount: toAmount(query.originalAmount, fromInfo.decimals).toString()
amount: toAmount(query.originalAmount, fromInfo.decimals).toString(),
urlRouter: query.urlRouter
});
routes = simulateRes?.routesSwap;
amount = simulateRes.returnAmount;
Expand Down

0 comments on commit d0df910

Please sign in to comment.