Skip to content

Commit

Permalink
Merge pull request #87 from oraichain/fix/circular-deps-dex-common
Browse files Browse the repository at this point in the history
fix: remove circular deps from oraidex common
  • Loading branch information
ducphamle2 authored Dec 8, 2023
2 parents 0ec8c8a + 209f746 commit 4940fb4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 198 deletions.
105 changes: 0 additions & 105 deletions packages/oraidex-common/src/config/bridgeTokens.ts

This file was deleted.

31 changes: 0 additions & 31 deletions packages/oraidex-common/src/config/networks.ts

This file was deleted.

52 changes: 2 additions & 50 deletions packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ import {
cosmosTokens,
flattenTokens,
oraichainTokens,
CoinGeckoPrices
CoinGeckoPrices,
tokenMap
} from "./token";
import { StargateMsg, Tx } from "./tx";
import { BigDecimal } from "./bigdecimal";
import {
isEvmNetworkNativeSwapSupported,
isSupportedNoPoolSwapEvm,
SwapDirection,
swapEvmRoutes
} from "@oraichain/oraidex-universal-swap";
import { swapFromTokens, swapToTokens, tokenMap } from "./config/bridgeTokens";

export const getEvmAddress = (bech32Address: string) => {
if (!bech32Address) throw new Error("bech32 address is empty");
Expand Down Expand Up @@ -331,48 +325,6 @@ export const getSwapType = ({

return "Universal Swap";
};
export function filterNonPoolEvmTokens(
chainId: string,
coingeckoId: CoinGeckoId,
denom: string,
searchTokenName: string,
direction: SwapDirection // direction = to means we are filtering to tokens
) {
// basic filter. Dont include itself & only collect tokens with searched letters
const listTokens = direction === SwapDirection.From ? swapFromTokens : swapToTokens;
let filteredToTokens = listTokens.filter(
(token) => token.denom !== denom && token.name.toLowerCase().includes(searchTokenName.toLowerCase())
);
// special case for tokens not having a pool on Oraichain
if (isSupportedNoPoolSwapEvm(coingeckoId)) {
const swappableTokens = Object.keys(swapEvmRoutes[chainId]).map((key) => key.split("-")[1]);
const filteredTokens = filteredToTokens.filter((token) => swappableTokens.includes(token.contractAddress));

// tokens that dont have a pool on Oraichain like WETH or WBNB cannot be swapped from a token on Oraichain
if (direction === SwapDirection.To)
return [...new Set(filteredTokens.concat(filteredTokens.map((token) => getTokenOnOraichain(token.coinGeckoId))))];
filteredToTokens = filteredTokens;
}
// special case filter. Tokens on networks other than supported evm cannot swap to tokens, so we need to remove them
if (!isEvmNetworkNativeSwapSupported(chainId as NetworkChainId))
return filteredToTokens.filter((t) => {
// one-directional swap. non-pool tokens of evm network can swap be swapped with tokens on Oraichain, but not vice versa
const isSupported = isSupportedNoPoolSwapEvm(t.coinGeckoId);
if (direction === SwapDirection.To) return !isSupported;
if (isSupported) {
// if we cannot find any matched token then we dont include it in the list since it cannot be swapped
const sameChainId = getTokenOnSpecificChainId(coingeckoId, t.chainId as NetworkChainId);
if (!sameChainId) return false;
return true;
}
return true;
});
return filteredToTokens.filter((t) => {
// filter out to tokens that are on a different network & with no pool because we are not ready to support them yet. TODO: support
if (isSupportedNoPoolSwapEvm(t.coinGeckoId)) return t.chainId === chainId;
return true;
});
}

export const feeEstimate = (tokenInfo: TokenItemType, gasDefault: number) => {
if (!tokenInfo) return 0;
Expand Down
3 changes: 1 addition & 2 deletions packages/oraidex-common/src/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import {
USDT_CONTRACT
} from "./constant";
import { parseAssetInfo } from "./helper";
import { TokenItemType } from "./token";
import { TokenItemType, assetInfoMap } from "./token";
import uniq from "lodash/uniq";
import flatten from "lodash/flatten";
import { assetInfoMap } from "./config/bridgeTokens";

export type PairMapping = {
asset_infos: [AssetInfo, AssetInfo];
Expand Down
26 changes: 26 additions & 0 deletions packages/oraidex-common/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,29 @@ export const cosmosTokens = uniqBy(
),
(c) => c.denom
);

export const cw20Tokens = uniqBy(
cosmosTokens.filter(
// filter cosmos based tokens to collect tokens that have contract addresses
(token) =>
// !token.contractAddress &&
token.contractAddress
),
(c) => c.denom
);

export const cw20TokenMap = Object.fromEntries(cw20Tokens.map((c) => [c.contractAddress, c]));

export const evmTokens = uniqBy(
flattenTokens.filter(
(token) =>
// !token.contractAddress &&
token.denom && !token.cosmosBased && token.coinGeckoId && token.chainId !== "kawaii_6886-1"
),
(c) => c.denom
);

export const kawaiiTokens = uniqBy(
cosmosTokens.filter((token) => token.chainId === "kawaii_6886-1"),
(c) => c.denom
);
46 changes: 45 additions & 1 deletion packages/universal-swap/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
network,
isInPairList
} from "@oraichain/oraidex-common";
import { OraiBridgeRouteData, SimulateResponse, SwapRoute, UniversalSwapConfig } from "./types";
import { OraiBridgeRouteData, SimulateResponse, SwapDirection, SwapRoute, UniversalSwapConfig } from "./types";
import {
AssetInfo,
OraiswapRouterClient,
Expand All @@ -48,6 +48,7 @@ import { isEqual } from "lodash";
import { ethers } from "ethers";
import { Amount, CwIcs20LatestQueryClient, CwIcs20LatestReadOnlyInterface } from "@oraichain/common-contracts-sdk";
import { CosmWasmClient, toBinary } from "@cosmjs/cosmwasm-stargate";
import { swapFromTokens, swapToTokens } from "./swap-filter";

// evm swap helpers
export const isSupportedNoPoolSwapEvm = (coingeckoId: CoinGeckoId) => {
Expand Down Expand Up @@ -597,3 +598,46 @@ export const buildIbcWasmHooksMemo = (stargateMsgs: StargateMsg[]): string => {
}
});
};

export function filterNonPoolEvmTokens(
chainId: string,
coingeckoId: CoinGeckoId,
denom: string,
searchTokenName: string,
direction: SwapDirection // direction = to means we are filtering to tokens
) {
// basic filter. Dont include itself & only collect tokens with searched letters
const listTokens = direction === SwapDirection.From ? swapFromTokens : swapToTokens;
let filteredToTokens = listTokens.filter(
(token) => token.denom !== denom && token.name.toLowerCase().includes(searchTokenName.toLowerCase())
);
// special case for tokens not having a pool on Oraichain
if (isSupportedNoPoolSwapEvm(coingeckoId)) {
const swappableTokens = Object.keys(swapEvmRoutes[chainId]).map((key) => key.split("-")[1]);
const filteredTokens = filteredToTokens.filter((token) => swappableTokens.includes(token.contractAddress));

// tokens that dont have a pool on Oraichain like WETH or WBNB cannot be swapped from a token on Oraichain
if (direction === SwapDirection.To)
return [...new Set(filteredTokens.concat(filteredTokens.map((token) => getTokenOnOraichain(token.coinGeckoId))))];
filteredToTokens = filteredTokens;
}
// special case filter. Tokens on networks other than supported evm cannot swap to tokens, so we need to remove them
if (!isEvmNetworkNativeSwapSupported(chainId as NetworkChainId))
return filteredToTokens.filter((t) => {
// one-directional swap. non-pool tokens of evm network can swap be swapped with tokens on Oraichain, but not vice versa
const isSupported = isSupportedNoPoolSwapEvm(t.coinGeckoId);
if (direction === SwapDirection.To) return !isSupported;
if (isSupported) {
// if we cannot find any matched token then we dont include it in the list since it cannot be swapped
const sameChainId = getTokenOnSpecificChainId(coingeckoId, t.chainId as NetworkChainId);
if (!sameChainId) return false;
return true;
}
return true;
});
return filteredToTokens.filter((t) => {
// filter out to tokens that are on a different network & with no pool because we are not ready to support them yet. TODO: support
if (isSupportedNoPoolSwapEvm(t.coinGeckoId)) return t.chainId === chainId;
return true;
});
}
20 changes: 20 additions & 0 deletions packages/universal-swap/src/swap-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { flattenTokens } from "@oraichain/oraidex-common";

const notAllowSwapCoingeckoIds = ["kawaii-islands", "milky-token", "injective-protocol"];
// universal swap. Currently we dont support from tokens that are not using the ibc wasm channel
const notAllowSwapFromChainIds = [
"kawaii_6886-1",
"osmosis-1",
"cosmoshub-4",
"oraibridge-subnet-2",
"injective-1",
"noble-1"
];
export const swapFromTokens = flattenTokens.filter(
(token) => !notAllowSwapCoingeckoIds.includes(token.coinGeckoId) && !notAllowSwapFromChainIds.includes(token.chainId)
);
// universal swap. We dont support kwt & milky & injective for simplicity. We also skip OraiBridge tokens because users dont care about them
const notAllowSwapToChainIds = ["oraibridge-subnet-2", "injective-1", "noble-1"];
export const swapToTokens = flattenTokens.filter(
(token) => !notAllowSwapCoingeckoIds.includes(token.coinGeckoId) && !notAllowSwapToChainIds.includes(token.chainId)
);
9 changes: 0 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3298,15 +3298,6 @@
resolved "https://registry.yarnpkg.com/@oraichain/immutable/-/immutable-4.3.9.tgz#ff8d5a7b39b5b01f3f72a902cffbfea32ccb20c3"
integrity sha512-INpHnhL970OCkR7I71Kssb2aLl2l4Y/x8W6FlyRO0KmC8GHjxc/hlNB1t44BiI7lkOYmcWMRQoC8dwParsp1RQ==

"@oraichain/oraidex-common@^1.0.43":
version "1.0.43"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-1.0.43.tgz#3ce40dfd1cc33c129253fd59c8e227c05837c7fa"
integrity sha512-Xtp00JnVUl1Z4QarJE+Gw02TrHcxUmXzuynGN9g/x7HVL5NymzbB//PShlJ2VFhJfZXHCBaSBqDniIBLPoQ+GQ==
dependencies:
"@ethersproject/providers" "^5.0.10"
"@keplr-wallet/types" "^0.11.38"
ethers "^5.0.15"

"@oraichain/[email protected]":
version "1.0.14"
resolved "https://registry.yarnpkg.com/@oraichain/oraidex-universal-swap/-/oraidex-universal-swap-1.0.14.tgz#fd4b60e7cfc21984dbaa14a7b9a2c560f02da37d"
Expand Down

0 comments on commit 4940fb4

Please sign in to comment.