Skip to content

Commit

Permalink
Merge branch 'remove/list-pairs' into feat/intergrate_common
Browse files Browse the repository at this point in the history
  • Loading branch information
trungbach committed Dec 13, 2024
2 parents 018fd97 + 453eb9f commit 4a84151
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 356 deletions.
85 changes: 0 additions & 85 deletions packages/oraidex-common/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,88 +112,3 @@ export const PAIRS: PairMapping[] = [
symbols: ["MAX", "ORAIX"]
}
];

// FIXME: makes this dynamic in the future so that permissionless listing is simpler
export enum pairLpTokens {
AIRI_ORAI = "orai1hxm433hnwthrxneyjysvhny539s9kh6s2g2n8y",
ORAIX_ORAI = "orai1qmy3uuxktflvreanaqph6yua7stjn6j65rur62",
SCORAI_ORAI = "orai1ay689ltr57jt2snujarvakxrmtuq8fhuat5rnvq6rct89vjer9gqm2vde6",
ATOM_ORAI = "orai1g2prqry343kx566cp7uws9w7v78n5tejylvaz6",
USDT_ORAI = "orai1mav52eqhd07c3lwevcnqdykdzhh4733zf32jcn",
KWT_ORAI = "orai17rcfcrwltujfvx7w4l2ggyku8qrncy0hdvrzvc",
OSMO_ORAI = "orai19ltj97jmdqnz5mrd2amethetvcwsp0220kww3e",
MILKY_USDT = "orai18ywllw03hvy720l06rme0apwyyq9plk64h9ccf",
USDC_ORAI = "orai1e0x87w9ezwq2sdmvv5dq5ngzy98lt47tqfaf2m7zpkg49g5dj6fqred5d7",
TRX_ORAI = "orai1wgywgvumt5dxhm7vjpwx5es9ecrtl85qaqdspjqwx2lugy7vmw5qlwrn88",
SCATOM_ATOM = "orai1hcjne0hmdj6pjrc3xuksucr0yplsa9ny7v047c34y8k8hfflq6yqyjapnn",
INJ_ORAI = "orai1slqw6gfvs6l2jgvh5ryjayf4g77d7sgfv6fumtyzcr06a6g9gnrq6c4rgg",
USDC_ORAIX = "orai1nwpfd09mr4rf8d5c9mh43axzezkwyr7dq2lus23jsw4xw2jqkaxqxwmkd3",
ORAI_WETH = "orai1rvr9wk6mdlfysvgp72ltthqvkkd5677mp892efq86yyr9alt0tms2a6lcs",
ORAI_BTC = "orai1jd9lc2qt0ltjsatgnu38xsz8ngp89clp0dpeh8geyjj70yvkn4kqmrmh3m",
NTMPI_USDC = "orai1rmvjmwd940ztafxue7630g75px8tqma4jskjuu57fkj0eqahqfgqqwjm00",
OCH_ORAI = "orai1xs5aj90d5m8kwfp9t6ghkcpk8d7sy5jsxdsyejjdxudhhfm7wegsdg929d",
MAX_ORAIX = "orai1gswmxchtlkav289eq86z4ehaezntctdrptcww4yvpp20d93xza0qsqjxvv"
}

// token identifier can be denom or contract addr
export const isInPairList = (tokenIdentifier: string) => {
return PAIRS.some((pair) =>
pair.asset_infos.some((info) => {
if ("native_token" in info) {
return info.native_token.denom === tokenIdentifier;
}
return info.token.contract_addr === tokenIdentifier;
})
);
};

export const isFactoryV1 = (assetInfos: [AssetInfo, AssetInfo]): boolean => {
const pair = PAIRS.find(
(pair) =>
pair.asset_infos.find((info) => parseAssetInfo(info) === parseAssetInfo(assetInfos[0])) &&
pair.asset_infos.find((info) => parseAssetInfo(info) === parseAssetInfo(assetInfos[1]))
);
if (!pair) {
return true;
}
return pair.factoryV1 ?? false;
};

export const getPoolTokens = (assetInfoMap: Record<string, TokenItemType>): TokenItemType[] => {
return uniq(flatten(PAIRS.map((pair) => pair.asset_infos)).map((info) => assetInfoMap[parseAssetInfo(info)]));
};

// TODO: remove hardcode here
export const PAIRS_CHART = PAIRS.map((pair) => {
const assets = pair.asset_infos.map((info) => {
if ("native_token" in info) return info.native_token.denom;
return info.token.contract_addr;
});

return {
...pair,
symbol: `${pair.symbols[0]}/${pair.symbols[1]}`,
info: `${assets[0]}-${assets[1]}`
};
});

export enum PairAddress {
AIRI_ORAI = "orai1wkhkazf88upf2dxqedggy3ldja342rzmfs2mep",
ORAIX_ORAI = "orai1m6q5k5nr2eh8q0rdrf57wr7phk7uvlpg7mwfv5",
ATOM_ORAI = "orai1jf74ry4m0jcy9emsaudkhe7vte9l8qy8enakvs",
USDT_ORAI = "orai1c5s03c3l336dgesne7dylnmhszw8554tsyy9yt",
KWT_ORAI = "orai1ynmd2cemryhcwtjq3adhcwayrm89l2cr4tws4v",
OSMO_ORAI = "orai1d37artrk4tkhz2qyjmaulc2jzjkx7206tmpfug",
MILKY_USDT = "orai1hr2l03ep6p9lwdkuqu5253fgpzc40xcpwymjfc",
SCORAI_ORAI = "orai15aunrryk5yqsrgy0tvzpj7pupu62s0t2n09t0dscjgzaa27e44esefzgf8",
USDC_ORAI = "orai19ttg0j7w5kr83js32tmwnwxxdq9rkmw4m3d7mn2j2hkpugwwa4tszwsnkg",
TRX_ORAI = "orai103ya8qkcf3vg4nksqquy0v5pvnugjtlt0uxpfh0fkuqge2a6k4aqwurg22",
SCATOM_ATOM = "orai16ltg2c8u9styus3dgql64mpupvtclxt9xdzvz0slx3pnrycxpm3qw75c5x",
INJ_ORAI = "orai1le7w5dmd23ky8f6zgtgfnpdv269qs6ezgr839sm8kj24rwaqqnrs58wf4u",
USDC_ORAIX = "orai1n4edv5h86rawzrvhy8lmrmnnmmherxnhuwqnk3yuvt0wgclh75usyn3md6",
ORAI_WETH = "orai10jgd0l4l0p2h7ugpk2lz64wpefjxc0h7evnlxf76a3fspdplarnsl9ma4j",
ORAI_BTC = "orai1fv5kwdv4z0gvp75ht378x8cg2j7prlywa0g35qmctez9q8u4xryspn6lrd",
NTMPI_USDC = "orai1yemx80gvcw05trjehy94rl4jz5dqjf2qxhks6258uvxd5s0m7h2quavx0g",
OCH_ORAI = "orai1d3f3e3j400hxse5z8vxxnxdwmvljs7mh8xa3wp3spe8g4ngnc3cqx8scs3",
MAX_ORAIX = "orai1s3746evfgwm7dtl3x4s7fmd37c4s8t566z0xvflr4shdculkxw5qa3pusc"
}
19 changes: 1 addition & 18 deletions packages/oraidex-common/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import {
validateAddressTonTron,
parseAssetInfoFromContractAddrOrDenom
} from "../src/helper";
import { CoinGeckoId } from "../src/network";
import { isFactoryV1 } from "../src/pairs";
import { AmountDetails } from "../src/format-types";
import { CoinGeckoId } from "../src/network";
const __filename = fileURLToPath(import.meta.url);
console.log("__filename: ", __filename);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -133,22 +132,6 @@ describe("should helper functions in helper run exactly", () => {
expect(parseAssetInfo(assetInfo)).toEqual(expectedResult);
});

it("test-isFactoryV1-true", () => {
const data = isFactoryV1([
{ native_token: { denom: ORAI } },
{ token: { contract_addr: "orai10ldgzued6zjp0mkqwsv2mux3ml50l97c74x8sg" } }
]);
expect(data).toEqual(true);
});

it("test-isFactoryV1-false", () => {
const data = isFactoryV1([
{ native_token: { denom: ORAI } },
{ token: { contract_addr: "orai15un8msx3n5zf9ahlxmfeqd2kwa5wm0nrpxer304m9nd5q6qq0g6sku5pdd" } }
]);
expect(data).toEqual(false);
});

it.each([
["1000000", "1000000", 1, 6, "990000"],
["1800000", "100000", 1, 6, "178200"],
Expand Down
27 changes: 0 additions & 27 deletions packages/oraidex-common/tests/pairs.spec.ts

This file was deleted.

106 changes: 37 additions & 69 deletions packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
IUniswapV2Router02__factory,
// cosmosTokens,
StargateMsg,
isInPairList,
BigDecimal,
NEUTARO_INFO,
USDC_INFO,
Expand Down Expand Up @@ -757,41 +756,6 @@ export class UniversalSwapHelper {
};
};

/**
* @deprecated. Use UniversalSwapHelper.handleSimulateSwap
*/
static simulateSwap = async (query: {
fromInfo: TokenItemType;
toInfo: TokenItemType;
amount: string;
routerClient: OraiswapRouterReadOnlyInterface;
}): Promise<{ amount: Uint128 }> => {
const { amount, fromInfo, toInfo, routerClient } = 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) {
return {
amount
};
}

// check if they have pairs. If not then we go through ORAI
const { info: offerInfo } = parseTokenInfo(fromInfo, amount);
const { info: askInfo } = parseTokenInfo(toInfo);
const operations = UniversalSwapHelper.generateSwapOperationMsgs(offerInfo, askInfo);
console.log("operations: ", operations);
try {
let finalAmount = amount;
const data = await routerClient.simulateSwapOperations({
offerAmount: finalAmount,
operations
});
return data;
} catch (error) {
throw new Error(`Error when trying to simulate swap using router v2: ${JSON.stringify(error)}`);
}
};

// simulate swap functions
static simulateSwapUsingSmartRoute = async (query: {
fromInfo: TokenItemType;
Expand Down Expand Up @@ -885,6 +849,23 @@ export class UniversalSwapHelper {
}
};

static getRouterConfig = (options: {
path?: string;
protocols?: string[];
dontAllowSwapAfter?: string[];
maxSplits?: number;
ignoreFee?: boolean;
}) => {
return {
url: "https://osor.oraidex.io",
path: options?.path ?? "/smart-router/alpha-router",
protocols: options?.protocols ?? ["Oraidex", "OraidexV3"],
dontAllowSwapAfter: options?.dontAllowSwapAfter ?? ["Oraidex", "OraidexV3"],
maxSplits: options?.maxSplits ?? 1,
ignoreFee: options?.ignoreFee ?? false
};
};

static handleSimulateSwap = async (query: {
flattenTokens: TokenItemType[];
oraichainTokens: TokenItemType[];
Expand Down Expand Up @@ -920,15 +901,7 @@ export class UniversalSwapHelper {
return { amount, displayAmount };
}

const routerConfigDefault = {
url: query?.routerConfig?.url ?? "https://osor.oraidex.io",
path: query?.routerConfig?.path ?? "/smart-router/alpha-router",
protocols: query?.routerConfig?.protocols ?? ["Oraidex", "OraidexV3"],
dontAllowSwapAfter: query?.routerConfig?.dontAllowSwapAfter ?? ["Oraidex", "OraidexV3"],
maxSplits: query?.routerConfig?.maxSplits ?? 10,
ignoreFee: query?.routerConfig?.ignoreFee ?? false
};

const routerConfigDefault = UniversalSwapHelper.getRouterConfig({});
let fromInfo = getTokenOnOraichain(query.originalFromInfo.coinGeckoId, query.oraichainTokens);
let toInfo = getTokenOnOraichain(query.originalToInfo.coinGeckoId, query.oraichainTokens);

Expand Down Expand Up @@ -996,26 +969,25 @@ export class UniversalSwapHelper {
relayerAmount: string;
routerClient: OraiswapRouterReadOnlyInterface;
}): Promise<boolean> => {
const { fromTokenInOrai, fromAmount, routerClient, relayerAmount } = query;
const { fromTokenInOrai, fromAmount, routerClient, relayerAmount, oraichainTokens } = query;
if (!fromTokenInOrai) return true;
if (fromTokenInOrai.chainId !== "Oraichain")
throw generateError(
"From token on Oraichain is not on Oraichain. The developers have made a mistake. Please notify them!"
);
// estimate exchange token when From Token not orai. Only need to swap & check if it is swappable with ORAI. Otherwise, we ignore the fees
if (isInPairList(fromTokenInOrai.denom) || isInPairList(fromTokenInOrai.contractAddress)) {
const oraiToken = getTokenOnOraichain("oraichain-token", query.oraichainTokens);
const { amount } = await UniversalSwapHelper.simulateSwap({
fromInfo: fromTokenInOrai,
toInfo: oraiToken,
amount: toAmount(fromAmount, fromTokenInOrai.decimals).toString(),
routerClient: routerClient
});
const amountDisplay = toDisplay(amount, fromTokenInOrai.decimals);
const relayerAmountDisplay = toDisplay(relayerAmount);
if (relayerAmountDisplay > amountDisplay) return false;
return true;
}
const oraiToken = getTokenOnOraichain("oraichain-token", oraichainTokens);
const { amount } = await UniversalSwapHelper.handleSimulateSwap({
flattenTokens: [],
oraichainTokens,
originalFromInfo: fromTokenInOrai,
originalToInfo: oraiToken,
originalAmount: fromAmount,
routerClient: routerClient
});
const amountDisplay = toDisplay(amount, fromTokenInOrai.decimals);
const relayerAmountDisplay = toDisplay(relayerAmount);
if (relayerAmountDisplay > amountDisplay) return false;
return true;
};

Expand Down Expand Up @@ -1067,10 +1039,12 @@ export class UniversalSwapHelper {
throw generateError(
`Error in checking balance channel ibc: cannot simulate from: ${fromToken.coinGeckoId} to: ${toToken.coinGeckoId}`
);
const { amount } = await UniversalSwapHelper.simulateSwap({
fromInfo: fromTokenInfo,
toInfo: toTokenInfo,
amount: toAmount(_toAmount, fromTokenInfo.decimals).toString(),
const { amount } = await UniversalSwapHelper.handleSimulateSwap({
flattenTokens: [],
oraichainTokens,
originalFromInfo: fromTokenInfo,
originalToInfo: toTokenInfo,
originalAmount: _toAmount,
routerClient
});
_toAmount = toDisplay(amount, fromTokenInfo.decimals);
Expand Down Expand Up @@ -1606,12 +1580,6 @@ export const generateSwapRoute = UniversalSwapHelper.generateSwapRoute;
*/
export const generateSwapOperationMsgs = UniversalSwapHelper.generateSwapOperationMsgs;

// simulate swap functions
/**
* @deprecated
*/
export const simulateSwap = UniversalSwapHelper.simulateSwap;

/**
* @deprecated
*/
Expand Down
Loading

0 comments on commit 4a84151

Please sign in to comment.