From 209f74668770dc142324faac2927a6cfe883c21f Mon Sep 17 00:00:00 2001 From: ducphamle2 Date: Thu, 7 Dec 2023 18:09:29 -0800 Subject: [PATCH] fix: remove circular deps from oraidex common --- .../oraidex-common/src/config/bridgeTokens.ts | 105 ------------------ .../oraidex-common/src/config/networks.ts | 31 ------ packages/oraidex-common/src/helper.ts | 52 +-------- packages/oraidex-common/src/pairs.ts | 3 +- packages/oraidex-common/src/token.ts | 26 +++++ packages/universal-swap/src/helper.ts | 46 +++++++- packages/universal-swap/src/swap-filter.ts | 20 ++++ yarn.lock | 9 -- 8 files changed, 94 insertions(+), 198 deletions(-) delete mode 100644 packages/oraidex-common/src/config/bridgeTokens.ts delete mode 100644 packages/oraidex-common/src/config/networks.ts create mode 100644 packages/universal-swap/src/swap-filter.ts diff --git a/packages/oraidex-common/src/config/bridgeTokens.ts b/packages/oraidex-common/src/config/bridgeTokens.ts deleted file mode 100644 index 7b8f2aa5..00000000 --- a/packages/oraidex-common/src/config/bridgeTokens.ts +++ /dev/null @@ -1,105 +0,0 @@ -import flatten from "lodash/flatten"; -import uniqBy from "lodash/uniqBy"; -import { chainInfos, oraichainNetwork } from "./chainInfos"; -import { INJECTIVE_ORAICHAIN_DENOM, KWTBSC_ORAICHAIN_DENOM, MILKYBSC_ORAICHAIN_DENOM } from "../constant"; -import { TokenItemType } from "../token"; -import { CustomChainInfo } from "../network"; - -const evmDenomsMap = { - kwt: [KWTBSC_ORAICHAIN_DENOM], - milky: [MILKYBSC_ORAICHAIN_DENOM], - injective: [INJECTIVE_ORAICHAIN_DENOM] -}; -const minAmountSwapMap = { - trx: 10 -}; - -export const getTokensFromNetwork = (network: CustomChainInfo): TokenItemType[] => { - return network.currencies.map((currency) => ({ - name: currency.coinDenom, - org: network.chainName, - coinType: network.bip44.coinType, - contractAddress: currency.contractAddress, - prefix: currency?.prefixToken ?? network.bech32Config?.bech32PrefixAccAddr, - coinGeckoId: currency.coinGeckoId, - denom: currency.coinMinimalDenom, - bridgeNetworkIdentifier: currency.bridgeNetworkIdentifier, - decimals: currency.coinDecimals, - bridgeTo: currency.bridgeTo, - chainId: network.chainId, - rpc: network.rpc, - lcd: network.rest, - cosmosBased: network.networkType === "cosmos", - maxGas: (network.feeCurrencies?.[0].gasPriceStep?.high ?? 0) * 20000, - gasPriceStep: currency.gasPriceStep, - feeCurrencies: network.feeCurrencies, - minAmountSwap: minAmountSwapMap[currency.coinMinimalDenom], - evmDenoms: evmDenomsMap[currency.coinMinimalDenom], - Icon: currency.Icon, - IconLight: currency?.IconLight - })); -}; - -// other chains, oraichain -const otherChainTokens = flatten( - chainInfos.filter((chainInfo) => chainInfo.chainId !== "Oraichain").map(getTokensFromNetwork) -); -export const oraichainTokens: TokenItemType[] = getTokensFromNetwork(oraichainNetwork); - -export const tokens = [otherChainTokens, oraichainTokens]; -export const flattenTokens = flatten(tokens); -export const tokenMap = Object.fromEntries(flattenTokens.map((c) => [c.denom, c])); -export const assetInfoMap = Object.fromEntries(flattenTokens.map((c) => [c.contractAddress || c.denom, c])); -export const cosmosTokens = uniqBy( - flattenTokens.filter( - (token) => - // !token.contractAddress && - token.denom && token.cosmosBased && token.coinGeckoId - ), - (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 -); - -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) -); diff --git a/packages/oraidex-common/src/config/networks.ts b/packages/oraidex-common/src/config/networks.ts deleted file mode 100644 index 7c063c41..00000000 --- a/packages/oraidex-common/src/config/networks.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { CustomChainInfo, NetworkConfig } from "../network"; -import { oraichainNetwork } from "./chainInfos"; -import { - CONVERTER_CONTRACT, - FACTORY_CONTRACT, - FACTORY_V2_CONTRACT, - MULTICALL_CONTRACT, - ORACLE_CONTRACT, - ORAIDEX_LISTING_CONTRACT, - REWARDER_CONTRACT, - ROUTER_V2_CONTRACT, - STAKING_CONTRACT -} from "../constant"; - -export const network: CustomChainInfo & NetworkConfig = { - ...oraichainNetwork, - prefix: oraichainNetwork.bech32Config.bech32PrefixAccAddr, - denom: "orai", - coinType: oraichainNetwork.bip44.coinType, - fee: { gasPrice: "0.00506", amount: "1518", gas: "2000000" }, // 0.000500 ORAI - factory: FACTORY_CONTRACT, - factory_v2: FACTORY_V2_CONTRACT, - router: ROUTER_V2_CONTRACT, - oracle: ORACLE_CONTRACT, - staking: STAKING_CONTRACT, - rewarder: REWARDER_CONTRACT, - converter: CONVERTER_CONTRACT, - oraidex_listing: ORAIDEX_LISTING_CONTRACT, - multicall: MULTICALL_CONTRACT, - explorer: "https://scan.orai.io" -}; diff --git a/packages/oraidex-common/src/helper.ts b/packages/oraidex-common/src/helper.ts index b9b8c0c4..afd0ddd3 100644 --- a/packages/oraidex-common/src/helper.ts +++ b/packages/oraidex-common/src/helper.ts @@ -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"); @@ -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; diff --git a/packages/oraidex-common/src/pairs.ts b/packages/oraidex-common/src/pairs.ts index 96388204..442bbc3a 100644 --- a/packages/oraidex-common/src/pairs.ts +++ b/packages/oraidex-common/src/pairs.ts @@ -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]; diff --git a/packages/oraidex-common/src/token.ts b/packages/oraidex-common/src/token.ts index 9aa773c9..703ccdfd 100644 --- a/packages/oraidex-common/src/token.ts +++ b/packages/oraidex-common/src/token.ts @@ -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 +); diff --git a/packages/universal-swap/src/helper.ts b/packages/universal-swap/src/helper.ts index 7a449220..f2d24118 100644 --- a/packages/universal-swap/src/helper.ts +++ b/packages/universal-swap/src/helper.ts @@ -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, @@ -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) => { @@ -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; + }); +} diff --git a/packages/universal-swap/src/swap-filter.ts b/packages/universal-swap/src/swap-filter.ts new file mode 100644 index 00000000..614b2eb9 --- /dev/null +++ b/packages/universal-swap/src/swap-filter.ts @@ -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) +); diff --git a/yarn.lock b/yarn.lock index 0d3cdc82..428b329e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/oraidex-universal-swap@1.0.14": version "1.0.14" resolved "https://registry.yarnpkg.com/@oraichain/oraidex-universal-swap/-/oraidex-universal-swap-1.0.14.tgz#fd4b60e7cfc21984dbaa14a7b9a2c560f02da37d"