diff --git a/packages/oraidex-common/src/config/chainInfosWithIcon.ts b/packages/oraidex-common/src/config/chainInfosWithIcon.ts index e8d56bcb..c25b5745 100644 --- a/packages/oraidex-common/src/config/chainInfosWithIcon.ts +++ b/packages/oraidex-common/src/config/chainInfosWithIcon.ts @@ -1,4 +1,3 @@ -import { CustomChainInfo, chainInfos as customChainInfos } from "../network"; import { TokenItemType } from "../token"; import { AiriIcon, @@ -225,4 +224,4 @@ export const mapListWithIcon = (list: any[], listIcon: ChainIcon[] | TokenIcon[] }; // mapped chain info with icon -export const chainInfosWithIcon = mapListWithIcon(customChainInfos, chainIcons, "chainId"); +// export const chainInfosWithIcon = mapListWithIcon(customChainInfos, chainIcons, "chainId"); diff --git a/packages/oraidex-common/src/constant.ts b/packages/oraidex-common/src/constant.ts index b273600f..8483ec7e 100644 --- a/packages/oraidex-common/src/constant.ts +++ b/packages/oraidex-common/src/constant.ts @@ -1,4 +1,4 @@ -import { CosmosChainId, EvmChainId, chainInfos, cosmosChains, evmChains } from "./network"; +import { CosmosChainId, EvmChainId } from "./network"; export const truncDecimals = 6; export const atomic = 10 ** truncDecimals; diff --git a/packages/oraidex-common/src/helper.ts b/packages/oraidex-common/src/helper.ts index df577ba7..93e02424 100644 --- a/packages/oraidex-common/src/helper.ts +++ b/packages/oraidex-common/src/helper.ts @@ -19,7 +19,7 @@ import { MULTIPLIER, CW20_DECIMALS } from "./constant"; -import { CoinGeckoId, cosmosChains } from "./network"; +import { CoinGeckoId } from "./network"; import { AmountDetails, TokenInfo, @@ -510,44 +510,44 @@ export const parseTxToMsgsAndEvents = (indexedTx: Tx, eventsParser?: (events: re }); }; -export const validateAndIdentifyCosmosAddress = (address: string, network: string) => { - try { - const cosmosAddressRegex = /^[a-z]{1,6}[0-9a-z]{0,64}$/; - if (!cosmosAddressRegex.test(address)) { - throw new Error("Invalid address"); - } - - const decodedAddress = bech32.decode(address); - const prefix = decodedAddress.prefix; - - let chainInfo; - const networkMap = cosmosChains.reduce((acc, cur) => { - if (cur.chainId === network) chainInfo = cur; - return { - ...acc, - [cur.bech32Config.bech32PrefixAccAddr]: true - }; - }, {}); - - if (chainInfo && chainInfo.bech32Config.bech32PrefixAccAddr !== prefix) { - throw new Error("Network doesn't match"); - } - - if (networkMap.hasOwnProperty(prefix)) { - return { - isValid: true, - network - }; - } else { - throw new Error("Unsupported address network"); - } - } catch (error) { - return { - isValid: false, - error: error.message - }; - } -}; +// export const validateAndIdentifyCosmosAddress = (address: string, network: string) => { +// try { +// const cosmosAddressRegex = /^[a-z]{1,6}[0-9a-z]{0,64}$/; +// if (!cosmosAddressRegex.test(address)) { +// throw new Error("Invalid address"); +// } + +// const decodedAddress = bech32.decode(address); +// const prefix = decodedAddress.prefix; + +// let chainInfo; +// const networkMap = cosmosChains.reduce((acc, cur) => { +// if (cur.chainId === network) chainInfo = cur; +// return { +// ...acc, +// [cur.bech32Config.bech32PrefixAccAddr]: true +// }; +// }, {}); + +// if (chainInfo && chainInfo.bech32Config.bech32PrefixAccAddr !== prefix) { +// throw new Error("Network doesn't match"); +// } + +// if (networkMap.hasOwnProperty(prefix)) { +// return { +// isValid: true, +// network +// }; +// } else { +// throw new Error("Unsupported address network"); +// } +// } catch (error) { +// return { +// isValid: false, +// error: error.message +// }; +// } +// }; export const validateEvmAddress = (address: string, network: string) => { try { @@ -587,17 +587,17 @@ export const validateTronAddress = (address: string, network: string) => { } }; -export const checkValidateAddressWithNetwork = (address: string, network: string) => { - switch (network) { - case "0x01": - case "0x38": - return validateEvmAddress(address, network); +// export const checkValidateAddressWithNetwork = (address: string, network: string) => { +// switch (network) { +// case "0x01": +// case "0x38": +// return validateEvmAddress(address, network); - // tron - case "0x2b6653dc": - return validateTronAddress(address, network); +// // tron +// case "0x2b6653dc": +// return validateTronAddress(address, network); - default: - return validateAndIdentifyCosmosAddress(address, network); - } -}; +// default: +// return validateAndIdentifyCosmosAddress(address, network); +// } +// }; diff --git a/packages/oraidex-common/src/network.ts b/packages/oraidex-common/src/network.ts index 52793f4b..590f94ab 100644 --- a/packages/oraidex-common/src/network.ts +++ b/packages/oraidex-common/src/network.ts @@ -1,7 +1,6 @@ import { BridgeAppCurrency as BridgeAppCurrencyCommon, - CustomChainInfo as CustomChainInfoCommon, - OraiCommon + CustomChainInfo as CustomChainInfoCommon } from "@oraichain/common"; import { AMM_V3_CONTRACT, @@ -17,7 +16,7 @@ import { ROUTER_V2_CONTRACT, STAKING_CONTRACT } from "./constant"; -import { readSupportedChainInfoStatic, supportedBridge, SupportedChainInfo } from "./supported"; +import { supportedBridge, SupportedChainInfo } from "./supported"; export type NetworkName = | "Oraichain" @@ -107,31 +106,30 @@ export interface NetworkConfig { export type CoinIcon = any; export type BridgeAppCurrency = BridgeAppCurrencyCommon; export type CoinType = 118 | 60 | 195; - export type CustomChainInfo = CustomChainInfoCommon; -let oraiCommon: OraiCommon = null; -let supportedChainIds = []; -let tokenListSupports: SupportedChainInfo = {}; -let mapDenomWithCoinGeckoId = {}; +// let oraiCommon: OraiCommon = null; +// let supportedChainIds = []; +// let tokenListSupports: SupportedChainInfo = {}; +// let mapDenomWithCoinGeckoId = {}; -const initOraiCommon = async () => { - if (!oraiCommon) { - oraiCommon = await OraiCommon.initializeFromBackend(); - const tokenListSupports = await readSupportedChainInfoStatic(); +// const initOraiCommon = async () => { +// if (!oraiCommon) { +// oraiCommon = await OraiCommon.initializeFromBackend(); +// const tokenListSupports = await readSupportedChainInfoStatic(); - supportedChainIds.push(...Object.keys(tokenListSupports)); - Object.values(tokenListSupports).map((item) => { - mapDenomWithCoinGeckoId = Object.values(item).reduce((acc, cur) => { - acc[cur.denom] = cur.coingecko_id; +// supportedChainIds.push(...Object.keys(tokenListSupports)); +// Object.values(tokenListSupports).map((item) => { +// mapDenomWithCoinGeckoId = Object.values(item).reduce((acc, cur) => { +// acc[cur.denom] = cur.coingecko_id; - return acc; - }, {}); - }); - } +// return acc; +// }, {}); +// }); +// } - return { oraiCommon, supportedChainIds }; -}; +// return { oraiCommon, supportedChainIds }; +// }; export const defaultBech32Config = ( mainPrefix: string, @@ -251,75 +249,75 @@ export const OsmoToken: BridgeAppCurrency = { } }; -export const oraichainNetwork: CustomChainInfo = { - ...oraiCommon.chainInfos.getSpecificChainInfo("Oraichain"), - bech32Config: defaultBech32Config("orai"), - currencies: oraiCommon.chainInfos.getSpecificChainInfo("Oraichain").currencies.map((currency) => { - const coingeckoId = - mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; - if (coingeckoId) { - return { - ...currency, - coinGeckoId: coingeckoId, - bridgeTo: !supportedBridge.Oraichain?.[coingeckoId]?.length - ? undefined - : supportedBridge.Oraichain?.[coingeckoId] - }; - } - return currency; - }) -}; +// export const oraichainNetwork: CustomChainInfo = { +// ...oraiCommon.chainInfos.getSpecificChainInfo("Oraichain"), +// bech32Config: defaultBech32Config("orai"), +// currencies: oraiCommon.chainInfos.getSpecificChainInfo("Oraichain").currencies.map((currency) => { +// const coingeckoId = +// mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; +// if (coingeckoId) { +// return { +// ...currency, +// coinGeckoId: coingeckoId, +// bridgeTo: !supportedBridge.Oraichain?.[coingeckoId]?.length +// ? undefined +// : supportedBridge.Oraichain?.[coingeckoId] +// }; +// } +// return currency; +// }) +// }; -export const chainInfos: CustomChainInfo[] = oraiCommon.chainInfos.chainInfos - .filter((chain) => supportedChainIds.includes(chain.chainId)) - .map((c) => { - const updatedCurrencies = c.currencies.map((currency) => { - const coingeckoId = - mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; - if (coingeckoId) { - return { - ...currency, - coinGeckoId: coingeckoId, - bridgeTo: !supportedBridge[c.chainId]?.[coingeckoId]?.length - ? undefined - : supportedBridge[c.chainId]?.[coingeckoId] - }; - } - return currency; - }); +// export const chainInfos: CustomChainInfo[] = oraiCommon.chainInfos.chainInfos +// .filter((chain) => supportedChainIds.includes(chain.chainId)) +// .map((c) => { +// const updatedCurrencies = c.currencies.map((currency) => { +// const coingeckoId = +// mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; +// if (coingeckoId) { +// return { +// ...currency, +// coinGeckoId: coingeckoId, +// bridgeTo: !supportedBridge[c.chainId]?.[coingeckoId]?.length +// ? undefined +// : supportedBridge[c.chainId]?.[coingeckoId] +// }; +// } +// return currency; +// }); - return { - ...c, - currencies: updatedCurrencies, - Icon: c.chainLogoSvg || c.chainLogoPng, - IconLight: c.chainLogoSvg || c.chainLogoPng - }; - }); +// return { +// ...c, +// currencies: updatedCurrencies, +// Icon: c.chainLogoSvg || c.chainLogoPng, +// IconLight: c.chainLogoSvg || c.chainLogoPng +// }; +// }); -export const network: CustomChainInfo & NetworkConfig = { - ...oraichainNetwork, - prefix: oraichainNetwork.bech32Config.bech32PrefixAccAddr, - denom: "orai", - coinType: oraichainNetwork.bip44.coinType as any, - fee: { gasPrice: "0.00506", amount: "1518", gas: "2000000" }, // 0.000500 ORAI - factory: FACTORY_CONTRACT, - factory_v2: FACTORY_V2_CONTRACT, - router: ROUTER_V2_CONTRACT, - mixer_router: MIXED_ROUTER, - oracle: ORACLE_CONTRACT, - staking: STAKING_CONTRACT, - rewarder: REWARDER_CONTRACT, - converter: CONVERTER_CONTRACT, - oraidex_listing: ORAIDEX_LISTING_CONTRACT, - multicall: MULTICALL_CONTRACT, - bid_pool: ORAIDEX_BID_POOL_CONTRACT, - explorer: "https://scan.orai.io", - pool_v3: AMM_V3_CONTRACT -}; +// export const network: CustomChainInfo & NetworkConfig = { +// ...oraichainNetwork, +// prefix: oraichainNetwork.bech32Config.bech32PrefixAccAddr, +// denom: "orai", +// coinType: oraichainNetwork.bip44.coinType as any, +// fee: { gasPrice: "0.00506", amount: "1518", gas: "2000000" }, // 0.000500 ORAI +// factory: FACTORY_CONTRACT, +// factory_v2: FACTORY_V2_CONTRACT, +// router: ROUTER_V2_CONTRACT, +// mixer_router: MIXED_ROUTER, +// oracle: ORACLE_CONTRACT, +// staking: STAKING_CONTRACT, +// rewarder: REWARDER_CONTRACT, +// converter: CONVERTER_CONTRACT, +// oraidex_listing: ORAIDEX_LISTING_CONTRACT, +// multicall: MULTICALL_CONTRACT, +// bid_pool: ORAIDEX_BID_POOL_CONTRACT, +// explorer: "https://scan.orai.io", +// pool_v3: AMM_V3_CONTRACT +// }; // exclude kawaiverse subnet and other special evm that has different cointype -export const evmChains = chainInfos.filter((c) => c.networkType === "evm"); -export const cosmosChains: CustomChainInfo[] = chainInfos.filter((c) => c.networkType === "cosmos"); +// export const evmChains = chainInfos.filter((c) => c.networkType === "evm"); +// export const cosmosChains: CustomChainInfo[] = chainInfos.filter((c) => c.networkType === "cosmos"); // evm network export enum Networks { diff --git a/packages/oraidex-common/src/test.ts b/packages/oraidex-common/src/test.ts index fb89addc..bb6968c3 100644 --- a/packages/oraidex-common/src/test.ts +++ b/packages/oraidex-common/src/test.ts @@ -1,13 +1,29 @@ -import { ChainInfos, OraiCommon, TokenItems } from "@oraichain/common"; +import { ChainInfos, MULTICALL_CONTRACT, OraiCommon, TokenItems } from "@oraichain/common"; import { flatten } from "lodash"; -import { mapListWithIcon, tokensIcon } from "./config"; - -export class TokenService { +import { chainIcons, mapListWithIcon, tokensIcon } from "./config"; +import { + AMM_V3_CONTRACT, + CONVERTER_CONTRACT, + FACTORY_CONTRACT, + FACTORY_V2_CONTRACT, + MIXED_ROUTER, + ORACLE_CONTRACT, + ORAIDEX_BID_POOL_CONTRACT, + ORAIDEX_LISTING_CONTRACT, + REWARDER_CONTRACT, + ROUTER_V2_CONTRACT, + STAKING_CONTRACT +} from "./constant"; +import bech32 from "bech32"; +import { CustomChainInfo, NetworkConfig } from "./network"; +import { validateEvmAddress, validateTronAddress } from "./helper"; + +export class OraidexCommon { constructor(private readonly tokenConfig: TokenItems, private readonly chainConfig: ChainInfos) {} - static async load(): Promise { + static async load(): Promise { const oraiCommon = await OraiCommon.initializeFromBackend("https://oraicommon-staging.oraidex.io", "oraidex"); - return new TokenService(oraiCommon.tokenItems, oraiCommon.chainInfos); + return new OraidexCommon(oraiCommon.tokenItems, oraiCommon.chainInfos); } get oraichainTokens() { @@ -73,16 +89,153 @@ export class TokenService { get flattenTokensWithIcon() { return flatten(this.tokensWithIcon); } + + get oraichainNetwork() { + return this.chainConfig.getSpecificChainInfo("Oraichain"); + // bech32Config: defaultBech32Config("orai"), + // currencies: oraiCommon.chainInfos.getSpecificChainInfo("Oraichain").currencies.map((currency) => { + // const coingeckoId = + // mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; + // if (coingeckoId) { + // return { + // ...currency, + // coinGeckoId: coingeckoId, + // bridgeTo: !supportedBridge.Oraichain?.[coingeckoId]?.length + // ? undefined + // : supportedBridge.Oraichain?.[coingeckoId] + // }; + // } + // return currency; + // }) + } + + get chainInfos() { + return this.chainConfig.chainInfos; + + // .filter((chain) => supportedChainIds.includes(chain.chainId)) + // .map((c) => { + // const updatedCurrencies = c.currencies.map((currency) => { + // const coingeckoId = + // mapDenomWithCoinGeckoId[currency.coinMinimalDenom] || mapDenomWithCoinGeckoId[currency.contractAddress]; + // if (coingeckoId) { + // return { + // ...currency, + // coinGeckoId: coingeckoId, + // bridgeTo: !supportedBridge[c.chainId]?.[coingeckoId]?.length + // ? undefined + // : supportedBridge[c.chainId]?.[coingeckoId] + // }; + // } + // return currency; + // }); + + // return { + // ...c, + // currencies: updatedCurrencies, + // Icon: c.chainLogoSvg || c.chainLogoPng, + // IconLight: c.chainLogoSvg || c.chainLogoPng + // }; + } + + get network(): CustomChainInfo & NetworkConfig { + return { + ...this.oraichainNetwork, + prefix: this.oraichainNetwork.bech32Config.bech32PrefixAccAddr, + denom: "orai", + coinType: this.oraichainNetwork.bip44.coinType as any, + fee: { gasPrice: "0.00506", amount: "1518", gas: "2000000" }, // 0.000500 ORAI + factory: FACTORY_CONTRACT, + factory_v2: FACTORY_V2_CONTRACT, + router: ROUTER_V2_CONTRACT, + mixer_router: MIXED_ROUTER, + oracle: ORACLE_CONTRACT, + staking: STAKING_CONTRACT, + rewarder: REWARDER_CONTRACT, + converter: CONVERTER_CONTRACT, + oraidex_listing: ORAIDEX_LISTING_CONTRACT, + multicall: MULTICALL_CONTRACT, + bid_pool: ORAIDEX_BID_POOL_CONTRACT, + explorer: "https://scan.orai.io", + pool_v3: AMM_V3_CONTRACT + }; + } + + get evmChains() { + return this.chainConfig.evmChains; + } + + get cosmosChains() { + return this.chainConfig.cosmosChains; + } + + validateAndIdentifyCosmosAddress(address: string, network: string) { + try { + const cosmosAddressRegex = /^[a-z]{1,6}[0-9a-z]{0,64}$/; + if (!cosmosAddressRegex.test(address)) { + throw new Error("Invalid address"); + } + + const decodedAddress = bech32.decode(address); + const prefix = decodedAddress.prefix; + + let chainInfo; + const networkMap = this.cosmosChains.reduce((acc, cur) => { + if (cur.chainId === network) chainInfo = cur; + return { + ...acc, + [cur.bech32Config.bech32PrefixAccAddr]: true + }; + }, {}); + + if (chainInfo && chainInfo.bech32Config.bech32PrefixAccAddr !== prefix) { + throw new Error("Network doesn't match"); + } + + if (networkMap.hasOwnProperty(prefix)) { + return { + isValid: true, + network + }; + } else { + throw new Error("Unsupported address network"); + } + } catch (error) { + return { + isValid: false, + error: error.message + }; + } + } + + checkValidateAddressWithNetwork(address: string, network: string) { + switch (network) { + case "0x01": + case "0x38": + return validateEvmAddress(address, network); + + // tron + case "0x2b6653dc": + return validateTronAddress(address, network); + + default: + return this.validateAndIdentifyCosmosAddress(address, network); + } + } + + get chainInfosWithIcon() { + return mapListWithIcon(this.chainInfos, chainIcons, "chainId"); + } } -// const main = async () => { -// const tokenService = await TokenService.load(); -// }; - -// main() -// .then(() => { -// console.log("Done"); -// }) -// .catch((error) => { -// console.error(error); -// }); +const main = async () => { + const tokenService = await OraidexCommon.load(); + console.log(tokenService.oraichainNetwork); +}; + +main() + .then(() => { + console.log("Done"); + }) + .catch((error) => { + console.error(error); + }); diff --git a/packages/oraiswap-v3/package.json b/packages/oraiswap-v3/package.json index 76ccc050..7dc25b02 100644 --- a/packages/oraiswap-v3/package.json +++ b/packages/oraiswap-v3/package.json @@ -13,6 +13,6 @@ "dependencies": { "@cosmjs/cosmwasm-stargate": "^0.32.4", "@oraichain/oraidex-contracts-sdk": "^1.0.54", - "@oraichain/oraidex-common": "../oraidex-common" + "@oraichain/oraidex-common": "^1.1.32" } } diff --git a/yarn.lock b/yarn.lock index 10ddc713..beaf7ea4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3805,9 +3805,6 @@ resolved "https://registry.yarnpkg.com/@oraichain/immutable/-/immutable-4.3.9.tgz#ff8d5a7b39b5b01f3f72a902cffbfea32ccb20c3" integrity sha512-INpHnhL970OCkR7I71Kssb2aLl2l4Y/x8W6FlyRO0KmC8GHjxc/hlNB1t44BiI7lkOYmcWMRQoC8dwParsp1RQ== -"@oraichain/oraidex-common@../oraidex-common": - version "1.0.13" - "@oraichain/oraidex-common@^1.0.10": version "1.1.28" resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-1.1.28.tgz#9bf25c825e3ee7a76bf8c00174579ca2b81d6fdb" @@ -3830,6 +3827,28 @@ ethers "^5.0.15" tronweb "6.0.0-beta.4" +"@oraichain/oraidex-common@^1.1.32": + version "1.1.32" + resolved "https://registry.yarnpkg.com/@oraichain/oraidex-common/-/oraidex-common-1.1.32.tgz#c787dc3451584ffccd63a20bb230a5dd0a2e383f" + integrity sha512-8DbfumZ26fKFN1yhiQbJOSoXdqHuoU3i2JvVLpSOtveYUd1Z5rBDs3XVS7odNcUAgF0osqzq5sg9+4yUzruZ8Q== + dependencies: + "@cosmjs/amino" "^0.32.4" + "@cosmjs/cosmwasm-stargate" "^0.32.4" + "@cosmjs/crypto" "^0.32.4" + "@cosmjs/proto-signing" "^0.32.4" + "@cosmjs/stargate" "^0.32.4" + "@cosmjs/tendermint-rpc" "^0.32.4" + "@ethersproject/providers" "^5.0.10" + "@injectivelabs/sdk-ts" "1.12.1" + "@keplr-wallet/types" "^0.11.38" + "@oraichain/oraidex-contracts-sdk" latest + axios "1.7.2" + axios-extensions "3.1.7" + bignumber.js "^9.1.2" + cosmjs-types "^0.9.0" + ethers "^5.0.15" + tronweb "6.0.0-beta.4" + "@oraichain/oraidex-contracts-sdk@latest": version "1.0.44" resolved "https://registry.yarnpkg.com/@oraichain/oraidex-contracts-sdk/-/oraidex-contracts-sdk-1.0.44.tgz#9ff41ec388dd92ba112c2eef545d11fd6e18c684"