Skip to content

Commit

Permalink
fix: network.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vuonghuuhung committed Nov 29, 2024
1 parent b7f3b96 commit dbefa92
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 161 deletions.
3 changes: 1 addition & 2 deletions packages/oraidex-common/src/config/chainInfosWithIcon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CustomChainInfo, chainInfos as customChainInfos } from "../network";
import { TokenItemType } from "../token";
import {
AiriIcon,
Expand Down Expand Up @@ -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");
2 changes: 1 addition & 1 deletion packages/oraidex-common/src/constant.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
102 changes: 51 additions & 51 deletions packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
MULTIPLIER,
CW20_DECIMALS
} from "./constant";
import { CoinGeckoId, cosmosChains } from "./network";
import { CoinGeckoId } from "./network";
import {
AmountDetails,
TokenInfo,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
// }
// };
170 changes: 84 additions & 86 deletions packages/oraidex-common/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
BridgeAppCurrency as BridgeAppCurrencyCommon,
CustomChainInfo as CustomChainInfoCommon,
OraiCommon
CustomChainInfo as CustomChainInfoCommon
} from "@oraichain/common";
import {
AMM_V3_CONTRACT,
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit dbefa92

Please sign in to comment.